Do day 3 and 4 for 2025
This commit is contained in:
35
2025/d03.py
Normal file
35
2025/d03.py
Normal file
@@ -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)
|
||||||
|
|
||||||
32
2025/d04.py
Normal file
32
2025/d04.py
Normal file
@@ -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)
|
||||||
@@ -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.
|
ticks naively. Maybe I should revisit this.
|
||||||
- Day 2: The simple direct approach with iterating over all the IDs in the
|
- Day 2: The simple direct approach with iterating over all the IDs in the
|
||||||
ranges just worked okay. Not pretty but acceptable.
|
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
|
## AoC 2024
|
||||||
|
|||||||
Reference in New Issue
Block a user