Compare commits

..

1 Commits

Author SHA1 Message Date
ba25ae677a Solve 2025 day 3 and 4 2026-01-11 10:33:07 -05:00
6 changed files with 2 additions and 185 deletions

2
.gitignore vendored
View File

@@ -4,8 +4,6 @@
__pycache__/
*.py[cod]
*$py.class
.python-version
uv.lock
# C extensions
*.so

View File

@@ -1,45 +1,4 @@
from lib import get_data
from lib import get_data, Grid2D
data = get_data(__file__)
ranges, ids = data.split("\n\n")
ids = tuple(map(int, ids.splitlines()))
ranges = [list(map(int, line.split("-"))) for line in ranges.splitlines()]
updated = True
while updated:
updated = False
sorted_ranges = []
for lo, up in ranges:
assert lo <= up
for i in range(len(sorted_ranges)):
slo, sup = sorted_ranges[i]
if up + 1 < slo:
sorted_ranges.insert(i, (lo, up))
elif lo < slo:
sorted_ranges[i] = (lo, max(up, sup))
updated = True
elif lo <= sup:
sorted_ranges[i] = (slo, max(up, sup))
updated = True
else:
continue
break
else:
sorted_ranges.append((lo, up))
ranges = sorted_ranges
result_part_1 = 0
for id_to_check in ids:
for lo, up in ranges:
if lo <= id_to_check <= up:
result_part_1 += 1
break
print(result_part_1)
result_part_2 = 0
for lo, up in ranges:
result_part_2 += up - lo + 1
print(result_part_2)
assert result_part_2 == 350780324308385
print(data)

View File

@@ -1,60 +0,0 @@
from lib import get_data
from typing import TypeVar
T = TypeVar('T')
data = """123 328 51 64
45 64 387 23
6 98 215 314
* + * + """
data = get_data(__file__)
def mul(xs):
r = 1
for x in xs:
r *= x
return r
lines = zip(*[line.split() for line in data.splitlines()])
r = 0
for xs in lines:
xs, op = xs[:-1], xs[-1]
xs = list(map(int, xs))
if op == "+":
r += sum(xs)
elif op == "*":
r += mul(xs)
else:
assert False, "Unexpected op"
print(r)
def split(xs: list[T], delim: T) -> list[list[T]]:
res = [[]]
for x in xs:
if x == delim:
res.append([])
else:
res[-1].append(x)
return res
chars_trans: list[tuple[str]] = list(zip(*[line for line in data.splitlines()]))
lines_trans: list[str] = list(map(lambda line: "".join(line).strip(), chars_trans))
cols: list[list[str]] = split(lines_trans, '')
r2 = 0
for xs in cols:
x0 = xs[0]
acc, op = int(x0[:-1]), x0[-1]
for x in xs[1:]:
x = int(x)
if op == "+":
acc += x
elif op == "*":
acc *= x
else:
assert False, "Unexpected op"
r2 += acc
print(r2)

View File

@@ -1,59 +0,0 @@
from lib import get_data, Grid2D
from collections import defaultdict
data = """.......S.......
...............
.......^.......
...............
......^.^......
...............
.....^.^.^.....
...............
....^.^...^....
...............
...^.^...^.^...
...............
..^...^.....^..
...............
.^.^.^.^.^...^.
..............."""
data = get_data(__file__)
g = Grid2D(data)
beams = [g.find('S')[0][1]]
splits = 0
for row in range(1, g.n_rows - 1):
new_beams = set()
for beam in beams:
field = g[(row, beam)]
if field == "^":
splits += 1
new_beams.add(beam - 1)
new_beams.add(beam + 1)
elif field == ".":
new_beams.add(beam)
else:
assert False, "unexpected field"
beams = list(new_beams)
# print(beams)
print(splits)
beams = {g.find('S')[0][1]: 1}
for row in range(1, g.n_rows - 1):
new_beams = defaultdict(int)
for beam, count in beams.items():
field = g[(row, beam)]
if field == "^":
new_beams[beam - 1] += count
new_beams[beam + 1] += count
elif field == ".":
new_beams[beam] += count
else:
assert False, "unexpected field"
beams = new_beams
print(sum(beams.values()))

View File

@@ -16,17 +16,6 @@ stressful and this will actually be more fun. Thank you Eric Wastl and let's go!
overall but it was fun.
- Day 4: One of the easier grid based puzzles. Less than five minutes with
existing grid library.
- Day 5: A problem with ranges; I decided to implement proper range merging this
time because I always kind of avoid that. I could probably decrease the
complexity from O(n^2) to O(n log(n)) but I am happy that I've implemented
merging at all.
- Day 6: Transposing some rows and cols. Fun and good to keep the brain fit
but not really hard. I would have been way too slow for the leaderboard
in pre-AI years.
- Day 7: Grid puzzle that required non-naiv implementation for part 2. Took
me a second to realize that I could not save the coordinates by list but
had to use a count to be efficient.
- Day 8:
## AoC 2024

View File

@@ -1,10 +0,0 @@
[project]
name = "aocpy"
version = "0.1.0"
description = "Make ruff and ty available via uv"
readme = "README.md"
requires-python = ">=3.13"
dependencies = [
"ruff>=0.14.11",
"ty>=0.0.11",
]