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 [ sum [^self inject: 0 into: [:a :b | a + b]] ]
- OrderedCollection extend [
- joltage: len [
- | queue |
- queue := self last: len.
- (self first: self size - len) reverse do: [:battery |
- | jolts |
- jolts := battery.
- " Cascade jolts down queue: "
- queue keysAndValuesDo: [:i :charge |
- (jolts >= charge)
- ifTrue: [ queue at: i put: jolts. jolts := charge ]
- ifFalse: [ jolts := 0 ].
- ]
- ].
- ^queue inject: 0 into: [:a :b | a * 10 + b]
- ]
- ]
- "
- | Mainline
- "
- input := stdin lines contents collect: [:line | line asOrderedCollection collect: #digitValue].
- {1 -> 2. 2 -> 12} do: [:part |
- ('Part %1: %2' % {part key. (input collect: [:l | l joltage: part value]) sum}) displayNl.
- ]
Advertisement
Add Comment
Please, Sign In to add comment