-
Notifications
You must be signed in to change notification settings - Fork 0
/
day14.rb
62 lines (49 loc) · 1.21 KB
/
day14.rb
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
file = File.open("input/day14.txt", "r")
lines = file.readlines
def floatingPositions(mask)
i = mask.index("X")
if i == nil
# puts mask
return [mask]
end
return floatingPositions(mask.sub("X", "1")) + floatingPositions(mask.sub("X", "0"))
end
memory1 = {}
memory2 = {}
raw_mask = ""
mask = [0, 0]
for i in 0..lines.length-1
line = lines[i]
if line.start_with?("mask")
raw_mask = line.split(" = ")[1].strip
ones = raw_mask.gsub("X", "0").to_i(2)
zeros = raw_mask.gsub("1", "X").gsub("0", "1").gsub("X", "0").to_i(2)
mask = [zeros, ones]
next
end
rx = /mem\[([0-9]+)\] = ([0-9]+)/
md = rx.match(lines[i])
pos = md[1].to_i
val = md[2].to_i
# part 1 #
maskedVal = ~(~(val | mask[1]) | mask[0])
memory1[pos] = maskedVal
##########
# part 2 #
tmp = (pos | ones).to_s(2)
while tmp.length < 36
tmp = "0" + tmp
end
for i in 0..raw_mask.length-1
if raw_mask[i] == "X"
tmp[i] = "X"
end
end
positions = floatingPositions(tmp)
for pstr in positions
memory2[pstr.to_i(2)] = val
end
##########
end
puts memory1.values.sum
puts memory2.values.sum