Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/gst -q
- Collection extend [
- at: idx inc: val [ ^self at: idx put: val + (self at: idx) ]
- sum [ ^self inject: 0 into: [:a :b | a + b] ]
- ]
- " Convert input into a list of number of wins on each card: "
- cardWins := stdin lines contents collect: [ :line |
- parts := line subStrings: ':|'.
- wins := parts second subStrings asSet.
- parts third subStrings count: [:n | wins includes: n].
- ].
- " Note: using floor to handle the 0 wins case, as 2^-1 = 1/2 "
- part1 := (cardWins collect: [:n | (2 raisedTo: n - 1) floor]) sum.
- " Part 2: Start with one of each card, propagate wins forwards: "
- cards := Array new: cardWins size withAll: 1.
- cards keysAndValuesDo: [ :card :num |
- (card + 1 to: card + (cardWins at: card)) do: [:i | cards at: i inc: num].
- ].
- ('Part 1: %1' % {part1}) displayNl.
- ('Part 2: %1' % {cards sum}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement