Skip to content

Instantly share code, notes, and snippets.

@henrik
Last active December 2, 2023 14:20
Show Gist options
  • Save henrik/8ec463dc6395ad3b14ecef409bfd9f4f to your computer and use it in GitHub Desktop.
Save henrik/8ec463dc6395ad3b14ecef409bfd9f4f to your computer and use it in GitHub Desktop.
Advent of Code day 2
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
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
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