Skip to content

Commit

Permalink
Day 13: Claw Contraption
Browse files Browse the repository at this point in the history
  • Loading branch information
ephemient committed Dec 13, 2024
1 parent 7f1d76b commit 4c30227
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ Development occurs in language-specific directories:
|[Day10.hs](hs/src/Day10.hs)|[Day10.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day10.kt)|[day10.py](py/aoc2024/day10.py)|[day10.rs](rs/src/day10.rs)|
|[Day11.hs](hs/src/Day11.hs)|[Day11.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day11.kt)|[day11.py](py/aoc2024/day11.py)|[day11.rs](rs/src/day11.rs)|
|[Day12.hs](hs/src/Day12.hs)|[Day12.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day12.kt)|[day12.py](py/aoc2024/day12.py)||
|[Day12.hs](hs/src/Day12.hs)|[Day12.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day12.kt)|||
|[Day13.hs](hs/src/Day13.hs)|[Day13.kt](kt/aoc2024-lib/src/commonMain/kotlin/com/github/ephemient/aoc2024/Day13.kt)|[day13.py](py/aoc2024/day13.py)||
52 changes: 52 additions & 0 deletions py/aoc2024/day13.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
"""
Day 13: Claw Contraption
"""

import re
from typing import Generator

SAMPLE_INPUT = """
Button A: X+94, Y+34
Button B: X+22, Y+67
Prize: X=8400, Y=5400
Button A: X+26, Y+66
Button B: X+67, Y+21
Prize: X=12748, Y=12176
Button A: X+17, Y+86
Button B: X+84, Y+37
Prize: X=7870, Y=6450
Button A: X+69, Y+23
Button B: X+27, Y+71
Prize: X=18641, Y=10279
"""

_pattern = re.compile(r"\d+")

def _parse(data: str) -> Generator[tuple[int, int, int, int, int, int]]:
return zip(*((int(match.group(0)) for match in _pattern.finditer(data)),) * 6)

def _solve(ax: int, ay: int, bx: int, by: int, x: int, y: int) -> int:
anumerator = x * by - y * bx
adenominator = ax * by - bx * ay
bnumerator = x * ay - y * ax
bdenominator = ay * bx - by * ax
if anumerator % adenominator or bnumerator % bdenominator:
return 0
a = anumerator // adenominator
b = bnumerator // bdenominator
return 3 * a + b

def part1(data: str) -> int:
"""
>>> part1(SAMPLE_INPUT)
480
"""
return sum(_solve(ax, ay, bx, by, x, y) for ax, ay, bx, by, x, y in _parse(data))

def part2(data: str) -> int:
return sum(_solve(ax, ay, bx, by, x + 10000000000000, y + 10000000000000) for ax, ay, bx, by, x, y in _parse(data))

parts = (part1, part2)
1 change: 1 addition & 0 deletions py/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ day9 = "aoc2024.day9:parts"
day10 = "aoc2024.day10:parts"
day11 = "aoc2024.day11:parts"
day12 = "aoc2024.day12:parts"
day13 = "aoc2024.day13:parts"

[build-system]
requires = ["poetry-core"]
Expand Down

0 comments on commit 4c30227

Please sign in to comment.