Advertisement
musifter

AoC day 24, Smalltalk

Dec 24th, 2020
2,413
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/local/bin/gst -q
  2.  
  3. LookupTable extend [
  4.     incAt: key [
  5.         ^self at: key put: (self at: key ifAbsent: [0]) + 1.
  6.     ]
  7. ]
  8.  
  9. Object subclass: HexGrid [
  10.     | hexgrid black_tiles |
  11.  
  12.     HexGrid class >> new [
  13.         Dirs := LookupTable new.
  14.         Dirs at:  #e put: ( 0 @  1); at: #ne put: ( 1 @  0); at: #nw put: ( 1 @ -1);
  15.              at:  #w put: ( 0 @ -1); at: #sw put: (-1 @  0); at: #se put: (-1 @  1).
  16.  
  17.         ^(super new) init
  18.     ]
  19.  
  20.     HexGrid class >> neighbours: cell [
  21.         ^Dirs collect: [ :d | cell + d ]
  22.     ]
  23.  
  24.     HexGrid class >> dirs [
  25.         ^Dirs
  26.     ]
  27.  
  28.     init [
  29.         hexgrid := LookupTable new.
  30.         black_tiles := OrderedCollection new.
  31.         ^self
  32.     ]
  33.  
  34.     flip: cell [
  35.         | hash |
  36.         hexgrid at: cell put: (hexgrid at: cell ifAbsentPut: [false]) not.
  37.     ]
  38.  
  39.     count_flipped [
  40.         hexgrid keysDo: [ :cell | (hexgrid at: cell) ifTrue: [ black_tiles add: cell ] ].
  41.         ^black_tiles size.
  42.     ]
  43.  
  44.     run_life: turns [
  45.         | black_adj count next |
  46.         black_adj := LookupTable new.
  47.         black_tiles do: [ :cell |
  48.             black_adj at: cell ifAbsentPut: [0].
  49.             (HexGrid neighbours: cell) do: [ :neigh | black_adj incAt: neigh ]
  50.         ].
  51.  
  52.         count := black_tiles size.
  53.         (1 to: turns) do: [ :time |
  54.             next := LookupTable new.
  55.  
  56.             black_adj associationsDo: [ :tile |
  57.                 (hexgrid at: tile key ifAbsentPut: [false]) ifTrue: [
  58.                     " black case "
  59.                     ((tile value = 0) or: [tile value > 2]) ifTrue: [
  60.                         self flip: tile key.
  61.                         count := count - 1.
  62.                     ]
  63.                 ] ifFalse: [
  64.                     " white case "
  65.                     (tile value = 2) ifTrue: [
  66.                         self flip: tile key.
  67.                         count := count + 1.
  68.                     ]
  69.                 ].
  70.  
  71.                 (hexgrid at: tile key) ifTrue: [
  72.                     next at: tile key ifAbsentPut: [0].
  73.                     (HexGrid neighbours: tile key) do: [ :neigh | next incAt: neigh ]
  74.                 ]
  75.             ].
  76.  
  77.             stdout nextPutAll: ('Time ', time asString, ' = ', count asString, ' '); cr; flush.
  78.             black_adj := next.
  79.         ].
  80.         stdout nl.
  81.     ]
  82. ]
  83.  
  84. "
  85. |  Mainline
  86. "
  87. grid := HexGrid new.
  88. stdin linesDo: [ :line |
  89.     hex := (0 @ 0).
  90.     ns_seen := ''.
  91.     line do: [ :chr |
  92.         ((chr = $n) or: [chr = $s]) ifTrue: [
  93.             ns_seen := chr asString.
  94.         ] ifFalse: [
  95.             hex := hex + (HexGrid dirs at: (ns_seen, chr asString) asSymbol).
  96.             ns_seen := ''.
  97.         ].
  98.     ].
  99.  
  100.     grid flip: hex.
  101. ].
  102.  
  103. stdout nextPutAll: ('Part 1: ', grid count_flipped asString); nl.
  104. grid run_life: 100.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement