Created
December 4, 2023 15:33
-
-
Save nickludlam/be7f56fcac79ecb372888166a2259441 to your computer and use it in GitHub Desktop.
Advent of Code 2023 - Day 4
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 "string_scanner" | |
# Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53 | |
puts "Let's gooooo!" | |
lines = [] of String # ooh fancy, types! | |
part_one_score = 0 | |
part_two_card_count = 0 | |
h = Hash(Int32, Int32).new(0) | |
STDIN.each_line do |line| | |
card = line.split(":") | |
num = card[0].gsub(/\D/, "").to_i | |
game_data = card[1].split("|") | |
winning_nums_set = game_data[0].scan(/\d+/).map() { |n| n[0].to_i }.to_set | |
my_nums = game_data[1].scan(/\d+/).map() { |n| n[0].to_i } | |
# Find the winning numbers by looping over my_nums and collecting those that are in the winning_nums_set | |
winning_nums = my_nums.select() { |n| winning_nums_set.includes?(n) } | |
card_count = 1 + h[num] | |
part_two_card_count += card_count | |
next if winning_nums.size == 0 | |
card_count.times() do | |
# Push the winning card counts ahead | |
winning_nums.each_with_index() do |n, i| | |
next_card_num = num + 1 + i | |
h[next_card_num] += 1 | |
end | |
end | |
# winning_num count must be > 0 so we can subtract one and raise 2 to that power | |
score = 2 ** (winning_nums.size - 1) | |
part_one_score += score | |
puts "#{num} (x#{card_count}) #{winning_nums_set.join(",")} ||| #{my_nums.join(":")} => #{winning_nums.join(",")} => #{score}" | |
end | |
puts "Part 1 score: #{part_one_score}" | |
puts "Part 2 total card count: #{part_two_card_count}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment