|
| 1 | +from aoc import * |
| 2 | +import re |
| 3 | +from itertools import product |
| 4 | + |
| 5 | + |
| 6 | +def main(infi: str): |
| 7 | + inp = filerstrip(infi) |
| 8 | + machines = [] |
| 9 | + for machine in inp.split('\n\n'): |
| 10 | + lines = machine.split('\n') |
| 11 | + a = re.fullmatch(r'Button A: X\+(.*?), Y\+(.*?)', lines[0]) |
| 12 | + b = re.fullmatch(r'Button B: X\+(.*?), Y\+(.*?)', lines[1]) |
| 13 | + prize = re.fullmatch(r'Prize: X=(.*?), Y=(.*?)', lines[2]) |
| 14 | + machines.append( |
| 15 | + ( |
| 16 | + (int(a[1]), int(a[2])), |
| 17 | + (int(b[1]), int(b[2])), |
| 18 | + (int(prize[1]), int(prize[2])), |
| 19 | + ) |
| 20 | + ) |
| 21 | + tokens_needed = 0 |
| 22 | + INITIAL = 999999999999999999 |
| 23 | + for machine in machines: |
| 24 | + tokens = INITIAL |
| 25 | + for a, b in product(range(1, 101), range(1, 101)): |
| 26 | + x = a * machine[0][0] + b * machine[1][0] |
| 27 | + y = a * machine[0][1] + b * machine[1][1] |
| 28 | + if (x, y) == machine[2]: |
| 29 | + tokens = min(tokens, a * 3 + b * 1) |
| 30 | + if tokens != INITIAL: |
| 31 | + tokens_needed += tokens |
| 32 | + return tokens_needed |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +DAY = 13 |
| 37 | +FILE_TEST = f"{DAY}_testa.txt" |
| 38 | +# FILE_TEST = f"{DAY}_testb.txt" |
| 39 | +FILE_EXP = f"{DAY}_exp.txt" |
| 40 | +FILE = f"{DAY}.txt" |
| 41 | +# test_and_submit(main, FILE_TEST, FILE_EXP, FILE, DAY) |
| 42 | +# print(main(FILE_TEST)) |
| 43 | +print(main(FILE)) |
0 commit comments