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)
- ]
- ]
- Object subclass: Boat [
- | pos face widder disp |
- Boat class >> new [
- ^(super new) init.
- ]
- init [
- pos := (0 @ 0).
- face := 0. " facing, add 1 before using on array "
- widder := #( #E #N #W #S ). " directions in widdershin order "
- disp := LookupTable new.
- disp at: #N put: [ :m | pos y: (pos y + m) ];
- at: #S put: [ :m | pos y: (pos y - m) ];
- at: #E put: [ :m | pos x: (pos x + m) ];
- at: #W put: [ :m | pos x: (pos x - m) ];
- at: #L put: [ :m | face := (face + (m / 90)) \\ 4 ];
- at: #R put: [ :m | face := (face - (m / 90)) \\ 4 ];
- at: #F put: [ :m | (disp at: (widder at: (face+1))) value: 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 1: ', ((0@0) gridDist: boat pos) printString); nl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement