musifter

AoC 2025 day 9 (Smalltalk)

Dec 9th, 2025
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Smalltalk 2.56 KB | Source Code | 0 0
  1. #!/usr/local/bin/gst -q
  2.  
  3. Symbol extend       [ value: arg [^arg perform: self]             ]
  4. Collection extend   [ max        [^self fold: [:a :b | a max: b]] ]
  5.  
  6. OrderedCollection extend [
  7.     pairs [
  8.         | res |
  9.         res := OrderedCollection new.
  10.         self keysAndValuesDo: [:i :elem1 |
  11.             res addAll: ((self first: i - 1) collect: [:elem2 | {elem2. elem1}]).
  12.         ].
  13.         ^res
  14.     ]
  15. ]
  16.  
  17. Point class extend  [ from: array  [^array first @ array second] ]
  18.  
  19. SequenceableCollection extend [
  20.     " Returns collection of results from calling binBlock for each link pair "
  21.     chain: binBlock [
  22.         | res |
  23.         res := self copyEmptyForCollect.
  24.         self fold: [:curr :next | res add: (binBlock value: curr value: next). next].
  25.         ^res
  26.     ]
  27. ]
  28.  
  29. Rectangle subclass: Box [
  30.     Box class >> origin: oPt corner: cPt [
  31.         ^self basicNew origin: oPt corner: cPt
  32.     ]
  33.  
  34.     Box class >> from: arr [
  35.         ^self basicNew origin: arr first corner: arr second
  36.     ]
  37.  
  38.     normalize [
  39.         ^Box origin: (self left min: self right) @ (self top min: self bottom)
  40.              corner: (self left max: self right) @ (self top max: self bottom)
  41.     ]
  42.  
  43.     area [ ^((self right - self left) abs + 1) * ((self top - self bottom) abs + 1) ]
  44.  
  45.     & other [
  46.         ^Box origin: (self left  max: other left)  @ (self top    max: other top)
  47.              corner: (self right min: other right) @ (self bottom min: other bottom)
  48.     ]
  49. ]
  50.  
  51. "
  52. | Mainline
  53. "
  54. ObjectMemory spaceGrowRate: 500.0.
  55.  
  56. points := (stdin lines contents collect: [:line |
  57.              Point from: ((line subStrings: $,) collect: #asNumber)
  58.           ]) asOrderedCollection.
  59.  
  60. lines := (points, {points first}) chain: [:pt1 :pt2 | (Box origin: pt1 corner: pt2) normalize].
  61. boxes := points pairs collect: [:pt | (Box origin: pt first corner: pt second) normalize].
  62. ('Part 1: %1' % {(boxes collect: #area) max}) displayNl.
  63.  
  64. part2 := 0.
  65. boxes do: [:box |
  66.     (part2 < box area) ifTrue: [
  67.         subBox := Box origin: (box left  + 1) @ (box top    + 1)
  68.                       corner: (box right - 1) @ (box bottom - 1).
  69.  
  70.         ((subBox left <= subBox right) and: [subBox top <= subBox bottom]) ifTrue: [
  71.             (lines allSatisfy: [:line |
  72.                | intersect |
  73.                 intersect := line & subBox.
  74.                 ((intersect right < intersect left) or: [intersect bottom < intersect top]).
  75.             ]) ifTrue: [
  76.                 part2 := (part2 max: box area) displayNl.
  77.             ]
  78.         ]
  79.     ]
  80. ].
  81.  
  82. ('Part 2: %1' % {part2}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment