Day 8
This commit is contained in:
parent
6b558d9330
commit
27f392f5c0
3 changed files with 43 additions and 5 deletions
|
|
@ -1,8 +1,3 @@
|
||||||
Dictionary extend [
|
|
||||||
at: key inA: Type [^self at: key ifAbsentPut: [Type new]]
|
|
||||||
at: key inA: Type add: elt [ (self at: key inA: Type) add: elt ]
|
|
||||||
]
|
|
||||||
|
|
||||||
Object subclass: Ordering [
|
Object subclass: Ordering [
|
||||||
| beforeMap |
|
| beforeMap |
|
||||||
initialize [beforeMap := Dictionary new]
|
initialize [beforeMap := Dictionary new]
|
||||||
|
|
|
||||||
38
days/day08.st
Normal file
38
days/day08.st
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
Grid subclass: AntennaGrid [
|
||||||
|
| antennae |
|
||||||
|
rows: r [ super rows: r. self indexAntennae ]
|
||||||
|
|
||||||
|
indexAntennae
|
||||||
|
[ antennae := Dictionary new.
|
||||||
|
self allPosnsDo: [
|
||||||
|
:pos | | c | c := self at: pos.
|
||||||
|
c = $. ifFalse: [ antennae at: c inA: Set add: pos ]
|
||||||
|
] ]
|
||||||
|
|
||||||
|
antinodesWithOffsets: offs
|
||||||
|
[ | set | set := Set new.
|
||||||
|
antennae do:
|
||||||
|
[ :posns | self putAntinodesBetween: posns into: set with: offs ].
|
||||||
|
^set ]
|
||||||
|
|
||||||
|
putAntinodesBetween: posns into: set with: offs
|
||||||
|
[ posns do: [
|
||||||
|
:p1 | posns do: [
|
||||||
|
:p2 | p1 = p2 ifFalse: [
|
||||||
|
self antinodeFrom: p1 to: p2 into: set with: offs.
|
||||||
|
self antinodeFrom: p2 to: p1 into: set with: offs.
|
||||||
|
]]]]
|
||||||
|
|
||||||
|
antinodeFrom: src to: dst into: set with: offs
|
||||||
|
[ | delta pos | delta := dst - src.
|
||||||
|
offs do: [
|
||||||
|
:off | pos := dst + (delta * off).
|
||||||
|
(self isInBounds: pos) ifFalse: [^self].
|
||||||
|
set add: pos ]]
|
||||||
|
]
|
||||||
|
|
||||||
|
AOC input: [ AntennaGrid new rows: stdin toLines asArray ];
|
||||||
|
part1: #(1);
|
||||||
|
part2: (0 to: 9999);
|
||||||
|
result: [ :grid :part | (grid antinodesWithOffsets: part) size ];
|
||||||
|
finish.
|
||||||
|
|
@ -255,6 +255,11 @@ Object subclass: Grid [
|
||||||
printOn: st [ rows do: [ :r | st << r; nl ] ]
|
printOn: st [ rows do: [ :r | st << r; nl ] ]
|
||||||
]
|
]
|
||||||
|
|
||||||
|
Dictionary extend [
|
||||||
|
at: key inA: Type [^self at: key ifAbsentPut: [Type new]]
|
||||||
|
at: key inA: Type add: elt [ (self at: key inA: Type) add: elt ]
|
||||||
|
]
|
||||||
|
|
||||||
Integer extend [
|
Integer extend [
|
||||||
ceilingDiv: denom [ ^((self - 1) // denom) + 1 ]
|
ceilingDiv: denom [ ^((self - 1) // denom) + 1 ]
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue