-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.py
58 lines (44 loc) · 1.91 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
import sys, itertools, re
from copy import deepcopy
from typing import Dict, List, Optional, Set, Tuple
def floating_bit_poss(floating_mask: int, value:int) -> Set[int]:
if floating_mask == 0:
return {value}
for i in range(36):
bit_pos = 35 - i
if floating_mask & (1 << bit_pos):
value_hi = value | (1 << bit_pos)
value_lo = value &~ (1 << bit_pos)
return (floating_bit_poss(floating_mask &~ (1 << bit_pos),value_hi) |
floating_bit_poss(floating_mask &~ (1 << bit_pos), value_lo))
raise Exception("Reached impossible state.")
def main(args: List[str]) -> int:
registrar: Dict[int,int]= {}
with open(args[1], 'r') as f:
input = [ line.strip() for line in f ]
for line in input:
if "mask" in line:
_,_, mask_input_string = line.split()
print(mask_input_string)
ones_mask = 0b000000000000000000000000000000000000
#no more zero masks...
floating_mask = 0b000000000000000000000000000000000000
for i,c in enumerate(mask_input_string):
bit_pos = 35 - i
if c == '1':
ones_mask |= ( 1 << bit_pos )
elif c == 'X':
floating_mask |= ( 1 << bit_pos )
elif "mem" in line:
#convert the line into a list of locations and registrars
added_commands = re.fullmatch( r'mem\[([0-9]+)\] = ([0-9]+)', line )
key, value = int(added_commands.group(1)), int(added_commands.group(2))
addr = key | ones_mask
#now generate the remaining iterations:
for key in floating_bit_poss(floating_mask, addr):
registrar[key] = value
#value = value & zeros_mask
print(sum(registrar.values()))
return 0
if __name__ == '__main__':
sys.exit( main( sys.argv ) )