musifter

AoC 2025 day 8 (Smalltalk)

Dec 8th, 2025 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 2.45 KB | Source Code | 0 0
  1. #!/usr/local/bin/gst -q
  2.  
  3. Symbol extend      [ value: arg [^arg perform: self] ]
  4. Collection extend  [ product    [^self inject: 1 into: [:a :b | a * b]] ]
  5.  
  6. SequenceableCollection extend [
  7.     " Return list of all pairs of elements in list "
  8.     pairs [
  9.         | res |
  10.         res := OrderedCollection new.
  11.         self allButFirst keysAndValuesDo: [:i :elem1 |
  12.             res addAll: ((self first: i) collect: [:elem2 | {elem2. elem1}]).
  13.         ].
  14.         ^res
  15.     ]
  16. ]
  17.  
  18. " Structure class to calculate and store Connection data "
  19. Object subclass: Connection [
  20.     | ends dist |
  21.  
  22.     Connection class >> new: endCoords [ ^super new init: endCoords ]
  23.     init: pts [
  24.         ends := pts.
  25.         dist := (pts first first - pts second first) squared
  26.                   + (pts first second - pts second second) squared
  27.                   + (pts first third - pts second third) squared.
  28.         ^self
  29.     ]
  30.  
  31.     <= other  [ ^dist <= other dist ]
  32.     ends      [ ^ends ]
  33.     dist      [ ^dist ]
  34.  
  35.     printOn: aStream [ aStream nextPutAll: dist asString ]
  36. ]
  37.  
  38. "
  39. | Mainline
  40. "
  41. input := stdin lines contents collect: [:line | (line subStrings: $,) collect: #asNumber].
  42.  
  43. partOneSize := (input size == 1000) ifTrue: [1000] ifFalse: [10].
  44.  
  45. " Build SortedCollection of Connections sorted by distance "
  46. distTable := SortedCollection sortBlock: [:a :b | b <= a].
  47. input pairs do: [:pair | distTable add: (Connection new: pair)].
  48.  
  49. " Build initial circuit graphs and backLookup vertex to graph table "
  50. circuits   := input collect: [:pt | Set with: pt].
  51. backLookup := LookupTable from: (circuits gather: [:graph | graph collect: [:pt | pt -> graph]]).
  52.  
  53. count := 0.
  54. [part2 isNil] whileTrue: [
  55.     | conx graphs |
  56.     conx   := distTable removeLast.
  57.     graphs := conx ends collect: [:end | backLookup at: end].
  58.  
  59.     (graphs first ~~ graphs second) ifTrue: [
  60.         " Merge smaller graph into larger: "
  61.         (graphs second size > graphs first size) ifTrue: [graphs swap: 1 with: 2].
  62.         graphs first addAll: graphs second.
  63.         graphs second do: [:pt | backLookup at: pt put: graphs first]; empty.
  64.  
  65.         (graphs first size == input size) ifTrue: [
  66.             part2 := (conx ends collect: #first) product.
  67.             ('Part 2: %1' % {part2}) displayNl.
  68.         ]
  69.     ].
  70.  
  71.     ((count := count + 1) == partOneSize) ifTrue: [
  72.         part1 := ((circuits collect: #size) sorted last: 3) product.
  73.         ('Part 1: %1' % {part1}) displayNl.
  74.     ]
  75. ]
Advertisement
Add Comment
Please, Sign In to add comment