Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/gst -q
- Point extend [
- gridDist: pt [
- ^((self x - pt x) abs + (self y - pt y) abs)
- ]
- rot90: n [
- ^n timesRepeat: [ self x: (self y negated) y: (self x) ]
- ]
- ]
- Object subclass: Boat [
- | pos way disp |
- Boat class >> new [
- ^(super new) init.
- ]
- init [
- pos := ( 0 @ 0).
- way := (10 @ 1).
- disp := LookupTable new.
- disp at: #N put: [ :m | way y: (way y + m) ];
- at: #S put: [ :m | way y: (way y - m) ];
- at: #E put: [ :m | way x: (way x + m) ];
- at: #W put: [ :m | way x: (way x - m) ];
- at: #L put: [ :m | way rot90: (m / 90) ];
- at: #R put: [ :m | way rot90: (m / 90) negated + 4 ];
- at: #F put: [ :m | pos := pos + (way * m) ].
- ]
- do: cmd with: mag [
- ^(disp at: cmd) value: mag.
- ]
- pos [ ^pos ]
- ]
- parse_re := '(\w)(\d+)' asRegex. " precompile regex for efficiency "
- boat := Boat new.
- stdin linesDo: [ :line |
- (line =~ parse_re) ifMatched: [ :results |
- cmd := (results at: 1) asSymbol.
- mag := (results at: 2) asNumber.
- boat do: cmd with: mag.
- ]
- ].
- stdout nextPutAll: ('Pos: ', boat pos printString); nl.
- stdout nextPutAll: ('Part 2: ', ((0@0) gridDist: boat pos) printString); nl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement