musifter

AoC 2025 day 11 (Smalltalk)

Dec 11th, 2025 (edited)
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 1.38 KB | Source Code | 0 0
  1. #!/usr/local/bin/gst -q
  2.  
  3. Symbol extend      [ value: arg  [^arg perform: self] ]
  4. Collection extend  [ sum         [^self inject: 0 into: [:a :b | a + b]] ]
  5.  
  6. Dictionary subclass: Graph [
  7.     <shape: #pointer>
  8.     | memo |
  9.  
  10.     " Returns list of number of paths through 0, 1, or 2 special (#fft or #dac) nodes "
  11.     pathsFrom: node [
  12.         memo ifNil: [ memo := Dictionary new ].
  13.  
  14.         ^memo at: node ifAbsentPut: [
  15.             (node == #out)
  16.                 ifTrue:  [OrderedCollection from: #(1 0 0)]
  17.                 ifFalse: [
  18.                     | res |
  19.                     res := OrderedCollection from: #(0 0 0).
  20.                     (self at: node) do: [:child |
  21.                         res := (self pathsFrom: child) with: res collect: [:a :b | a + b].
  22.                     ].
  23.  
  24.                     ((node == #fft) or: [node == #dac]) ifTrue: [
  25.                         res removeLast; addFirst: 0. " rotate list to shift values up "
  26.                     ].
  27.                     res
  28.                 ]
  29.         ]
  30.     ]
  31. ]
  32.  
  33. "
  34. | Mainline
  35. "
  36. graph := Graph from: (stdin lines contents collect: [:line | | words |
  37.              words := line substrings.
  38.              words first allButLast asSymbol -> (words allButFirst collect: #asSymbol)
  39.          ]).
  40.  
  41. ('Part 1: %1' % {(graph pathsFrom: #you) sum})   displayNl.
  42. ('Part 2: %1' % {(graph pathsFrom: #svr) third}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment