musifter

AoC 2025 day 4 (Smalltalk)

Dec 4th, 2025 (edited)
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 1.22 KB | Source Code | 0 0
  1. #!/usr/local/bin/gst -q
  2.  
  3. FileStream fileIn: './Automata.st'!
  4.  
  5. " Definition of a hexgrid space for the Automaton class "
  6. Space subclass: PaperGrid [
  7.     | dirs |
  8.     coordDirs := { (-1 @ -1). (0 @ -1). (1 @ -1).
  9.                    (-1 @  0).           (1 @  0).
  10.                    (-1 @  1). (0 @  1). (1 @  1) }.
  11.  
  12.     PaperGrid class >> load: input [
  13.         ^(super new) init: input
  14.     ]
  15.  
  16.     init: inputData [
  17.         | width |
  18.         width := inputData first size + 1.
  19.         dirs  := coordDirs collect: [:c | c y * width + c x].
  20.  
  21.         inputData keysAndValuesDo: [:y :line |
  22.             line keysAndValuesDo: [:x :char |
  23.                 (char = $@) ifTrue: [ self setAlive: (y * width + x) ]
  24.             ]
  25.         ].
  26.  
  27.         super initState.
  28.         ^self
  29.     ]
  30.  
  31.     neighbours: cell [
  32.         ^dirs collect: [ :d | cell + d ]
  33.     ]
  34. ]
  35.  
  36. "
  37. |  Mainline
  38. "
  39. auto := Automaton space:    (PaperGrid load: stdin lines contents)
  40.                   dieRule:  [:neigh | neigh < 4]
  41.                   liveRule: [:neigh | false    ].
  42.  
  43. startingRolls := auto space liveCells size.
  44.  
  45. ('Part 1: %1' % {startingRolls - (auto runTurns: 1)}) displayNl.
  46. ('Part 2: %1' % {startingRolls - (auto runUntilStable)}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment