-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday13.py
More file actions
80 lines (63 loc) · 2.31 KB
/
Copy pathday13.py
File metadata and controls
80 lines (63 loc) · 2.31 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from collections import namedtuple
from sympy import symbols, Eq, solve, Integer
filepath = "day13.txt"
Prize = namedtuple('Prize', 'x y')
Button = namedtuple('Button', 'x y')
Machine = namedtuple('Machine', 'prize button_a button_b')
def read_file(part = 1):
with open(filepath) as file:
machines = []
button_a = None
button_b = None
prize = None
for line in file.readlines():
if line.startswith("Button A"):
button_a = get_button(line)
elif line.startswith("Button B"):
button_b = get_button(line)
elif line.startswith("Prize"):
prize = get_prize(line, part)
else: # empty line
machines.append(Machine(prize, button_a, button_b))
return machines
def get_prize(line, part = 1):
tokens = line.strip().split()
x = int(tokens[1].split("=")[1][:-1])
y = int(tokens[2].split("=")[1])
if part == 2:
x += 10000000000000
y += 10000000000000
return Prize(x, y)
def get_button(line):
tokens = line.strip().split()
x = int(tokens[2].split("+")[1][:-1])
y = int(tokens[3].split("+")[1])
return Button(x, y)
def solve_machine_equation(machine: Machine):
x, y = symbols('x y')
eq1 = Eq(machine.button_a.x * x + machine.button_b.x * y, machine.prize.x)
eq2 = Eq(machine.button_a.y * x + machine.button_b.y * y, machine.prize.y)
solution = solve((eq1, eq2), (x, y))
return solution[x], solution[y]
def solve_part_i():
machines = read_file()
sum_tokens = 0
for machine in machines:
print(machine)
push_a, push_b = solve_machine_equation(machine)
print(push_a, ",", push_b)
if isinstance(push_a, Integer) and isinstance(push_b, Integer):
if int(push_a) <= 100 and int(push_b) <= 100:
sum_tokens += int(push_a) * 3 + int(push_b)
print(f"Part I: {sum_tokens}")
def solve_part_ii():
machines = read_file(part = 2)
sum_tokens = 0
for machine in machines:
print(machine)
push_a, push_b = solve_machine_equation(machine)
print(push_a, ",", push_b)
if isinstance(push_a, Integer) and isinstance(push_b, Integer):
sum_tokens += int(push_a) * 3 + int(push_b)
print(f"Part II: {sum_tokens}")
solve_part_ii()