musifter

AoC 2025, day 3 (Smalltalk)

Dec 3rd, 2025 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 0.94 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. OrderedCollection extend [
  7.     joltage: len [
  8.         | queue |
  9.         queue := self last: len.
  10.         (self first: self size - len) reverse do: [:battery |
  11.            | jolts |
  12.             jolts := battery.
  13.  
  14.             " Cascade jolts down queue: "
  15.             queue keysAndValuesDo: [:i :charge |
  16.                 (jolts >= charge)
  17.                     ifTrue:  [ queue at: i put: jolts. jolts := charge ]
  18.                     ifFalse: [ jolts := 0 ].
  19.             ]
  20.         ].
  21.  
  22.         ^queue inject: 0 into: [:a :b | a * 10 + b]
  23.     ]
  24. ]
  25.  
  26.  
  27. "
  28. | Mainline
  29. "
  30. input := stdin lines contents collect: [:line | line asOrderedCollection collect: #digitValue].
  31.  
  32. {1 -> 2. 2 -> 12} do: [:part |
  33.     ('Part %1: %2' % {part key. (input collect: [:l | l joltage: part value]) sum}) displayNl.
  34. ]
Advertisement
Add Comment
Please, Sign In to add comment