-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathday14.py
executable file
·79 lines (54 loc) · 1.28 KB
/
day14.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
#!/usr/bin/env python3
from utils.all import *
advent.setup(2019, 14)
fin = advent.get_input()
# eprint(*fin, sep='')
timer_start()
##################################################
lines = get_lines(fin)
produced = {}
needed = {}
left = defaultdict(int)
for l in lines:
a, b = l.split(' => ')
aa = a.split(', ')
b = b.split()
b_num, b_name = int(b[0]), b[1]
produced[b_name] = b_num
needed[b_name] = {}
for el in aa:
x = el.split()
a_num, a_name = int(x[0]), x[1]
needed[b_name][a_name] = a_num
left[a_name] += 1
from copy import deepcopy
def solve(wanted_fuel):
global left
want = defaultdict(int)
want['FUEL'] = wanted_fuel
_left = deepcopy(left)
_left['FUEL'] = 0
while 1:
goal = min(_left, key=_left.get)
assert _left[goal] == 0
del _left[goal]
if goal == 'ORE':
ans = want[goal]
break
wanted_qty = want[goal]
recipe_qty = produced[goal]
multiplier = wanted_qty // recipe_qty
if wanted_qty % recipe_qty != 0:
multiplier += 1
for req, nreq in needed[goal].items():
want[req] += nreq * multiplier
_left[req] -= 1
return ans
# print(solve(1))
ans = solve(1)
advent.submit_answer(1, ans)
# hehe, good ol' manual binary search
n = int(sys.argv[1])
s = solve(n)
print(s, 'delta', 1000000000000 - s)
# advent.submit_answer(2, ans2)