Advent of Code 2024, Day 10

I lost time in part 1 because I had accidentally implemented part 2 already. So to solve part 2, I had to undo my fixes.

I took a similar approach as in previous days:

Part 1

import sys

def score(lines, i, j, x):
    if not 0 <= i < len(lines): return set()
    if not 0 <= j < len(lines[i]): return set()
    if not lines[i][j] == x: return set()
    if x == 9: return {(i, j)}
    a = score(lines, i-1, j, x+1)
    b = score(lines, i+1, j, x+1)
    c = score(lines, i, j-1, x+1)
    d = score(lines, i, j+1, x+1)
    return a.union(b.union(c.union(d)))

with open(sys.argv[1]) as f:
    lines = f.readlines()
    lines = [[int(c) for c in line.strip()] for line in lines]

s = 0
for i in range(len(lines)):
    for j in range(len(lines[i])):
        s += len(score(lines, i, j, 0))
print(s)

Part 2

import sys

def score(lines, i, j, x):
    if not 0 <= i < len(lines): return 0
    if not 0 <= j < len(lines[i]): return 0
    if not lines[i][j] == x: return 0
    if x == 9: return 1
    a = score(lines, i-1, j, x+1)
    b = score(lines, i+1, j, x+1)
    c = score(lines, i, j-1, x+1)
    d = score(lines, i, j+1, x+1)
    return a+b+c+d

with open(sys.argv[1]) as f:
    lines = f.readlines()
    lines = [[int(c) for c in line.strip()] for line in lines]

s = 0
for i in range(len(lines)):
    for j in range(len(lines[i])):
        s += score(lines, i, j, 0)
print(s)

2024-12-10

Proxy Information
Original URL
gemini://dkalak.de/aoc/10.gmi
Status Code
Success (20)
Meta
text/gemini; lang=en
Capsule Response Time
106.021732 milliseconds
Gemini-to-HTML Time
0.248599 milliseconds

This content has been proxied by September (ba2dc).