-
Notifications
You must be signed in to change notification settings - Fork 0
/
sol14.py
85 lines (58 loc) · 2.36 KB
/
sol14.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import utils
import re
import itertools
def initialise():
global instructions, exp
instructions = utils.loadInputFile("input_14.txt")
exp = re.compile("^mem\\[(\\d+)\\]\\s=\\s(\\d+)$")
def part1():
global instructions, exp
memory = {}
for instruction in instructions:
if instruction.startswith("mask"):
mask = instruction[7:]
xes = [idx for idx, char in enumerate(mask) if char == 'X']
ones = [idx for idx, char in enumerate(mask) if char == '1']
maskX = sum(1 << (35-x) for x in xes)
maskOne = sum(1 << (35-one) for one in ones)
else:
m = re.match(exp, instruction)
location = m.group(1)
value = int(m.group(2))
maskedValue = (value & maskX) | maskOne
memory[location] = maskedValue
return sum(memory.values())
def part2():
# this time we are altering the addresses not the values
global instructions
memory = {}
for instruction in instructions:
if instruction.startswith("mask"):
toAdd = []
mask = instruction[7:]
flippedMask = mask.replace("0", "1").replace("X", "0")
flippedXes = [idx for idx, char in enumerate(flippedMask) if char == '1']
xes = [idx for idx, char in enumerate(mask) if char == 'X']
ones = [idx for idx, char in enumerate(mask) if char == '1']
maskX = sum(1 << (35-x) for x in flippedXes)
maskOne = sum(1 << (35-one) for one in ones)
combinationSets = itertools.chain([itertools.combinations(xes, x) for x in range(len(xes)+1)])
for combinationSet in combinationSets:
for combination in combinationSet:
x = 0
for y in combination:
x = x | (1 << 35-y)
toAdd.append(x)
else:
m = re.match(exp, instruction)
location = int(m.group(1))
value = int(m.group(2))
# this will override all 1's in mask, leave everything else
# maskedKey = location | maskOne
maskedLocation = (location | maskOne) & maskX
for y in toAdd:
memory[maskedLocation + y] = value
return sum(memory.values())
initialise()
print(part1()) # 8566770985168
print(part2()) # 4832039794082