Solve 2025 day 5
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -4,6 +4,8 @@
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
.python-version
|
||||
uv.lock
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
45
2025/d05.py
45
2025/d05.py
@@ -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
|
||||
|
||||
@@ -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
10
pyproject.toml
Normal 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",
|
||||
]
|
||||
Reference in New Issue
Block a user