-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
part2.rb
56 lines (50 loc) 路 1.56 KB
/
part2.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
52
53
54
55
56
# frozen_string_literal: true
require_relative "part1"
require_relative "set"
module AdventOfCode
module Puzzles2023
module Day02
##
# Class for solving Day 02 - Part 2 puzzle
class Part2 < Part1
##
# Find the sum of the powers for the minimum valid sets.
#
# @return [Integer] sum of the powers
def answer
minimum_valid_sets.values.sum(&:power)
end
##
# Find the minimum valid set for each game.
#
# @return [Hash<Game, Set>] minimum valid sets
def minimum_valid_sets
@minimum_valid_sets ||= games.to_h { |game| [game, minimum_valid_set_for(game:)] }
end
protected
##
# Array of colors
# @return [Array<Symbol>] the colors of the cubes
COLORS = %i[blue green red].freeze
##
# Find the minimum valid set for a game.
# The minimum valid set is the set with the maximum number of
# blue, green, and red cubes for all sets.
#
# @param game [Game] game to find the minimum valid set for
#
# @return [Set] minimum valid set
def minimum_valid_set_for(game:)
# Find the maximum number of blue, green, and red cubes in each set
minimum_set = COLORS.to_h { |color| [color, 0] }
game.sets.each do |set|
COLORS.each do |color|
minimum_set[color] = [set.send(color), minimum_set[color]].max
end
end
Set.new(**minimum_set)
end
end
end
end
end