Skip to content

Commit

Permalink
Day 9: Mirage Maintenance
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 9, 2023
1 parent a652dd8 commit 2330198
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ Development occurs in language-specific directories:
|[Day6.hs](hs/src/Day6.hs)|[Day6.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day6.kt)|[day6.py](py/aoc2023/day6.py)|[day6.rs](rs/src/day6.rs)|
|[Day7.hs](hs/src/Day7.hs)|[Day7.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day7.kt)|[day7.py](py/aoc2023/day7.py)|[day7.rs](rs/src/day7.rs)|
|[Day8.hs](hs/src/Day8.hs)|[Day8.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day8.kt)|[day8.py](py/aoc2023/day8.py)|[day8.rs](rs/src/day8.rs)|
|[Day9.hs](hs/src/Day9.hs)|[Day9.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day9.kt)|||
|[Day9.hs](hs/src/Day9.hs)|[Day9.kt](kt/aoc2023-lib/src/commonMain/kotlin/com/github/ephemient/aoc2023/Day9.kt)|[day9.py](py/aoc2023/day9.py)||
40 changes: 40 additions & 0 deletions py/aoc2023/day9.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""
Day 9: Mirage Maintenance
"""

SAMPLE_INPUT = """
0 3 6 9 12 15
1 3 6 10 15 21
10 13 16 21 30 45
"""


def _extrapolate(nums):
if any(nums):
return nums[-1] + _extrapolate([y - x for x, y in zip(nums, nums[1:])])
return 0


def part1(data):
"""
>>> part1(SAMPLE_INPUT)
114
"""
return sum(
_extrapolate([int(word) for word in line.split() if word])
for line in data.splitlines()
)


def part2(data):
"""
>>> part2(SAMPLE_INPUT)
2
"""
return sum(
_extrapolate([int(word) for word in line.split() if word][::-1])
for line in data.splitlines()
)


parts = (part1, part2)
1 change: 1 addition & 0 deletions py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ day5 = "aoc2023.day5:parts"
day6 = "aoc2023.day6:parts"
day7 = "aoc2023.day7:parts"
day8 = "aoc2023.day8:parts"
day9 = "aoc2023.day9:parts"

[tool.black]
target_version = ["py312"]
Expand Down

0 comments on commit 2330198

Please sign in to comment.