diff --git a/2025/d03.py b/2025/d03.py new file mode 100644 index 0000000..86d9973 --- /dev/null +++ b/2025/d03.py @@ -0,0 +1,35 @@ +from lib import get_data + +data = get_data(__file__) + + +def max_joltage(xs: str, target_len: int) -> int: + def dynamic(xs: str) -> dict[int, str]: + if len(xs) == 0: + return {0: ""} + + rs = dynamic(xs[1:]) + for i in range(target_len, 0, -1): + if not (i - 1) in rs: + continue + nv = int(xs[0] + rs[i - 1]) + if not i in rs: + rs[i] = str(nv) + elif nv > int(rs[i]): + rs[i] = str(nv) + return rs + + rs = dynamic(xs) + return int(rs[target_len]) + + +r1 = 0 +r2 = 0 +for line in data.splitlines(): + r1 += max_joltage(line, 2) + r2 += max_joltage(line, 12) + + +print(r1) +print(r2) + diff --git a/2025/d04.py b/2025/d04.py new file mode 100644 index 0000000..e2e65e8 --- /dev/null +++ b/2025/d04.py @@ -0,0 +1,32 @@ +from lib import get_data, Grid2D + +data = get_data(__file__) +g = Grid2D(data) + +r = 0 +adjs = [] +for pos in g.all_coords(): + if g[pos] != "@": + continue + nr_of_rolls_adj = len([nb for nb in g.neighbors_adj(pos) if g[nb] == "@"]) + if nr_of_rolls_adj < 4: + r += 1 + adjs.append(pos) +print(r) + +r = 0 +while True: + adjs = [] + for pos in g.all_coords(): + if g[pos] != "@": + continue + nr_of_rolls_adj = len([nb for nb in g.neighbors_adj(pos) if g[nb] == "@"]) + if nr_of_rolls_adj < 4: + adjs.append(pos) + if adjs: + r += len(adjs) + for a in adjs: + g[a] = "." + else: + break +print(r) diff --git a/README.md b/README.md index 1d6c023..6af0e09 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,10 @@ stressful and this will actually be more fun. Thank you Eric Wastl and let's go! ticks naively. Maybe I should revisit this. - Day 2: The simple direct approach with iterating over all the IDs in the ranges just worked okay. Not pretty but acceptable. -- Day 3: +- Day 3: Fun dynamic programming problem. Part 2 took me a little too long + overall but it was fun. +- Day 4: One of the easier grid based puzzles. Less than five minutes with + existing grid library. ## AoC 2024