-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd15.rb
51 lines (44 loc) · 1.09 KB
/
d15.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
def compute_hash(s)
n = 0
s.each_char { |c| n = ((n + c.ord) * 17) & 0xff }
n
end
def solve(file_name)
steps = File.open(file_name, 'r').readline(chomp: true).split(',')
result1 = steps.map { |s| compute_hash(s) }.sum
puts("Part 1: #{result1}")
boxes = {}
256.times { |i| boxes[i] = { index: i, slots: [] } }
steps.each do |step|
n = step.index(/[-=]/)
label = step[0...n]
op = step[n]
value = step[(n + 1)..].to_i
hash = compute_hash(label)
slots = boxes[hash][:slots]
n = slots.index { |v| v[0] == label }
case op
when '-'
slots.delete_at(n) if n
when '='
if n
slots[n][1] = value
else
slots << [label, value]
end
end
end
result2 = 0
boxes.each_value do |box_contents|
box_number, slots = box_contents.values_at(:index, :slots)
slots.each_with_index do |lens, slot_number|
power = (1 + box_number) * (1 + slot_number) * lens[1]
result2 += power
end
end
puts("Part 2: #{result2}")
end
puts("== TEST ==")
solve('test/d15.txt')
puts("== INPUT ==")
solve('input/d15.txt')