diff --git a/.gitignore b/.gitignore index 7a39e24..fc0825f 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,8 @@ __pycache__/ *.py[cod] *$py.class +.python-version +uv.lock # C extensions *.so diff --git a/2025/d05.py b/2025/d05.py index fd6c012..810ef0c 100644 --- a/2025/d05.py +++ b/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 diff --git a/README.md b/README.md index 6af0e09..cf8fdc5 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..084a1c7 --- /dev/null +++ b/pyproject.toml @@ -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", +]