Advertisement
musifter

AoC day 11 (pt1), Smalltalk

Dec 11th, 2020
2,071
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/local/bin/gst -q
  2.  
  3. "
  4. |  Class for handling a 2D array Grid
  5. "
  6. Object subclass: Grid [
  7.     | dim_x dim_y rows changes |
  8.  
  9.     Grid class >> new [
  10.         ^(super new) init
  11.     ]
  12.  
  13.     init [
  14.         changes := OrderedCollection new.
  15.         rows    := OrderedCollection new.
  16.         dim_x   := 0.
  17.         dim_y   := 0.
  18.         ^self
  19.     ]
  20.  
  21.     " loading method "
  22.     addRow: str [
  23.         rows addLast: str.
  24.         dim_y := dim_y + 1.
  25.         (dim_x = 0) ifTrue: [ dim_x := str size ].
  26.         ^self
  27.     ]
  28.  
  29.     " access methods "
  30.     changes [ ^changes size ]
  31.  
  32.     at: pt [
  33.         ^(rows at: pt y) at: pt x.
  34.     ]
  35.  
  36.     at: pt put: val [
  37.         ^(rows at: pt y) at: pt x put: val.
  38.     ]
  39.  
  40.     " iterator methods "
  41.     do: aBlock [
  42.         rows do: [ :y |
  43.             y do: [ :x |
  44.                 aBlock value: x
  45.             ]
  46.         ]
  47.     ]
  48.  
  49.     doPoint: aBlock [
  50.         (1 to: dim_y) do: [ :y |
  51.             (1 to: dim_x) do: [ :x |
  52.                 aBlock value: (x @ y)
  53.             ]
  54.         ]
  55.     ]
  56.  
  57.     inject: val into: aBlock [
  58.         | ret |
  59.         ret := val.
  60.         self do: [ :b | ret := aBlock value: ret value: b ].
  61.         ^ret
  62.     ]
  63.  
  64.     from: pt1 to: pt2 inject: val into: aBlock [
  65.         | ret |
  66.         ret := val.
  67.         (pt1 y to: pt2 y) do: [ :y |
  68.             (pt1 x to: pt2 x) do: [ :x |
  69.                 ret := aBlock value: ret value: (self at: (x @ y)).
  70.             ]
  71.         ].
  72.         ^ret
  73.     ]
  74.  
  75.     neighbours: pt inject: val into: aBlock [
  76.         | pt1 pt2 |
  77.         pt1 := ((pt x - 1) max: 1) @ ((pt y - 1) max: 1).
  78.         pt2 := ((pt x + 1) min: dim_x) @ ((pt y + 1) min: dim_y).
  79.  
  80.         ^self from: pt1 to: pt2 inject: val into: aBlock
  81.     ]
  82.  
  83.     " methods for marking changes and applying them "
  84.     mark: pt put: val [
  85.         changes add: (Array with: pt with: val).
  86.     ]
  87.  
  88.     applyChanges [
  89.         changes do: [ :ch |
  90.             self at: ch first put: ch second.
  91.         ].
  92.         changes empty.
  93.     ]
  94.  
  95.     " output method "
  96.     printOn: stream [
  97.         rows do: [ :line |
  98.             stream nextPutAll: line; nl.
  99.         ]
  100.     ]
  101. ]
  102.  
  103. "
  104. |  Mainline
  105. "
  106. grid := Grid new.
  107. stdin linesDo: [ :line |
  108.     grid addRow: line.
  109. ].
  110.  
  111. grid printNl.
  112.  
  113. gen := 1.
  114. [ (grid changes > 0) or: [ gen = 1 ] ] whileTrue: [
  115.     stdout nextPutAll: 'Generation: ', gen asString,
  116.                        '  Changes: ',  grid changes asString, '  ';
  117.                        cr; flush.
  118.  
  119.     grid applyChanges.
  120.  
  121.     gen := gen + 1.
  122.     grid doPoint: [ :pt |
  123.         occ := grid neighbours: pt
  124.                     inject: 0
  125.                     into: [:a :b | a + ((b == $#) asCBooleanValue)].
  126.  
  127.         cell := grid at: pt.
  128.  
  129.         ((cell == $#) and: [occ >= 5]) ifTrue: [ grid mark: pt put: $L ].
  130.         ((cell == $L) and: [occ = 0])  ifTrue: [ grid mark: pt put: $# ].
  131.     ].
  132. ].
  133.  
  134. grid printNl.
  135. part1 := grid inject: 0 into: [ :a :b | a + ((b == $#) asCBooleanValue) ].
  136.  
  137. stdout nl; nextPutAll: 'Part 1: ', part1 asString; nl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement