musifter

AoC 2024 day 15, part 1 (smalltalk)

Dec 15th, 2024
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 2.36 KB | Source Code | 0 0
  1. #!/usr/local/bin/gst -q
  2.  
  3. Symbol extend [ value: arg [^arg perform: self] ]
  4.  
  5. Object subclass: TextGrid [
  6.     | grid dim robot |
  7.  
  8.     dirs := Dictionary from: {$< -> (-1@0). $^ -> (0@-1). $v -> (0@1). $> -> (1@0)}.
  9.  
  10.     TextGrid class >> new: mapArray [^super new init: mapArray]
  11.     init: mapArray [
  12.         " No sentinel needed, has boundary wall."
  13.         dim  := (mapArray first size @ mapArray size).
  14.         grid := mapArray join.
  15.  
  16.         robot := grid indexOfSubCollection: '@'.
  17.         grid at: robot put: $..
  18.         robot := self toCoord: robot.
  19.         ^self
  20.     ]
  21.  
  22.     " Access to grid via Points "
  23.     at: pt            [^grid at: (pt y * dim x) + pt x + 1]
  24.     at: pt put: chr   [^grid at: (pt y * dim x) + pt x + 1 put: chr]
  25.  
  26.     toCoord: idx      [^(idx - 1 \\ dim x) @ (idx - 1 // dim x)]
  27.     toIndex: pt       [^(pt y * dim x) + pt x + 1]
  28.  
  29.     " Basic access "
  30.     dirs        [^dirs]
  31.     dim         [^dim]
  32.     robot       [^robot]
  33.     robot: pt   [^robot := pt]
  34.  
  35.     " Return array of Points where aBlock on that character returns true "
  36.     selectPoints: aBlock [
  37.         | res |
  38.         res := OrderedCollection new.
  39.         grid keysAndValuesDo: [:idx :chr |
  40.             (aBlock value: chr) ifTrue: [res add: (self toCoord: idx)].
  41.         ].
  42.         ^res
  43.     ]
  44.  
  45.     " Print grid on stream "
  46.     printOn: aStream [
  47.         | robotIdx |
  48.         robotIdx := self toIndex: robot.
  49.         grid keysAndValuesDo: [:idx :chr |
  50.             aStream nextPut: (idx == robotIdx ifTrue: [$@] ifFalse: [chr]).
  51.             (idx \\ dim x = 0) ifTrue: [aStream nl].
  52.         ]
  53.     ]
  54. ]
  55.  
  56. "
  57. | Mainline
  58. "
  59. sections := (stdin contents tokenize: '\n\n') collect: #lines.
  60.  
  61. grid := TextGrid new: sections first.
  62.  
  63. sections second join do: [ :dchr |
  64.     dir  := grid dirs at: dchr.
  65.     move := grid robot + dir.
  66.     targ := grid at: move.
  67.  
  68.     (targ == $O) ifTrue: [
  69.         end := move.
  70.         [(grid at: end) == $O] whileTrue: [end := end + dir].
  71.  
  72.         ((grid at: end) == $.) ifTrue: [
  73.             grid at: end  put: $O.
  74.             grid at: move put: $..
  75.             grid robot: move.
  76.         ]
  77.  
  78.     ] ifFalse: [
  79.         (targ == $.) ifTrue: [grid robot: move]  "$# does noting"
  80.     ].
  81. ].
  82.  
  83. grid displayNl.
  84. part1 := (grid selectPoints: [:chr | chr == $O]) inject: 0 into: [:a :b| a + (100 * b y) + b x].
  85. ('Part 1: %1' % {part1}) displayNl.
Add Comment
Please, Sign In to add comment