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] ]
- Bag extend [
- removeAllOccurrences: key [ ^self contents removeKey: key ]
- keysAndValuesDo: aBlock [ ^self contents copy keysAndValuesDo: aBlock ]
- ]
- SequenceableCollection extend [
- indicesOf: obj [ ^((1 to: self size) select: [:i | (self at: i) = obj]) asSet ]
- ]
- "
- | Mainline
- "
- input := stdin lines contents asOrderedCollection.
- start := input removeFirst indexOf: $S.
- " Build table of splitters "
- table := (input collect: [:line | line indicesOf: $^]) reject: #isEmpty.
- " Process beams "
- part1 := 0.
- beams := Bag with: start.
- table do: [:row |
- beams keysAndValuesDo: [:beam :num |
- (row includes: beam) ifTrue: [
- part1 := part1 + 1.
- beams removeAllOccurrences: beam;
- add: beam + 1 withOccurrences: num;
- add: beam - 1 withOccurrences: num.
- ]
- ]
- ].
- ('Part 1: %1' % {part1}) displayNl.
- ('Part 2: %1' % {beams size}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment