Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/gst -q
- Point extend [
- " Distance and moveDelta calcuations extenstions for this problem "
- distance: pt [ ^(self x - pt x) abs max: (self y - pt y) abs ]
- moveDelta: pt [ ^(pt x - self x) sign @ (pt y - self y) sign ]
- ]
- SequenceableCollection extend [
- " Returns collection of results from calling binBlock for each link pair, "
- " with 'item' added to the front. "
- inject: item intoChain: binBlock [
- | res |
- res := self copyEmptyForCollect.
- self inject: item into: [:prev :curr| res add: (binBlock value: prev value: curr). curr].
- ^res
- ]
- ]
- "
- | Mainline
- "
- dirs := Dictionary from: {'R' -> (0 @ 1). 'L' -> ( 0 @ -1).
- 'D' -> (1 @ 0). 'U' -> (-1 @ 0)}.
- rope := OrderedCollection from: ((1 to: 10) collect: [:x | (0 @ 0)]).
- trails := Array from: { Set with: (0 @ 0). Set with: (0 @ 0) }.
- stdin linesDo: [ :line |
- cmd := line substrings.
- cmd second asNumber timesRepeat: [
- rope at: 1 put: (rope first + (dirs at: cmd first)).
- rope := rope inject: (rope first) intoChain: [ :prev :curr |
- curr + ((prev distance: curr) > 1 ifTrue: [curr moveDelta: prev]
- ifFalse: [0 @ 0])
- ].
- trails first add: rope second.
- trails second add: rope last.
- ]
- ].
- ('Part 1: %1' % {trails first size}) displayNl.
- ('Part 2: %1' % {trails second size}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement