-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13.py
81 lines (65 loc) · 1.78 KB
/
day13.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
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
81
import sys
from sympy import symbols, Eq, solve
inputfile = ""
if len(sys.argv) == 2:
inputfile = sys.argv[1] + ".txt"
else:
inputfile = "input" + sys.argv[0][3:5] + ".txt"
equations = []
with open(inputfile,"r") as input:
for line in input:
line = line.rstrip()
if line.startswith("Button"):
coords = line[10:].split(", ")
if line[7] == "A":
a_x = int(coords[0][2:])
a_y = int(coords[1][2:])
elif line[7] == "B":
b_x = int(coords[0][2:])
b_y = int(coords[1][2:])
else:
print("huh? "+line[7])
elif line.startswith("Prize"):
coords = line[7:].split(", ")
A, B = symbols('A B')
eq1 = A * a_x + B * b_x - int(coords[0][2:])
eq2 = A * a_y + B *b_y - int(coords[1][2:])
equations.append((eq1,eq2))
elif len(line) == 0:
continue
else:
print("wtf?")
def run_part1():
print(equations)
total = 0
for eqs in equations:
sols = solve(eqs, A, B, dict=True, rational=True)
if len(sols) > 1:
print("wow")
else:
for sol in sols:
if int(sol[A]) == sol[A] and int(sol[B]) == sol[B]:
if int(sol[A]) > 100 or int(sol[B]) > 100:
print("too many!")
break
else:
cost = int(sol[A]) * 3 + int(sol[B])
print(cost)
total += cost
print(total)
def run_part2():
total = 0
for eqs in equations:
new_eqs = (eqs[0] - 10000000000000, eqs[1] - 10000000000000)
sols = solve(new_eqs, A, B, dict=True, rational=True)
if len(sols) > 1:
print("wow")
else:
for sol in sols:
if int(sol[A]) == sol[A] and int(sol[B]) == sol[B]:
cost = int(sol[A]) * 3 + int(sol[B])
print(cost)
total += cost
print(total)
run_part1()
run_part2()