Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/gst -q
- Collection extend [
- apply: method [ ^self collect: [:x | x perform: method] ]
- product [ ^self fold: [:a : b | a * b] ]
- at: idx inc: val [ ^self at: idx put: ((self at: idx ifAbsent: [0]) + val) ]
- ]
- String extend [
- asInterval [
- | range |
- range := (self substrings: $.) apply: #asInteger.
- ^Interval from: range first to: range second.
- ]
- ]
- Object subclass: Cuboid [
- | ranges str |
- Cuboid class >> new: rangeArr [
- ^super new init: rangeArr
- ]
- init: rangeArr [
- ranges := rangeArr.
- str := ranges gather: [:r | r first asString, '..', r last asString, ' '].
- ^self.
- ]
- intersect: aCub [
- | intersect |
- intersect := ranges with: aCub ranges collect: [ :r :s |
- (r first max: s first) to: (r last min: s last)
- ].
- (intersect conform: [:r | r notEmpty]) ifFalse: [ ^nil ].
- ^Cuboid new: intersect.
- ]
- " Need both of these for LookupTable key checks to work correctly "
- = aCub [ ^str = aCub string ]
- hash [ ^str hash ]
- string [ ^str ]
- ranges [ ^ranges ]
- volume [ ^(ranges apply: #size) product ]
- printOn: aStream [
- str printOn: aStream
- ]
- ]
- "
- | Mainline
- "
- input := stdin lines contents collect: [:line | line substrings: ' xyz=,'].
- count := 0.
- cuboids := LookupTable new.
- input do: [ :in |
- | newCube |
- stderr nextPutAll: ('Working: %1' % {count := count + 1}); cr; flush.
- newCube := Cuboid new: (in allButFirst apply: #asInterval).
- subCubes := LookupTable with: (newCube -> (in first = 'on') asCBooleanValue).
- cuboids keysAndValuesDo: [ :cub :weight |
- | int |
- (int := cub intersect: newCube) ifNotNil: [
- subCubes at: int inc: weight negated " toggling for overlap parities "
- ]
- ].
- " merge and prune "
- subCubes keysAndValuesDo: [ :sc :w |
- ((cuboids at: sc inc: w) = 0) ifTrue: [ cuboids removeKey: sc ].
- ]
- ].
- stdout nl.
- ('Cuboids: %1' % {cuboids size}) displayNl.
- part1 := 0.
- cuboids keysAndValuesDo: [ :c :w |
- part1 := part1 + (c volume * w).
- ].
- ('Part 2: %1' % {part1}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement