-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd2.rb
46 lines (42 loc) · 1.22 KB
/
d2.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
def part1(file_name)
id_sum = 0
max_cubes = { red: 12, green: 13, blue:14 }
File.open(file_name, 'r').readlines.map do |line|
game_info, cube_sets = line.split(': ')
game_id = game_info.sub('Game ', '').to_i
game_possible = true
cube_sets.split('; ').map do |cube_set|
cube_set.split(', ').map do |subset|
str_count, str_color = subset.split(' ')
count = str_count.to_i
color = str_color.to_sym
game_possible = false if count.to_i > max_cubes[color]
end
end
id_sum += game_id if game_possible
end
id_sum
end
def part2(file_name)
power_sum = 0
File.open(file_name, 'r').readlines.map do |line|
min_cubes = { red: 0, green: 0, blue: 0 }
cube_sets = line.split(': ').last
cube_sets.split('; ').map do |cube_set|
cube_set.split(', ').map do |subset|
str_count, str_color = subset.split(' ')
count = str_count.to_i
color = str_color.to_sym
min_cubes[color] = [min_cubes[color], count].max
end
end
power_sum += min_cubes.values.reduce(1, :*)
end
power_sum
end
puts '== PART 1 =='
puts part1('test/d2.txt')
puts part1('input/d2.txt')
puts '== PART 2 =='
puts part2('test/d2.txt')
puts part2('input/d2.txt')