-
Notifications
You must be signed in to change notification settings - Fork 1
/
02-2.rb
executable file
·43 lines (37 loc) · 910 Bytes
/
02-2.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
#!/usr/bin/env ruby
lines = File.read('02.input').lines.map(&:strip)
line_format = /^Game (?<num>[[:digit:]]+): (?<reveals>.*)$/
games = {}
lines.each do |line|
m = line.match line_format
games[m['num'].to_i] = []
reveals = m['reveals'].split ';'
reveals.each do |reveal|
colours = {
'red' => 0,
'green' => 0,
'blue' => 0
}
tokens = reveal.delete(',').split
until tokens.empty?
num = tokens.shift.to_i
colour = tokens.shift
colours[colour] = num
end
games[m['num'].to_i] << colours
end
end
def power(game)
required = {
'red' => 0,
'green' => 0,
'blue' => 0
}
game.each do |reveal|
['red', 'green', 'blue'].each do |colour|
required[colour] = [required[colour], reveal[colour]].max
end
end
required['red'] * required['green'] * required['blue']
end
print games.values.map { |g| power(g) }.sum, "\n"