File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments