|
| 1 | +#! /usr/bin/env python3 |
| 2 | +# |
| 3 | +# Advent of Code 2024 Day 13 |
| 4 | +# |
| 5 | + |
| 6 | +import sys |
| 7 | + |
| 8 | +from aoc import my_aocd |
| 9 | +from aoc.common import InputData |
| 10 | +from aoc.common import SolutionBase |
| 11 | +from aoc.common import aoc_samples |
| 12 | + |
| 13 | +Input = list[tuple[tuple[int, int], tuple[int, int], tuple[int, int]]] |
| 14 | +Output1 = int |
| 15 | +Output2 = int |
| 16 | + |
| 17 | + |
| 18 | +TEST = """\ |
| 19 | +Button A: X+94, Y+34 |
| 20 | +Button B: X+22, Y+67 |
| 21 | +Prize: X=8400, Y=5400 |
| 22 | +
|
| 23 | +Button A: X+26, Y+66 |
| 24 | +Button B: X+67, Y+21 |
| 25 | +Prize: X=12748, Y=12176 |
| 26 | +
|
| 27 | +Button A: X+17, Y+86 |
| 28 | +Button B: X+84, Y+37 |
| 29 | +Prize: X=7870, Y=6450 |
| 30 | +
|
| 31 | +Button A: X+69, Y+23 |
| 32 | +Button B: X+27, Y+71 |
| 33 | +Prize: X=18641, Y=10279 |
| 34 | +""" |
| 35 | + |
| 36 | + |
| 37 | +class Solution(SolutionBase[Input, Output1, Output2]): |
| 38 | + def parse_input(self, input_data: InputData) -> Input: |
| 39 | + machines = [] |
| 40 | + for block in my_aocd.to_blocks(input_data): |
| 41 | + a = (int(block[0][12:14]), int(block[0][18:20])) |
| 42 | + b = (int(block[1][12:14]), int(block[1][18:20])) |
| 43 | + sp = block[2].split(", ") |
| 44 | + px = int(sp[0].split("=")[1]) |
| 45 | + py = int(sp[1][2:]) |
| 46 | + machines.append((a, b, (px, py))) |
| 47 | + return machines |
| 48 | + |
| 49 | + def guess( |
| 50 | + self, ax: int, bx: int, ay: int, by: int, px: int, py: int |
| 51 | + ) -> int | None: |
| 52 | + # best = sys.maxsize |
| 53 | + div = bx * ay - ax * by |
| 54 | + ans_a = (py * bx - px * by) / div |
| 55 | + ans_b = (px * ay - py * ax) / div |
| 56 | + if int(ans_a) == ans_a: |
| 57 | + return int(ans_a) * 3 + int(ans_b) |
| 58 | + # for ans_a in range(100, 0, -1): |
| 59 | + # for ans_b in range(1, 101): |
| 60 | + # if ( |
| 61 | + # ans_a * ax + ans_b * bx == px |
| 62 | + # and ans_a * ay + ans_b * by == py |
| 63 | + # ): |
| 64 | + # best = min(best, ans_a * 3 + ans_b) |
| 65 | + # if best < sys.maxsize: |
| 66 | + # return best |
| 67 | + else: |
| 68 | + return None |
| 69 | + |
| 70 | + def part_1(self, machines: Input) -> Output1: |
| 71 | + ans = 0 |
| 72 | + for a, b, p in machines: |
| 73 | + ax, ay = a |
| 74 | + bx, by = b |
| 75 | + px, py = p |
| 76 | + g = self.guess(ax, bx, ay, by, px, py) |
| 77 | + if g is not None: |
| 78 | + ans += g |
| 79 | + return ans |
| 80 | + |
| 81 | + def part_2(self, machines: Input) -> Output2: |
| 82 | + MOD = 10_000_000_000_000 |
| 83 | + ans = 0 |
| 84 | + for a, b, p in machines: |
| 85 | + ax, ay = a |
| 86 | + bx, by = b |
| 87 | + px, py = p |
| 88 | + g = self.guess(ax, bx, ay, by, MOD + px, MOD + py) |
| 89 | + if g is not None: |
| 90 | + ans += g |
| 91 | + return ans |
| 92 | + |
| 93 | + @aoc_samples( |
| 94 | + ( |
| 95 | + ("part_1", TEST, 480), |
| 96 | + ) |
| 97 | + ) |
| 98 | + def samples(self) -> None: |
| 99 | + pass |
| 100 | + |
| 101 | + |
| 102 | +solution = Solution(2024, 13) |
| 103 | + |
| 104 | + |
| 105 | +def main() -> None: |
| 106 | + solution.run(sys.argv) |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + main() |
0 commit comments