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 ]
- value: arg1 value: arg2 [ ^arg1 perform: self with: arg2 ]
- ]
- Collection extend [ sum [ ^self inject: 0 into: [:a :b | a + b] ] ]
- SequenceableCollection extend [
- transpose [
- ^(1 to: self first size) collect: [:col | (self collect: [:row | row at: col])].
- ]
- ]
- "
- | Mainline
- "
- input := stdin lines contents asOrderedCollection.
- ops := input removeLast subStrings collect: #asSymbol.
- " Part 1 numbers (read horizontal and transpose by numbers in table): "
- horz := (input collect: [:row | row subStrings collect: #asNumber]) transpose.
- " Part 2 numbers (transpose by col and then split on delimiting spaces (0)): "
- colNums := input transpose collect: [:charArray |
- (charArray select: #isDigit) asString asNumber
- ].
- vert := (ReadStream on: colNums) splitAt: 0.
- " Calculate values and output: "
- #(1 2) with: {horz. vert} do: [:part :table |
- ('Part %1: %2' % {
- part.
- (table with: ops collect: #fold:) sum
- }) displayNl.
- ]
Advertisement
Add Comment
Please, Sign In to add comment