Advertisement
musifter

AoC 2022, day 10 (smalltalk)

Dec 10th, 2022
2,762
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 1.05 KB | Source Code | 0 0
  1. #!/usr/local/bin/gst -q
  2.  
  3. Collection extend [
  4.     apply: method  [ ^self collect: [:x | x perform: method] ]
  5.     sum            [ ^self inject: 0 into: [ :a :b | a + b ] ]
  6. ]
  7.  
  8. "
  9. | Mainline
  10. "
  11. " Build a list of increment events to regX "
  12. events := stdin lines contents gather: [ :line |
  13.               " addx VALUE adds (0, VALUE), delaying by one cycle "
  14.               " noop adds (0), a cycle of adding nothing "
  15.               line substrings apply: #asNumber
  16.           ].
  17.  
  18. " Build table of the values of regX for each time "
  19. regX := OrderedCollection with: 1.
  20. events inject: 1 into: [:a :b | regX add: (a + b)].
  21.  
  22. " Part one is the sum of (time * regX) at these set times "
  23. 'Part 1: ' display.
  24. ((20 to: 220 by: 40) collect: [:time | time * (regX at: time)]) sum displayNl.
  25.  
  26. " Part 2 involves outputing a # pixel when regX is inside scan window "
  27. 'Part 2:' displayNl.
  28. regX keysAndValuesDo: [ :time :val |
  29.     (((time - 1) \\ 40 - val) abs <= 1 ifTrue: [$#] ifFalse: [$ ]) display.
  30.     (time \\ 40) = 0 ifTrue: [ stdout nl ].     " newline every 40 "
  31. ]
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement