musifter

AoC 2025 day 7 (Smalltalk)

Dec 7th, 2025 (edited)
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 1.02 KB | Source Code | 0 0
  1. #!/usr/local/bin/gst -q
  2.  
  3. Symbol extend [ value: arg [^arg perform: self] ]
  4.  
  5. Bag extend [
  6.     removeAllOccurrences: key  [ ^self contents removeKey: key ]
  7.     keysAndValuesDo: aBlock    [ ^self contents copy keysAndValuesDo: aBlock ]
  8. ]
  9.  
  10. SequenceableCollection extend [
  11.     indicesOf: obj [ ^((1 to: self size) select: [:i | (self at: i) = obj]) asSet ]
  12. ]
  13.  
  14. "
  15. | Mainline
  16. "
  17. input := stdin lines contents asOrderedCollection.
  18. start := input removeFirst indexOf: $S.
  19.  
  20. " Build table of splitters "
  21. table := (input collect: [:line | line indicesOf: $^]) reject: #isEmpty.
  22.  
  23. " Process beams "
  24. part1 := 0.
  25.  
  26. beams := Bag with: start.
  27. table do: [:row |
  28.     beams keysAndValuesDo: [:beam :num |
  29.         (row includes: beam) ifTrue: [
  30.             part1 := part1 + 1.
  31.             beams removeAllOccurrences: beam;
  32.                   add: beam + 1 withOccurrences: num;
  33.                   add: beam - 1 withOccurrences: num.
  34.         ]
  35.     ]
  36. ].
  37.  
  38. ('Part 1: %1' % {part1}) displayNl.
  39. ('Part 2: %1' % {beams size}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment