Skip to content

Instantly share code, notes, and snippets.

@henrik
Last active December 5, 2023 01:03
Show Gist options
  • Save henrik/5c96cbcbeef374917d824522b447a120 to your computer and use it in GitHub Desktop.
Save henrik/5c96cbcbeef374917d824522b447a120 to your computer and use it in GitHub Desktop.
Advent of Code day 4
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
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