Skip to content

Instantly share code, notes, and snippets.

@carlwiedemann
Created December 3, 2023 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlwiedemann/88a75fc83b23c6f2686939a7eb99cf00 to your computer and use it in GitHub Desktop.
Save carlwiedemann/88a75fc83b23c6f2686939a7eb99cf00 to your computer and use it in GitHub Desktop.
Advent of Code 2023 day002.rb
require_relative "main"
module Day002
INPUT = File.read('INPUT.txt')
games = INPUT.to_lines.map do |line|
sets = line.split_strip(":").last.split_strip(";")
sets.map { |set| set.split_strip(",") }
end
##########
# Part 1 #
##########
MAX_RED = 12
MAX_GREEN = 13
MAX_BLUE = 14
total = 0
games.each.with_index do |sets, i|
invalid = false
sets.each do |set|
set.each do |pull|
qty = pull.to_i
invalid_blue = pull.include?("blue") && qty > MAX_BLUE
invalid_green = pull.include?("green") && qty > MAX_GREEN
invalid_red = pull.include?("red") && qty > MAX_RED
invalid = true if invalid_blue || invalid_green || invalid_red
end
end
total += i + 1 unless invalid
end
answer1 = total
pp answer1
##########
# Part 2 #
##########
total = 0
games.each do |sets|
min_red = 0
min_blue = 0
min_green = 0
sets.each do |set|
set.each do |pull|
qty = pull.to_i
min_blue = qty if pull.include?("blue") && qty > min_blue
min_green = qty if pull.include?("green") && qty > min_green
min_red = qty if pull.include?("red") && qty > min_red
end
end
total += min_red * min_green * min_blue
end
answer2 = total
pp answer2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment