blob: 6dfab221caf7bf0c97e4c1e67aa9cffb84cd61e2 (
plain)
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
|
lines = File.readlines('input.txt')
one_mask = 0
zero_mask = (1 << 36) - 1
mem = Hash.new 0
lines.each do |line|
if line.slice(0, 4) == 'mask'
mask = line.split(' = ')[1].strip
one_mask = 2
zero_mask = 3
mask.each_char do |c|
case c
when '1'
one_mask += 1
when '0'
zero_mask -= 1
end
one_mask *= 2
zero_mask *= 2
zero_mask += 1
end
one_mask >>= 1
zero_mask >>= 1
else
split = line.split(/\[|\] = /)
address = split[1].to_i
value = split[2].to_i
actual_value = (value & zero_mask) | one_mask
mem[address] = actual_value
end
end
sum = 0
mem.each_value do |v|
sum += v
sum -= 1 << 36
end
puts sum
|