Skip to content

Commit

Permalink
feat: solved 2023 day 6
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAegis committed Dec 6, 2023
1 parent d75a7c9 commit 5d92ade
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 21 deletions.
2 changes: 2 additions & 0 deletions resources/2023/06/example.1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time: 7 15 30
Distance: 9 40 200
2 changes: 2 additions & 0 deletions resources/2023/06/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time: 57 72 69 92
Distance: 291 1172 1176 2026
5 changes: 0 additions & 5 deletions solutions/typescript/2023/06/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion solutions/typescript/2023/06/readme.md
Original file line number Diff line number Diff line change
@@ -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)

Expand Down
4 changes: 2 additions & 2 deletions solutions/typescript/2023/06/src/p1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
25 changes: 21 additions & 4 deletions solutions/typescript/2023/06/src/p1.ts
Original file line number Diff line number Diff line change
@@ -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<RaceResult>((_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
4 changes: 2 additions & 2 deletions solutions/typescript/2023/06/src/p2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
11 changes: 7 additions & 4 deletions solutions/typescript/2023/06/src/p2.ts
Original file line number Diff line number Diff line change
@@ -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
40 changes: 38 additions & 2 deletions solutions/typescript/2023/06/src/parse.ts
Original file line number Diff line number Diff line change
@@ -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;
};
1 change: 1 addition & 0 deletions solutions/typescript/libs/lib/src/math/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Original file line number Diff line number Diff line change
@@ -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];
};
2 changes: 1 addition & 1 deletion solutions/typescript/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | - | - |
Expand Down

0 comments on commit 5d92ade

Please sign in to comment.