Advertisement
musifter

AoC 2022 day 17 (smalltalk pt 1)

Dec 17th, 2022
2,453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 2.47 KB | Source Code | 0 0
  1. #!/usr/local/bin/gst -q
  2.  
  3. Integer extend [
  4.     " Does number % N, but returns residue on interval of 1 to N, not 0 to N-1. "
  5.     %% modulus [ ^self - 1 \\ modulus + 1 ]
  6. ]
  7.  
  8. Object subclass: MoveStream [
  9.     | buff idx |
  10.  
  11.     dirs := Dictionary from: {$> -> (1 @ 0). $< -> (-1 @ 0)}.
  12.  
  13.     MoveStream class >> new: input [ ^super new init: input ]
  14.  
  15.     init: input  [ buff := input. idx := 0. ^self ]
  16.     next         [ ^dirs at: (buff at: (idx := (idx + 1) %% buff size)) ]
  17. ]
  18.  
  19. Object subclass: BlockStream [
  20.     | idx |
  21.     blocks := {{0@0. 1@0. 2@0. 3@0}.
  22.                {1@0. 0@1. 1@1. 2@1. 1@2}.
  23.                {0@0. 1@0. 2@0. 2@1. 2@2}.
  24.                {0@0. 0@1. 0@2. 0@3}.
  25.                {0@0. 1@0. 0@1. 1@1}}.
  26.  
  27.     BlockStream class >> new [ ^super new init ]
  28.  
  29.     init  [ idx := 0. ^self ]
  30.  
  31.     nextPos: pos [
  32.         idx := (idx + 1) %% blocks size.
  33.         ^(blocks at: idx) collect: [:sq | sq + pos].
  34.     ]
  35. ]
  36.  
  37. Object subclass: Shaft [
  38.     | grid maxHeight top |
  39.  
  40.     Shaft class >> new: height [ ^super new init: height ]
  41.  
  42.     init: height [
  43.         grid := Array new: (height * 7) withAll: $..
  44.         top := 0.
  45.         ^self
  46.     ]
  47.  
  48.     top  [ ^top ]
  49.  
  50.     at: pt  [ ^grid at: (pt y - 1) * 7 + pt x ]
  51.     at: pt put: chr [
  52.         top := top max: pt y.
  53.         ^grid at: (pt y - 1) * 7 + pt x put: chr
  54.     ]
  55.  
  56.     printOn: aStream [
  57.         (top to: 1 by: -1) do: [:y |
  58.             aStream nextPut: $|.
  59.             (1 to: 7) do: [:x |
  60.                 aStream nextPut: (self at: x @ y).
  61.             ].
  62.             aStream nextPut: $|; nl.
  63.         ].
  64.         aStream nextPutAll: '+-------+'; nl.
  65.     ]
  66. ]
  67.  
  68. "
  69. | Mainline
  70. "
  71. input  := MoveStream  new: stdin nextLine.
  72. blocks := BlockStream new.
  73. shaft  := Shaft new: 10000.
  74.  
  75. 2022 timesRepeat: [
  76.     dropped := true.
  77.  
  78.     " Get next block "
  79.     block := blocks nextPos: 3 @ (shaft top + 4).
  80.  
  81.     [ dropped ] whileTrue: [
  82.         " Try shifting byh move "
  83.         move := input next.
  84.         try := block collect: [:pt | pt + move].
  85.         (try conform: [:pt | (pt x between: 1 and: 7) and: [(shaft at: pt) = $.]]) ifTrue: [
  86.             block := try.
  87.         ].
  88.  
  89.         " Try dropping down "
  90.         try := block collect: [:pt | pt + (0 @ -1)].
  91.         (dropped := try conform: [:pt | (pt y > 0) and: [(shaft at: pt) = $.]]) ifTrue: [
  92.             block := try.
  93.         ].
  94.     ].
  95.  
  96.     " Place block "
  97.     block do: [:pt | shaft at: pt put: $#].
  98. ].
  99.  
  100. ('Part 1: %1' % {shaft top}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement