-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.py
48 lines (34 loc) · 1.46 KB
/
script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from GhostyUtils import aoc
from GhostyUtils.vec2 import Vec2
def solve(machine: list[list[tuple], tuple], offset: int = 0) -> int:
a, b = [Vec2(button) for button in machine[0]]
p = Vec2(machine[1]) + Vec2(offset, offset)
if aoc.args.progress or aoc.args.verbose:
print(f"A vec: {a}, B vec: {b}, Prize location: {p}")
det = (a.x * b.y - a.y * b.x)
a_presses = int((p.x * b.y - p.y * b.x) / det)
b_presses = int((p.y * a.x - p.x * a.y) / det)
if a * a_presses + b * b_presses == p:
if aoc.args.progress or aoc.args.verbose:
print(f"> A: {a_presses}x, B: {b_presses}x, Tokens: {a_presses * 3 + b_presses}")
return a_presses * 3 + b_presses
else:
if aoc.args.progress or aoc.args.verbose:
print("> Misses")
return 0
def main():
inputs = aoc.read_sections()
machines = []
for mch in inputs:
mch = mch.splitlines()
buttons = [tuple(int(coord[1:])
for coord in btn.split(': ')[1].split(', '))
for btn in mch[:2]]
prize = tuple(int(coord[2:]) for coord in mch[2].split(': ')[1].split(', '))
machines.append([buttons, prize])
if aoc.args.verbose:
print(f"buttons: {buttons}, prize: {prize}")
print(f"p1: {sum(solve(machine) for machine in machines)}")
print(f"p2: {sum(solve(machine, offset=10000000000000) for machine in machines)}")
if __name__ == "__main__":
main()