Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/gst -q
- Collection extend [
- apply: method [ ^self collect: [:x | x perform: method] ]
- sum [ ^self inject: 0 into: [ :a :b | a + b ] ]
- ]
- "
- | Mainline
- "
- " Build a list of increment events to regX "
- events := stdin lines contents gather: [ :line |
- " addx VALUE adds (0, VALUE), delaying by one cycle "
- " noop adds (0), a cycle of adding nothing "
- line substrings apply: #asNumber
- ].
- " Build table of the values of regX for each time "
- regX := OrderedCollection with: 1.
- events inject: 1 into: [:a :b | regX add: (a + b)].
- " Part one is the sum of (time * regX) at these set times "
- 'Part 1: ' display.
- ((20 to: 220 by: 40) collect: [:time | time * (regX at: time)]) sum displayNl.
- " Part 2 involves outputing a # pixel when regX is inside scan window "
- 'Part 2:' displayNl.
- regX keysAndValuesDo: [ :time :val |
- (((time - 1) \\ 40 - val) abs <= 1 ifTrue: [$#] ifFalse: [$ ]) display.
- (time \\ 40) = 0 ifTrue: [ stdout nl ]. " newline every 40 "
- ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement