From 5d92adef1ba52a8347b83e0e06ed05286988673d Mon Sep 17 00:00:00 2001 From: AlexAegis Date: Wed, 6 Dec 2023 08:30:43 +0100 Subject: [PATCH] feat: solved 2023 day 6 --- resources/2023/06/example.1.txt | 2 + resources/2023/06/input.txt | 2 + solutions/typescript/2023/06/package.json | 5 --- solutions/typescript/2023/06/readme.md | 2 +- solutions/typescript/2023/06/src/p1.spec.ts | 4 +- solutions/typescript/2023/06/src/p1.ts | 25 ++++++++++-- solutions/typescript/2023/06/src/p2.spec.ts | 4 +- solutions/typescript/2023/06/src/p2.ts | 11 +++-- solutions/typescript/2023/06/src/parse.ts | 40 ++++++++++++++++++- .../libs/lib/src/math/common/index.ts | 1 + .../math/common/quadartic-formula.function.ts | 4 ++ solutions/typescript/readme.md | 2 +- 12 files changed, 81 insertions(+), 21 deletions(-) create mode 100644 resources/2023/06/example.1.txt create mode 100644 resources/2023/06/input.txt create mode 100644 solutions/typescript/libs/lib/src/math/common/quadartic-formula.function.ts diff --git a/resources/2023/06/example.1.txt b/resources/2023/06/example.1.txt new file mode 100644 index 000000000..28f5ae955 --- /dev/null +++ b/resources/2023/06/example.1.txt @@ -0,0 +1,2 @@ +Time: 7 15 30 +Distance: 9 40 200 diff --git a/resources/2023/06/input.txt b/resources/2023/06/input.txt new file mode 100644 index 000000000..d3192a6f3 --- /dev/null +++ b/resources/2023/06/input.txt @@ -0,0 +1,2 @@ +Time: 57 72 69 92 +Distance: 291 1172 1176 2026 diff --git a/solutions/typescript/2023/06/package.json b/solutions/typescript/2023/06/package.json index 17ffd8e5f..23732f9e9 100644 --- a/solutions/typescript/2023/06/package.json +++ b/solutions/typescript/2023/06/package.json @@ -61,11 +61,6 @@ "import": "./dist/p2.js", "default": "./dist/p2.js" }, - "./p2.naive": { - "types": "./src/p2.naive.ts", - "import": "./dist/p2.naive.js", - "default": "./dist/p2.naive.js" - }, "./parse": { "types": "./src/parse.ts", "import": "./dist/parse.js", diff --git a/solutions/typescript/2023/06/readme.md b/solutions/typescript/2023/06/readme.md index 41b15eff0..c00766bfc 100644 --- a/solutions/typescript/2023/06/readme.md +++ b/solutions/typescript/2023/06/readme.md @@ -1,4 +1,4 @@ -# [Day 6](https://adventofcode.com/2023/day/6) +# [Day 6: Wait For It](https://adventofcode.com/2023/day/6) ## [Part One](https://adventofcode.com/2023/day/6#part1) diff --git a/solutions/typescript/2023/06/src/p1.spec.ts b/solutions/typescript/2023/06/src/p1.spec.ts index 32a9e5721..c426a3615 100644 --- a/solutions/typescript/2023/06/src/p1.spec.ts +++ b/solutions/typescript/2023/06/src/p1.spec.ts @@ -7,14 +7,14 @@ describe('2023 06 p1', () => { describe('the input', () => { it('should solve the input', async () => { const resources = await loadTaskResources(packageJson.aoc); - expect(p1(resources.input)).toEqual(0); + expect(p1(resources.input)).toEqual(160_816); }); }); describe('example 1', () => { it('should be solved', async () => { const resources = await loadTaskResources(packageJson.aoc, 'example.1.txt'); - expect(p1(resources.input)).toEqual(0); + expect(p1(resources.input)).toEqual(288); }); }); }); diff --git a/solutions/typescript/2023/06/src/p1.ts b/solutions/typescript/2023/06/src/p1.ts index 6b8e8d926..feda71020 100644 --- a/solutions/typescript/2023/06/src/p1.ts +++ b/solutions/typescript/2023/06/src/p1.ts @@ -1,8 +1,25 @@ import { task } from '@alexaegis/advent-of-code-lib'; import packageJson from '../package.json'; +import { parseAsSeparateRaces } from './parse.js'; +interface RaceResult { + boatSpeed: number; + distanceTraveled: number; + record: number; +} -export const p1 = (_input: string): number => { - return 0; -}; +export const p1 = (input: string): number => + parseAsSeparateRaces(input) + .map( + (race) => + Array.from({ length: race.time }) + .map((_v, i) => { + const boatSpeed = i; + const remainingTime = race.time - boatSpeed; + const distanceTraveled = remainingTime * boatSpeed; + return { boatSpeed, distanceTraveled, record: race.distance }; + }) + .filter((result) => result.distanceTraveled > result.record).length, + ) + .product(); -await task(p1, packageJson.aoc); // 84470622 ~4.36ms +await task(p1, packageJson.aoc); // 160816 ~40μs diff --git a/solutions/typescript/2023/06/src/p2.spec.ts b/solutions/typescript/2023/06/src/p2.spec.ts index 02ddc2c23..435ae507d 100644 --- a/solutions/typescript/2023/06/src/p2.spec.ts +++ b/solutions/typescript/2023/06/src/p2.spec.ts @@ -7,14 +7,14 @@ describe('2023 05 p2', () => { describe('the input', () => { it('should solve the input', async () => { const { input } = await loadTaskResources(packageJson.aoc); - expect(p2(input)).toEqual(0); + expect(p2(input)).toEqual(46_561_107); }); }); describe('example 1', () => { it('should be solved', async () => { const { input } = await loadTaskResources(packageJson.aoc, 'example.1.txt'); - expect(p2(input)).toEqual(0); + expect(p2(input)).toEqual(71_503); }); }); }); diff --git a/solutions/typescript/2023/06/src/p2.ts b/solutions/typescript/2023/06/src/p2.ts index 6b3905b81..3391e8b06 100644 --- a/solutions/typescript/2023/06/src/p2.ts +++ b/solutions/typescript/2023/06/src/p2.ts @@ -1,8 +1,11 @@ -import { task } from '@alexaegis/advent-of-code-lib'; +import { quadratic, task } from '@alexaegis/advent-of-code-lib'; import packageJson from '../package.json'; +import { parseAsOneRace } from './parse.js'; -export const p2 = (_input: string): number => { - return 0; +export const p2 = (input: string): number => { + const race = parseAsOneRace(input); + const [low, high] = quadratic(1, -race.time, race.distance); + return Math.floor(high) - Math.ceil(low) + 1; }; -await task(p2, packageJson.aoc, 'example.1.txt'); // 84470622 ~4.36ms +await task(p2, packageJson.aoc); // 46561107 ~7μs diff --git a/solutions/typescript/2023/06/src/parse.ts b/solutions/typescript/2023/06/src/parse.ts index 8a782d16b..d2980ae4b 100644 --- a/solutions/typescript/2023/06/src/parse.ts +++ b/solutions/typescript/2023/06/src/parse.ts @@ -1,3 +1,39 @@ -export const parse = (_input: string): number => { - return 0; +export interface Race { + time: number; + distance: number; +} + +export const parseAsSeparateRaces = (input: string): Race[] => { + const races: Race[] = []; + for (const line of input.lines(false)) { + const [, ...n] = line.split(/ +/g); + const values = n.map((n) => Number.parseInt(n, 10)); + if (line.startsWith('Time')) { + for (const value of values) { + races.push({ time: value, distance: -1 }); + } + } else { + for (let i = 0; i < values.length; i++) { + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + races[i]!.distance = values[i]!; + } + } + } + return races; +}; + +export const parseAsOneRace = (input: string): Race => { + const race: Race = { + distance: -1, + time: -1, + }; + for (const line of input.lines(false)) { + const [, ...n] = line.split(/ +/g); + if (line.startsWith('Time')) { + race.time = Number.parseInt(n.join(''), 10); + } else { + race.distance = Number.parseInt(n.join(''), 10); + } + } + return race; }; diff --git a/solutions/typescript/libs/lib/src/math/common/index.ts b/solutions/typescript/libs/lib/src/math/common/index.ts index 9732dd12a..34a568207 100644 --- a/solutions/typescript/libs/lib/src/math/common/index.ts +++ b/solutions/typescript/libs/lib/src/math/common/index.ts @@ -8,4 +8,5 @@ export * from './hz-to-ms.function.js'; export * from './interval.class.js'; export * from './lcm.function.js'; export * from './lerp1d.function.js'; +export * from './quadartic-formula.function.js'; export * from './round-to-decimal.function.js'; diff --git a/solutions/typescript/libs/lib/src/math/common/quadartic-formula.function.ts b/solutions/typescript/libs/lib/src/math/common/quadartic-formula.function.ts new file mode 100644 index 000000000..b9f894b04 --- /dev/null +++ b/solutions/typescript/libs/lib/src/math/common/quadartic-formula.function.ts @@ -0,0 +1,4 @@ +export const quadratic = (a: number, b: number, c: number): [number, number] => { + const sqrt = Math.sqrt(Math.pow(b, 2) - 4 * a * c); + return [((-b - sqrt) / 2) * a, ((-b + sqrt) / 2) * a]; +}; diff --git a/solutions/typescript/readme.md b/solutions/typescript/readme.md index 40949bac1..0531ea97b 100644 --- a/solutions/typescript/readme.md +++ b/solutions/typescript/readme.md @@ -13,7 +13,7 @@ | [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/) | [?ms](/solutions/typescript/2023/05/src/p1.ts) | [?ms](/solutions/typescript/2023/05/src/p2.ts) | -| [Day 6](/solutions/typescript/2023/06/) | [?ms](/solutions/typescript/2023/06/src/p1.ts) | [?ms](/solutions/typescript/2023/06/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 | - | - | | Day 8 | - | - | | Day 9 | - | - |