diff --git a/days/day05.st b/days/day05.st index 902a861..88fa9ce 100644 --- a/days/day05.st +++ b/days/day05.st @@ -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 [ | beforeMap | initialize [beforeMap := Dictionary new] diff --git a/days/day08.st b/days/day08.st new file mode 100644 index 0000000..c035111 --- /dev/null +++ b/days/day08.st @@ -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. diff --git a/days/utils.st b/days/utils.st index 8112623..df8fd3a 100644 --- a/days/utils.st +++ b/days/utils.st @@ -255,6 +255,11 @@ Object subclass: Grid [ 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 [ ceilingDiv: denom [ ^((self - 1) // denom) + 1 ]