Finished day 4
This commit is contained in:
1 parent
2e8a0c4284
commit
0be9447aa2
3 files changed
+68
-1
No files matched your search
@@ -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
|
||||
|
||||
+32
-1
@@ -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
|
||||
|
||||
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}"
|
||||
puts "Solution to part 2: #{cards.all_scratchcards.length}"
|
||||
end
|
||||
@@ -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
|
||||
|
||||
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!
|
||||
Reference in new issue
Block a user