From bd41fa416952e7024fdba41f07b6f66c452e3ce2 Mon Sep 17 00:00:00 2001 From: AlexAegis Date: Mon, 11 Dec 2023 07:05:45 +0100 Subject: [PATCH] chore: cleaned up around 2023 day 11 --- solutions/typescript/2023/11/src/p1.ts | 42 ++++++++++++-------- solutions/typescript/2023/11/src/p2.ts | 24 ++---------- solutions/typescript/readme.md | 54 +++++++++++++------------- 3 files changed, 57 insertions(+), 63 deletions(-) diff --git a/solutions/typescript/2023/11/src/p1.ts b/solutions/typescript/2023/11/src/p1.ts index 510847195..ebfa38919 100644 --- a/solutions/typescript/2023/11/src/p1.ts +++ b/solutions/typescript/2023/11/src/p1.ts @@ -2,22 +2,32 @@ import { BoundingBox, task } from '@alexaegis/advent-of-code-lib'; import packageJson from '../package.json'; import { parse } from './parse.js'; -export const p1 = (input: string): number => { - const { galaxies, emptyRows, emptyColumns } = parse(input); +export const solver = + (expansion = 2) => + (input: string): number => { + const { galaxies, emptyRows, emptyColumns } = parse(input); - return galaxies - .pairs() - .map(([a, b]) => { - const nonExpandedDistance = a.manhattan(b); - const galaxyBox = BoundingBox.fromVectors([a, b]); - const emptyRowCount = emptyRows.filter((i) => galaxyBox.vertical.contains(i)).length; - const emptyColumnCount = emptyColumns.filter((i) => - galaxyBox.horizontal.contains(i), - ).length; + return galaxies + .pairs() + .map(([a, b]) => { + const nonExpandedDistance = a.manhattan(b); + const galaxyBox = BoundingBox.fromVectors([a, b]); + const emptyRowCount = emptyRows.filter((i) => + galaxyBox.vertical.contains(i), + ).length; + const emptyColumnCount = emptyColumns.filter((i) => + galaxyBox.horizontal.contains(i), + ).length; - return nonExpandedDistance + emptyRowCount + emptyColumnCount; - }) - .sum(); -}; + return ( + nonExpandedDistance + + emptyRowCount * (expansion - 1) + + emptyColumnCount * (expansion - 1) + ); + }) + .sum(); + }; -await task(p1, packageJson.aoc); // 10228230 ~0ms +export const p1 = (input: string): number => solver(2)(input); + +await task(p1, packageJson.aoc); // 10228230 ~104.54ms diff --git a/solutions/typescript/2023/11/src/p2.ts b/solutions/typescript/2023/11/src/p2.ts index 440d9048c..d287919a1 100644 --- a/solutions/typescript/2023/11/src/p2.ts +++ b/solutions/typescript/2023/11/src/p2.ts @@ -1,23 +1,7 @@ -import { BoundingBox, task } from '@alexaegis/advent-of-code-lib'; +import { task } from '@alexaegis/advent-of-code-lib'; import packageJson from '../package.json'; -import { parse } from './parse.js'; +import { solver } from './p1.js'; -export const p2 = (input: string): number => { - const { galaxies, emptyRows, emptyColumns } = parse(input); +export const p2 = (input: string): number => solver(1_000_000)(input); - return galaxies - .pairs() - .map(([a, b]) => { - const nonExpandedDistance = a.manhattan(b); - const galaxyBox = BoundingBox.fromVectors([a, b]); - const emptyRowCount = emptyRows.filter((i) => galaxyBox.vertical.contains(i)).length; - const emptyColumnCount = emptyColumns.filter((i) => - galaxyBox.horizontal.contains(i), - ).length; - - return nonExpandedDistance + emptyRowCount * 999_999 + emptyColumnCount * 999_999; - }) - .sum(); -}; - -await task(p2, packageJson.aoc); // 10228230 ~0ms +await task(p2, packageJson.aoc); // 10228230 ~104.48ms diff --git a/solutions/typescript/readme.md b/solutions/typescript/readme.md index 53ebbeea6..3a87968e8 100644 --- a/solutions/typescript/readme.md +++ b/solutions/typescript/readme.md @@ -6,33 +6,33 @@ -| Day | Part One | Part Two | -| ---------------------------------------- | -------------------------------------------------- | -------------------------------------------------- | -| [Day 1](/solutions/typescript/2023/01/) | [0.19ms](/solutions/typescript/2023/01/src/p1.ts) | [1.38ms](/solutions/typescript/2023/01/src/p2.ts) | -| [Day 2](/solutions/typescript/2023/02/) | [0.22ms](/solutions/typescript/2023/02/src/p1.ts) | [0.25ms](/solutions/typescript/2023/02/src/p2.ts) | -| [Day 3](/solutions/typescript/2023/03/) | [1.39ms](/solutions/typescript/2023/03/src/p1.ts) | [3.89ms](/solutions/typescript/2023/03/src/p2.ts) | -| [Day 4](/solutions/typescript/2023/04/) | [4.36ms](/solutions/typescript/2023/04/src/p1.ts) | [4.57ms](/solutions/typescript/2023/04/src/p2.ts) | -| [Day 5](/solutions/typescript/2023/05/) | [0.11ms](/solutions/typescript/2023/05/src/p1.ts) | [0.39ms](/solutions/typescript/2023/05/src/p2.ts) | -| [Day 6](/solutions/typescript/2023/06/) | [40μs](/solutions/typescript/2023/06/src/p1.ts) | [7μs](/solutions/typescript/2023/06/src/p2.ts) | -| [Day 7](/solutions/typescript/2023/07/) | [8.70ms](/solutions/typescript/2023/07/src/p1.ts) | [20.46ms](/solutions/typescript/2023/07/src/p2.ts) | -| [Day 8](/solutions/typescript/2023/08/) | [10.25ms](/solutions/typescript/2023/08/src/p1.ts) | [11.44ms](/solutions/typescript/2023/08/src/p2.ts) | -| [Day 9](/solutions/typescript/2023/09/) | [1.57ms](/solutions/typescript/2023/09/src/p1.ts) | [1.62ms](/solutions/typescript/2023/09/src/p2.ts) | -| [Day 10](/solutions/typescript/2023/10/) | [68.44ms](/solutions/typescript/2023/10/src/p1.ts) | [1.4s](/solutions/typescript/2023/10/src/p2.ts) | -| [Day 11](/solutions/typescript/2023/11/) | [0ms](/solutions/typescript/2023/11/src/p1.ts) | [0ms](/solutions/typescript/2023/11/src/p2.ts) | -| Day 12 | - | - | -| Day 13 | - | - | -| Day 14 | - | - | -| Day 15 | - | - | -| Day 16 | - | - | -| Day 17 | - | - | -| Day 18 | - | - | -| Day 19 | - | - | -| Day 20 | - | - | -| Day 21 | - | - | -| Day 22 | - | - | -| Day 23 | - | - | -| Day 24 | - | - | -| Day 25 | - | - | +| Day | Part One | Part Two | +| ---------------------------------------- | --------------------------------------------------- | --------------------------------------------------- | +| [Day 1](/solutions/typescript/2023/01/) | [0.19ms](/solutions/typescript/2023/01/src/p1.ts) | [1.38ms](/solutions/typescript/2023/01/src/p2.ts) | +| [Day 2](/solutions/typescript/2023/02/) | [0.22ms](/solutions/typescript/2023/02/src/p1.ts) | [0.25ms](/solutions/typescript/2023/02/src/p2.ts) | +| [Day 3](/solutions/typescript/2023/03/) | [1.39ms](/solutions/typescript/2023/03/src/p1.ts) | [3.89ms](/solutions/typescript/2023/03/src/p2.ts) | +| [Day 4](/solutions/typescript/2023/04/) | [4.36ms](/solutions/typescript/2023/04/src/p1.ts) | [4.57ms](/solutions/typescript/2023/04/src/p2.ts) | +| [Day 5](/solutions/typescript/2023/05/) | [0.11ms](/solutions/typescript/2023/05/src/p1.ts) | [0.39ms](/solutions/typescript/2023/05/src/p2.ts) | +| [Day 6](/solutions/typescript/2023/06/) | [40μs](/solutions/typescript/2023/06/src/p1.ts) | [7μs](/solutions/typescript/2023/06/src/p2.ts) | +| [Day 7](/solutions/typescript/2023/07/) | [8.70ms](/solutions/typescript/2023/07/src/p1.ts) | [20.46ms](/solutions/typescript/2023/07/src/p2.ts) | +| [Day 8](/solutions/typescript/2023/08/) | [10.25ms](/solutions/typescript/2023/08/src/p1.ts) | [11.44ms](/solutions/typescript/2023/08/src/p2.ts) | +| [Day 9](/solutions/typescript/2023/09/) | [1.57ms](/solutions/typescript/2023/09/src/p1.ts) | [1.62ms](/solutions/typescript/2023/09/src/p2.ts) | +| [Day 10](/solutions/typescript/2023/10/) | [68.44ms](/solutions/typescript/2023/10/src/p1.ts) | [1.4s](/solutions/typescript/2023/10/src/p2.ts) | +| [Day 11](/solutions/typescript/2023/11/) | [104.54ms](/solutions/typescript/2023/11/src/p1.ts) | [104.48ms](/solutions/typescript/2023/11/src/p2.ts) | +| Day 12 | - | - | +| Day 13 | - | - | +| Day 14 | - | - | +| Day 15 | - | - | +| Day 16 | - | - | +| Day 17 | - | - | +| Day 18 | - | - | +| Day 19 | - | - | +| Day 20 | - | - | +| Day 21 | - | - | +| Day 22 | - | - | +| Day 23 | - | - | +| Day 24 | - | - | +| Day 25 | - | - |