-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
81 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time: 7 15 30 | ||
Distance: 9 40 200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time: 57 72 69 92 | ||
Distance: 291 1172 1176 2026 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
solutions/typescript/libs/lib/src/math/common/quadartic-formula.function.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters