Solve 2025 day 5

This commit is contained in:
2026-01-11 12:02:40 -05:00
parent df0682e989
commit d114310864
4 changed files with 60 additions and 2 deletions

2
.gitignore vendored
View File

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

View File

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

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. overall but it was fun.
- Day 4: One of the easier grid based puzzles. Less than five minutes with - Day 4: One of the easier grid based puzzles. Less than five minutes with
existing grid library. 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 ## 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",
]