diff --git a/src/advent/day3_test.rb b/src/advent/day3_test.rb index ac471d7..e196b16 100644 --- a/src/advent/day3_test.rb +++ b/src/advent/day3_test.rb @@ -33,6 +33,8 @@ class Day3Test < Test::Unit::TestCase assert_true 0 <= 10 and 10 < 100 + assert_equal [1, 2] + [3, 4], [1,2,3,4] + end diff --git a/src/advent/day4.rb b/src/advent/day4.rb index 069cb0e..bfb80d3 100644 --- a/src/advent/day4.rb +++ b/src/advent/day4.rb @@ -7,8 +7,12 @@ class Card @winning_numbers = winning_numbers end + def matching_number + @selected_numbers.filter {|s| @winning_numbers.index(s) != nil}.length + end + def score - n = @selected_numbers.filter {|s| @winning_numbers.index(s) != nil}.length + n = matching_number n == 0 ? 0 : 2 ** (n - 1) end @@ -29,6 +33,7 @@ end class Cards def initialize(cards) @cards = cards + @scratchcards_generated_by_card_cache = {} end def self.deserialize(ser) @@ -43,12 +48,38 @@ class Cards @cards.map {|card| card.score} 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 end -input_file_name = File.join(File.dirname(__FILE__), "day_4_input.txt") -input_content = File.open(input_file_name).read -cards = Cards.deserialize input_content +if __FILE__ == $0 + input_file_name = File.join(File.dirname(__FILE__), "day_4_input.txt") + input_content = File.open(input_file_name).read + cards = Cards.deserialize input_content -puts "Solution to part 1: #{cards.scores.sum}" \ No newline at end of file + puts "Solution to part 1: #{cards.scores.sum}" + puts "Solution to part 2: #{cards.all_scratchcards.length}" +end diff --git a/src/advent/day4_test.rb b/src/advent/day4_test.rb index 41a21c8..bd16f8a 100644 --- a/src/advent/day4_test.rb +++ b/src/advent/day4_test.rb @@ -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 cards = Cards.deserialize @@easy_input assert_equal 13, cards.scores.sum - end -end \ No newline at end of file + + 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 + +#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! \ No newline at end of file