File tree 2 files changed +58
-1
lines changed
Filter options
2 files changed +58
-1
lines changed
Original file line number Diff line number Diff line change 23
23
| [ 19] ( https://adventofcode.com/2020/day/19 ) | Monster Messages| [ py] ( /day19/main.py ) , [ alt] ( /day19/alt.py ) |
24
24
| [ 20] ( https://adventofcode.com/2020/day/20 ) | Jurassic Jigsaw| [ py] ( /day20/main.py ) |
25
25
| [ 21] ( https://adventofcode.com/2020/day/21 ) | Allergen Assessment| [ py] ( /day21/main.py ) |
26
- | [ 22] ( https://adventofcode.com/2020/day/22 ) | - | - |
26
+ | [ 22] ( https://adventofcode.com/2020/day/22 ) | Crab Combat | [ py ] ( /day22/main.py ) |
27
27
| [ 23] ( https://adventofcode.com/2020/day/23 ) | -| -|
28
28
| [ 24] ( https://adventofcode.com/2020/day/24 ) | -| -|
29
29
| [ 25] ( https://adventofcode.com/2020/day/25 ) | -| -|
Original file line number Diff line number Diff line change
1
+
2
+ with open ("input.txt" ) as f :
3
+ chunks = f .read ().split ("\n \n " )
4
+
5
+ orig_p1 = list (map (int , chunks [0 ].strip ().split ("\n " )[1 :]))
6
+ orig_p2 = list (map (int , chunks [1 ].strip ().split ("\n " )[1 :]))
7
+
8
+ p1 = orig_p1 .copy ()
9
+ p2 = orig_p2 .copy ()
10
+
11
+ while len (p1 ) > 0 and len (p2 ) > 0 :
12
+ a , b = p1 .pop (0 ), p2 .pop (0 )
13
+ if a > b :
14
+ p1 += [a ,b ]
15
+ else :
16
+ p2 += [b ,a ]
17
+
18
+ if len (p1 ) > 0 :
19
+ winner = p1
20
+ else :
21
+ winner = p2
22
+
23
+ print (sum (e * (len (winner )- i ) for i , e in enumerate (winner )))
24
+
25
+ # Part 2
26
+ def play_game (p1 , p2 ):
27
+ seen1 , seen2 = set (), set ()
28
+
29
+ while len (p1 ) > 0 and len (p2 ) > 0 :
30
+ s1 = "," .join ([str (c ) for c in p1 ])
31
+ s2 = "," .join ([str (c ) for c in p2 ])
32
+ if s1 in seen1 or s2 in seen2 :
33
+ return "p1" , p1
34
+ seen1 .add (s1 )
35
+ seen2 .add (s2 )
36
+
37
+ a , b = p1 .pop (0 ), p2 .pop (0 )
38
+ if a <= len (p1 ) and b <= len (p2 ):
39
+ winner , _ = play_game (p1 .copy ()[:a ], p2 .copy ()[:b ])
40
+ else :
41
+ if a > b :
42
+ winner = "p1"
43
+ else :
44
+ winner = "p2"
45
+
46
+ if winner == "p1" :
47
+ p1 += [a ,b ]
48
+ else :
49
+ p2 += [b ,a ]
50
+
51
+ if len (p1 ) > 0 :
52
+ return "p1" , p1
53
+ else :
54
+ return "p2" , p2
55
+
56
+ _ , w = play_game (orig_p1 .copy (), orig_p2 .copy ())
57
+ print (sum (e * (len (w )- i ) for i , e in enumerate (w )))
You can’t perform that action at this time.
0 commit comments