Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/gst -q
- OrderedCollection extend [
- pairs [
- | res |
- res := OrderedCollection new.
- self allButFirst keysAndValuesDo: [:i :elem1 |
- res addAll: ((self first: i) collect: [:elem2 | {elem2. elem1}]).
- ].
- ^res
- ]
- ]
- Object subclass: AntennaMap [
- | antenna bounds |
- AntennaMap class >> new: mapArray [
- ^super new init: mapArray
- ]
- init: mapArray [
- bounds := Rectangle origin: (1@1) extent: (mapArray first size @ mapArray size).
- " Read array and build lists of antennae for each frequency: "
- antenna := Dictionary new.
- mapArray keysAndValuesDo: [:y :line |
- line keysAndValuesDo: [:x :chr |
- (chr ~= $.) ifTrue: [
- (antenna at: chr ifAbsentPut: [OrderedCollection new]) add: x@y
- ]
- ]
- ].
- ^self
- ]
- validPoint: pt [ ^bounds containsPoint: pt ]
- " Get harmonic multiple antinode of two antannae: "
- aOne: ant1 aTwo: ant2 multiple: mult [
- | delta |
- delta := ant2 - ant1.
- ^{ant1 - (delta * mult). ant2 + (delta * mult)} select: [:pt | self validPoint: pt]
- ]
- " Get all antinodes with a set harmonic multiple: "
- getAntinodes: mult [
- | anti |
- anti := Set new.
- antenna do: [:ant |
- ant pairs do: [ :pair |
- anti addAll: (self aOne: pair first aTwo: pair second multiple: mult)
- ]
- ].
- ^anti
- ]
- " Get all antinodes with all harmonic multiples: "
- getAntinodes [
- | anti |
- anti := Set new.
- antenna do: [:ant |
- ant pairs do: [ :pair |
- | mult harm |
- mult := 0.
- [
- harm := self aOne: pair first aTwo: pair second multiple: mult.
- mult := mult + 1.
- (anti addAll: harm) isEmpty. " Keep going until harm isEmpty "
- ] whileFalse.
- ]
- ].
- ^anti
- ]
- ]
- "
- | Mainline
- "
- map := AntennaMap new: stdin lines contents.
- ('Part 1: %1' % {(map getAntinodes: 1) size}) displayNl.
- ('Part 2: %1' % {map getAntinodes size}) displayNl.
Advertisement
Add Comment
Please, Sign In to add comment