-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday14.rb
34 lines (32 loc) · 859 Bytes
/
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
input = File.read("day14_input.txt").lines
# Part 1
mask = "X"*36
mem = {}
input.each do |l|
if m = l.match(/([01X]{36})/)
mask = m[1]
elsif m = l.match(/(\d+)\D+(\d+)/)
val = mask.gsub(/X/, '0').to_i(2)
bit_mask = mask.gsub(/1/, '0').gsub(/X/, '1').to_i(2)
mem[m[1].to_i] = (m[2].to_i & bit_mask) | val
end
end
p mem.values.sum
# Part 2
mask = "X"*36
mem = {}
input.each do |l|
if m = l.match(/([01X]{36})/)
mask = m[1]
elsif m = l.match(/(\d+)\D+(\d+)/)
change_mask = mask.gsub(/1/, '0').gsub(/X/, '1').to_i(2)
or_mask = (mask.gsub(/X/, '0').to_i(2) | m[1].to_i) & ~change_mask
iter = change_mask
loop do
mem[iter | or_mask] = m[2].to_i
break if iter == 0
iter = (iter-1) & change_mask
end
end
end
p mem.values.sum