Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/gst -q
- Symbol extend [ value: arg [^arg perform: self] ]
- Collection extend [ product [^self inject: 1 into: [:a :b | a * b]] ]
- Array extend [
- countWins [
- | time dist discrim upper lower |
- " ASSUME: size of array is at least 2 "
- time := self first.
- dist := self second + 1. " Need at least one more for the win "
- discrim := (time * time - (4 * dist)) sqrt.
- upper := ((time + discrim) / 2) floor.
- lower := ((time - discrim) / 2) ceiling.
- ^upper - lower + 1
- ]
- ]
- "
- | Mainline
- "
- input := stdin lines contents collect: [:line | line subStrings allButFirst].
- " Parse input into pairs of values for part one "
- table := input collect: [:line | line collect: #asNumber].
- cols := (table first) with: (table second).
- " Join for part 2 "
- concat := input collect: [:line | line join asNumber].
- ('Part 1: %1' % {(cols collect: #countWins) product}) displayNl.
- ('Part 2: %1' % {concat countWins}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement