File tree Expand file tree Collapse file tree 2 files changed +30
-1
lines changed
Filter options
Expand file tree Collapse file tree 2 files changed +30
-1
lines changed
Original file line number Diff line number Diff line change 15
15
| [ 11] ( https://adventofcode.com/2020/day/11 ) | Seating System| [ py] ( /day11/main.py ) |
16
16
| [ 12] ( https://adventofcode.com/2020/day/12 ) | Rain Risk| [ py] ( /day12/main.py ) |
17
17
| [ 13] ( https://adventofcode.com/2020/day/13 ) | Shuttle Search| [ py] ( /day13/main.py ) |
18
- | [ 14] ( https://adventofcode.com/2020/day/14 ) | - | - |
18
+ | [ 14] ( https://adventofcode.com/2020/day/14 ) | Docking Data | [ py ] ( /day14/main.py ) |
19
19
| [ 15] ( https://adventofcode.com/2020/day/15 ) | -| -|
20
20
| [ 16] ( https://adventofcode.com/2020/day/16 ) | -| -|
21
21
| [ 17] ( https://adventofcode.com/2020/day/17 ) | -| -|
Original file line number Diff line number Diff line change
1
+ from itertools import product
2
+ from parse import search
3
+
4
+ def possible_addrs (mask , addr ):
5
+ mask2 = "" .join (v if m == "0" else m for m , v in zip (mask , f"{ addr :036b} " ))
6
+ res = []
7
+ for t in product ("01" , repeat = mask2 .count ("X" )):
8
+ it = iter (t )
9
+ res .append (int ("" .join (next (it ) if c == "X" else c for c in mask2 ), 2 ))
10
+ return res
11
+
12
+ with open ("input.txt" ) as f :
13
+ lines = [x .strip () for x in f ]
14
+
15
+ for is_part1 in [True , False ]:
16
+ mask = ""
17
+ mem = {}
18
+ for line in lines :
19
+ if line .startswith ("mask" ):
20
+ mask = line .split (" = " )[1 ]
21
+ else :
22
+ arg1 , arg2 = search ("mem[{:d}] = {:d}" , line ).fixed
23
+ if is_part1 :
24
+ mem [arg1 ] = int ("" .join (v if m == "X" else m for m , v in zip (mask , f"{ arg2 :036b} " )), 2 )
25
+ else :
26
+ for addr in possible_addrs (mask , arg1 ):
27
+ mem [addr ] = arg2
28
+
29
+ print (sum (mem .values ()))
You can’t perform that action at this time.
0 commit comments