Solve 2025 day 5

This commit is contained in:
2026-01-11 12:02:40 -05:00
parent df0682e989
commit 867a7ed2df
5 changed files with 89 additions and 2 deletions

2
.gitignore vendored
View File

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

View File

@@ -1,4 +1,45 @@
from lib import get_data, Grid2D
from lib import get_data
data = get_data(__file__)
print(data)
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

29
2025/d06.py Normal file
View File

@@ -0,0 +1,29 @@
from lib import get_data
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)

View File

@@ -16,6 +16,11 @@ 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:
## AoC 2024

10
pyproject.toml Normal file
View File

@@ -0,0 +1,10 @@
[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",
]