Finished day 4

This commit is contained in:
didier amyot committed 2023-12-04 16:25:38 -05:00
1 parent 2e8a0c4284
commit 0be9447aa2
3 files changed
+72 -5

No files matched your search

+2
View File
@@ -33,6 +33,8 @@ class Day3Test < Test::Unit::TestCase
assert_true 0 <= 10 and 10 < 100 assert_true 0 <= 10 and 10 < 100
assert_equal [1, 2] + [3, 4], [1,2,3,4]
end end
+36 -5
View File
@@ -7,8 +7,12 @@ class Card
@winning_numbers = winning_numbers @winning_numbers = winning_numbers
end end
def matching_number
@selected_numbers.filter {|s| @winning_numbers.index(s) != nil}.length
end
def score def score
n = @selected_numbers.filter {|s| @winning_numbers.index(s) != nil}.length n = matching_number
n == 0 ? 0 : 2 ** (n - 1) n == 0 ? 0 : 2 ** (n - 1)
end end
@@ -29,6 +33,7 @@ end
class Cards class Cards
def initialize(cards) def initialize(cards)
@cards = cards @cards = cards
@scratchcards_generated_by_card_cache = {}
end end
def self.deserialize(ser) def self.deserialize(ser)
@@ -43,12 +48,38 @@ class Cards
@cards.map {|card| card.score} @cards.map {|card| card.score}
end end
def scratchcards_generated_by_card(card)
if @scratchcards_generated_by_card_cache.has_key? card.id
return @scratchcards_generated_by_card_cache[card.id]
end
ret = [card]
score = card.matching_number
if score > 0
sub_cards = @cards.filter {|sub_card| sub_card.id > card.id and sub_card.id <= card.id + score }
ret += sub_cards
.map {|sub_card| scratchcards_generated_by_card(sub_card)}
.flatten
end
@scratchcards_generated_by_card_cache[card.id] = ret
ret
end
def all_scratchcards
ret = []
@cards.each { |card| ret += scratchcards_generated_by_card card }
ret
end
attr_accessor :cards attr_accessor :cards
end end
input_file_name = File.join(File.dirname(__FILE__), "day_4_input.txt") if __FILE__ == $0
input_content = File.open(input_file_name).read input_file_name = File.join(File.dirname(__FILE__), "day_4_input.txt")
cards = Cards.deserialize input_content input_content = File.open(input_file_name).read
cards = Cards.deserialize input_content
puts "Solution to part 1: #{cards.scores.sum}" puts "Solution to part 1: #{cards.scores.sum}"
puts "Solution to part 2: #{cards.all_scratchcards.length}"
end
+34
View File
@@ -49,6 +49,40 @@ Card 6: 31 18 13 56 72 | 74 77 10 23 35 67 36 11"
def test_day_4_part_1 def test_day_4_part_1
cards = Cards.deserialize @@easy_input cards = Cards.deserialize @@easy_input
assert_equal 13, cards.scores.sum assert_equal 13, cards.scores.sum
end
def test_scratchcards_generated_by_card
cards = Cards.deserialize @@easy_input
ret = cards.scratchcards_generated_by_card cards.cards[0]
expt = [1, 2, 3, 4, 5, 5, 4, 5, 3, 4, 5, 5, 4, 5, 5]
assert_equal expt, ret.map {|card| card.id}
end
def counter(lst)
ret = {}
lst.each do |e|
if ret.has_key? e
ret[e] += 1
else
ret[e] = 1
end
end
ret
end
def test_day_4_part_2
cards = Cards.deserialize @@easy_input
assert_equal( 30, cards.all_scratchcards.length)
end end
end end
#Once all of the originals and copies have been processed, you end up with
# 1 instance of card 1
# , 2 instances of card 2,
# 4 instances of card 3,
# 8 instances of card 4,
# 14 instances of card 5,
# and 1 instance of card 6.
#
#
# In total, this example pile of scratchcards causes you to ultimately have 30 scratchcards!