diff --git a/README.md b/README.md index 0f40d42..4665fcc 100644 --- a/README.md +++ b/README.md @@ -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)|| diff --git a/py/aoc2024/day13.py b/py/aoc2024/day13.py new file mode 100644 index 0000000..0ab919d --- /dev/null +++ b/py/aoc2024/day13.py @@ -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) diff --git a/py/pyproject.toml b/py/pyproject.toml index f8bc0e6..adcaf49 100644 --- a/py/pyproject.toml +++ b/py/pyproject.toml @@ -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"]