Skip to content

Commit d9b8f63

Browse files
committed
Day21 implemented
1 parent 2ecdac2 commit d9b8f63

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

day21.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
def shout(name, val, mb):
2+
if name == 'humn' and val >= 0:
3+
return val
4+
ops = mb[name]
5+
if len(ops) == 1:
6+
return int(ops[0])
7+
else:
8+
return eval(''.join(ops).replace(ops[0], str(shout(ops[0], val, mb)))
9+
.replace(ops[2], str(shout(ops[2], val, mb))))
10+
11+
12+
mnky_bussiness = {}
13+
14+
for line in open('inputs/day21.txt').readlines():
15+
words = line.split()
16+
name = words[0].strip(':')
17+
mnky_bussiness[name] = line.split(':')[1].split()
18+
19+
print('Part1:', shout('root', -1, mnky_bussiness))
20+
21+
mnky1 = mnky_bussiness['root'][0]
22+
mnky2 = mnky_bussiness['root'][2]
23+
24+
mnky1_score = shout(mnky1, -1, mnky_bussiness)
25+
mnky2_score = shout(mnky2, -1, mnky_bussiness)
26+
27+
target = 0
28+
if mnky1_score < mnky2_score:
29+
target = mnky1_score
30+
othr_mnky = mnky2
31+
else:
32+
target = mnky2_score
33+
othr_mnky = mnky1
34+
35+
bottom = 0
36+
top = 124765768589550 # my part1 solution -
37+
while bottom < top:
38+
mid = (bottom + top)//2
39+
score = target - shout(othr_mnky, mid, mnky_bussiness)
40+
if score < 0:
41+
bottom = mid
42+
elif score == 0:
43+
print('Part2:', mid)
44+
break
45+
else:
46+
top = mid

0 commit comments

Comments
 (0)