Advertisement
musifter

AoC 2021 day 13 (smalltalk)

Dec 13th, 2021
1,998
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/local/bin/gst -q
  2.  
  3. Collection extend [
  4.     apply: method  [ ^self collect: [:x | x perform: method] ]
  5. ]
  6.  
  7. "
  8. | Mainline
  9. "
  10. sections := stdin contents tokenize: '\n\n'.
  11.  
  12. " Turn list of points into Set of Points "
  13. points := Set from: (sections first lines collect: [ :line |
  14.               coord := (line substrings: $,) apply: #asNumber.
  15.               (coord first @ coord second)
  16.           ]).
  17.  
  18. " Turn folding rules into an Array of axis->line-num associations "
  19. folds := sections second lines collect: [ :line |
  20.             rule := (line substrings) third substrings: $=.
  21.             (rule first -> rule second asNumber)
  22.          ].
  23.  
  24. part1 := nil.
  25. extent := (0 @ 0).
  26. folds do: [ :fold |
  27.     " Make point methods to get and set the right coord for the fold "
  28.     axis := fold key asSymbol.
  29.     set  := (fold key, ':') asSymbol.
  30.  
  31.     " Adjust extent of visible rectangle "
  32.     extent perform: set with: fold value - 1.
  33.  
  34.     " Map points under transformation.  Set merges points as needed. "
  35.     points := points collect: [ :pt |
  36.                   ((pt perform: axis) > fold value) ifTrue: [
  37.                       pt perform: set with: (2 * fold value - (pt perform: axis)).
  38.                   ].
  39.                   pt
  40.               ].
  41.  
  42.     (part1) ifNil: [ part1 := points size ].  " Get first size for part 1 "
  43. ].
  44.  
  45. ('Part 1: %1' % {part1}) displayNl.
  46.  
  47. 'Part 2:' displayNl.
  48. (0 to: extent y) do: [ :y |
  49.     (0 to: extent x) do: [ :x |
  50.         stdout nextPut: ((points includes: x@y) ifTrue: [$#] ifFalse: [$ ]).
  51.     ].
  52.     stdout nl.
  53. ]
  54.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement