Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/gst -q
- " Class to handle position of sub for part 1 "
- Object subclass: Position [
- | horz vert |
- Position class >> new [
- ^(super new) init.
- ]
- init [
- horz := vert := 0.
- ^self
- ]
- forward: mag [ horz := horz + mag ]
- up: mag [ vert := vert - mag ]
- down: mag [ vert := vert + mag ]
- result [ ^horz * vert ]
- ]
- " Subclass of above to handle part 2 "
- Position subclass: AimPosition [
- | depth |
- AimPosition class >> new [
- ^(super new) init.
- ]
- init [
- super init.
- depth := 0. " vert is now aim, depth is the actual position "
- ^self
- ]
- forward: mag [ super forward: mag. depth := mag * vert + depth ]
- result: part [
- ^(part = 1) ifTrue: [super result] ifFalse: [horz * depth]
- ]
- ]
- "
- | Mainline
- "
- sub := AimPosition new.
- stdin linesDo: [ :line |
- " Parse command into keyword method symbol "
- cmd := (line substrings first, ':') asSymbol.
- mag := line substrings second asNumber.
- sub perform: cmd with: mag.
- ].
- 1 to: 2 do: [ :part |
- ('Part %1: %2' % {part. sub result: part}) displayNl.
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement