Last active
December 5, 2023 01:03
-
-
Save henrik/5c96cbcbeef374917d824522b447a120 to your computer and use it in GitHub Desktop.
Advent of Code 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
puts DATA.readlines.sum { |line| | |
_, winning_numbers, my_numbers = line.split(/[:|]/).map(&:split) | |
wins = (winning_numbers & my_numbers).length | |
wins.zero? ? 0 : 2**(wins - 1) | |
} | |
__END__ | |
Data goes here |
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
lines = DATA.readlines | |
count_per_card = 1.upto(lines.length).to_h { [ _1, 1 ] } | |
lines.each.with_index(1) do |line, i| | |
_, winning_numbers, my_numbers = line.split(/[:|]/).map(&:split) | |
wins = (winning_numbers & my_numbers).length | |
count_per_card[i].times do | |
1.upto(wins) { count_per_card[i + _1] += 1 } | |
end | |
end | |
puts count_per_card.values.sum | |
__END__ | |
Data goes here |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment