Skip to content

Instantly share code, notes, and snippets.

@carlwiedemann
Created December 4, 2023 05:35
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/bfb5fb229451c7cdf55a3f034bd009e4 to your computer and use it in GitHub Desktop.
Save carlwiedemann/bfb5fb229451c7cdf55a3f034bd009e4 to your computer and use it in GitHub Desktop.
Advent of Code 2023 day004.rb
require_relative "main"
module Day004
INPUT = File.read('INPUT.txt')
lines = INPUT.to_lines
result_counts = lines.map do |line|
data = line.split_strip(":").last
winning, mine = data.split_strip("|").map { _1.split_strip(" ").map(&:to_i) }
(mine & winning).count
end
##########
# Part 1 #
##########
answer1 = result_counts.reduce(0) do |memo, count|
memo + ((count > 0) ? 2**(count - 1) : 0)
end
pp answer1
##########
# Part 2 #
##########
counts = Array.new(result_counts.count, 1)
result_counts.each.with_index do |result_count, i|
counts[i].times do
result_count.times { |j| counts[1 + i + j] += 1 }
end
end
answer2 = counts.sum
pp answer2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment