Last active
December 2, 2023 14:20
-
-
Save henrik/8ec463dc6395ad3b14ecef409bfd9f4f to your computer and use it in GitHub Desktop.
Advent of Code day 2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CONSTRAINTS = { red: 12, green: 13, blue: 14 } | |
puts DATA.readlines.sum { |line| | |
game, rounds = line.match(/Game (\d+): (.+)/).captures | |
rounds = rounds.split("; ").map { _1.scan(/(\d+) (\w+)/) } | |
next 0 unless rounds.all? { |round| round.all? { |count, color| count.to_i <= CONSTRAINTS[color.to_sym] } } | |
game.to_i | |
} | |
__END__ | |
Data goes here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
COLORS = %w[ red green blue ] | |
puts DATA.readlines.sum { |line| | |
_game, rounds = line.match(/Game (\d+): (.+)/).captures | |
rounds = rounds.split("; ").map { _1.scan(/(\d+) (\w+)/) } | |
COLORS.map { |color| rounds.filter_map { |round| round.find { _2 == color }&.first&.to_i }.max }.reduce(:*) | |
# Alt: | |
#COLORS.map { |color| rounds.flat_map { |round| round.filter_map { _1.to_i if _2 == color } }.max }.reduce(:*) | |
} | |
__END__ | |
Data goes here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts DATA.readlines.sum { |line| | |
line.scan(/(\d+) (\w+)/).group_by(&:last).map { _2.map { |n,| n.to_i }.max }.reduce(:*) | |
} | |
__END__ | |
Data goes here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment