Created
December 4, 2023 05:35
-
-
Save carlwiedemann/bfb5fb229451c7cdf55a3f034bd009e4 to your computer and use it in GitHub Desktop.
Advent of Code 2023 day004.rb
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
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