Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/gst -q
- Symbol extend [ value: arg [^arg perform: self] ]
- Collection extend [ max [^self fold: [:a :b | a max: b]] ]
- OrderedCollection extend [
- pairs [
- | res |
- res := OrderedCollection new.
- self keysAndValuesDo: [:i :elem1 |
- res addAll: ((self first: i - 1) collect: [:elem2 | {elem2. elem1}]).
- ].
- ^res
- ]
- ]
- Point class extend [ from: array [^array first @ array second] ]
- SequenceableCollection extend [
- " Returns collection of results from calling binBlock for each link pair "
- chain: binBlock [
- | res |
- res := self copyEmptyForCollect.
- self fold: [:curr :next | res add: (binBlock value: curr value: next). next].
- ^res
- ]
- ]
- Rectangle subclass: Box [
- Box class >> origin: oPt corner: cPt [
- ^self basicNew origin: oPt corner: cPt
- ]
- Box class >> from: arr [
- ^self basicNew origin: arr first corner: arr second
- ]
- normalize [
- ^Box origin: (self left min: self right) @ (self top min: self bottom)
- corner: (self left max: self right) @ (self top max: self bottom)
- ]
- area [ ^((self right - self left) abs + 1) * ((self top - self bottom) abs + 1) ]
- & other [
- ^Box origin: (self left max: other left) @ (self top max: other top)
- corner: (self right min: other right) @ (self bottom min: other bottom)
- ]
- ]
- "
- | Mainline
- "
- ObjectMemory spaceGrowRate: 500.0.
- points := (stdin lines contents collect: [:line |
- Point from: ((line subStrings: $,) collect: #asNumber)
- ]) asOrderedCollection.
- lines := (points, {points first}) chain: [:pt1 :pt2 | (Box origin: pt1 corner: pt2) normalize].
- boxes := points pairs collect: [:pt | (Box origin: pt first corner: pt second) normalize].
- ('Part 1: %1' % {(boxes collect: #area) max}) displayNl.
- part2 := 0.
- boxes do: [:box |
- (part2 < box area) ifTrue: [
- subBox := Box origin: (box left + 1) @ (box top + 1)
- corner: (box right - 1) @ (box bottom - 1).
- ((subBox left <= subBox right) and: [subBox top <= subBox bottom]) ifTrue: [
- (lines allSatisfy: [:line |
- | intersect |
- intersect := line & subBox.
- ((intersect right < intersect left) or: [intersect bottom < intersect top]).
- ]) ifTrue: [
- part2 := (part2 max: box area) displayNl.
- ]
- ]
- ]
- ].
- ('Part 2: %1' % {part2}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment