musifter

AoC 2025 day 4, faster queue version (Smalltalk)

Dec 4th, 2025
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 1.45 KB | Source Code | 0 0
  1. #!/usr/local/bin/gst -q
  2.  
  3. Collection extend [
  4.     decAt: idx  [ ^self at: idx put: ((self at: idx) - 1) ]
  5. ]
  6.  
  7. "
  8. | Mainline
  9. "
  10. coordDirs := {(-1 @ -1). ( 0 @ -1). ( 1 @ -1).
  11.               (-1 @  0).            ( 1 @  0).
  12.               (-1 @  1). ( 0 @  1). ( 1 @  1)}.
  13.  
  14.  
  15. " Load input "
  16. input := stdin lines contents.
  17. width := input first size + 1.
  18. dirs  := coordDirs collect: [:c | c y * width + c x].
  19.  
  20. grid := Array new: ((input size + 2) * width + 1).
  21.  
  22. input keysAndValuesDo: [:y :line |
  23.     line keysAndValuesDo: [:x :char |
  24.         (char == $@) ifTrue: [ grid at: (y * width + x + 1) put: 0 ].
  25.     ].
  26. ].
  27.  
  28. " Calculate number of neighbours "
  29. rolls := (grid keys select: [:r | (grid at: r) notNil]) asSet.
  30.  
  31. rolls do: [:roll |
  32.     neigh := dirs collect: [:d | roll + d].
  33.     grid at: roll put: (neigh count: [:n | (grid at: n) notNil]).
  34. ].
  35.  
  36. " Get queue of removable rolls "
  37. removable := (rolls select: [:roll | (grid at: roll) < 4]) asOrderedCollection.
  38. ('Part 1: %1' % {removable size}) displayNl.
  39.  
  40. " Remove rolls one at a time, adding rolls when they become removable "
  41. part2 := 0.
  42. [ removable notEmpty ] whileTrue: [
  43.     next  := removable removeFirst.
  44.     part2 := part2 + 1.
  45.     neigh := (dirs collect: [:d | next + d]).
  46.  
  47.     rolls remove: next.
  48.     (neigh select: [:n | rolls includes: n]) do: [:n |
  49.         ((grid decAt: n) == 3) ifTrue: [
  50.             removable addLast: n.
  51.         ]
  52.     ]
  53. ].
  54.  
  55. ('Part 2: %1' % {part2}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment