Day Six complete
This commit is contained in:
parent
9806b225d1
commit
02e6f1b76c
5
index.ts
5
index.ts
|
@ -1,3 +1,2 @@
|
|||
import {runDayFour} from "./src/day_four";
|
||||
|
||||
runDayFour();
|
||||
import {runDaySix} from "./src/day_six";
|
||||
runDaySix()
|
|
@ -18,6 +18,7 @@
|
|||
"typescript": "^5.3.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"immutable": "^5.0.0-beta.4",
|
||||
"lodash": "^4.17.21",
|
||||
"parjs": "^0.16.1"
|
||||
}
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
import {BoatRace, Race} from "./day_six";
|
||||
|
||||
describe('Day Six', () => {
|
||||
const input = `Time: 7 15 30
|
||||
Distance: 9 40 200`;
|
||||
|
||||
it.each([
|
||||
[[7, 9], 4],
|
||||
[[15, 40], 8],
|
||||
[[30, 200], 9],
|
||||
[[71530, 940200], 71503]
|
||||
])('should calculate that race %s could be won %s different ways', (race, expected) => {
|
||||
const boatRace = new BoatRace(input);
|
||||
expect(boatRace.numberOfWinningMethods(race as Race)).toEqual(expected);
|
||||
});
|
||||
|
||||
it('should calculate the total number of winning methods', () => {
|
||||
const boatRace = new BoatRace(input);
|
||||
expect(boatRace.totalNumberOfWaysToBeatRace()).toEqual(71503);
|
||||
})
|
||||
});
|
|
@ -0,0 +1,43 @@
|
|||
import {anyCharOf, spaces1, string, whitespace} from "parjs";
|
||||
import {between, exactly, manySepBy, stringify, then} from "parjs/combinators";
|
||||
import {Range} from "immutable";
|
||||
import fs from "fs";
|
||||
|
||||
export type Race = [number, number];
|
||||
|
||||
const timeName = string('Time:').pipe(then(spaces1()));
|
||||
const distanceName = string('Distance:').pipe(then(spaces1()));
|
||||
|
||||
const timeParser = anyCharOf("0123456789").pipe(manySepBy(whitespace().pipe(exactly(2))), stringify(), between(timeName, whitespace()));
|
||||
const distanceParser = anyCharOf("0123456789").pipe(manySepBy(whitespace().pipe(exactly(2))), stringify(), between(distanceName, whitespace()));
|
||||
|
||||
const parser = timeParser.pipe(then(distanceParser));
|
||||
|
||||
|
||||
export class BoatRace {
|
||||
private readonly race: Race;
|
||||
|
||||
constructor(input: string) {
|
||||
const [times, distances] = parser.parse(input).value;
|
||||
this.race = [parseInt(times), parseInt(distances)];
|
||||
}
|
||||
|
||||
numberOfWinningMethods = (race: Race): number => {
|
||||
const [time, distance] = race;
|
||||
|
||||
const canWin = (holdingTime: number) => ((time - holdingTime) * holdingTime) > distance;
|
||||
|
||||
const range = Range(0, time).filter(canWin).cacheResult();
|
||||
return range.size || 0;
|
||||
}
|
||||
|
||||
public totalNumberOfWaysToBeatRace(): number {
|
||||
return this.numberOfWinningMethods(this.race);
|
||||
}
|
||||
}
|
||||
|
||||
export const runDaySix = () => {
|
||||
const input = fs.readFileSync('./inputs/day_six_input.txt', 'utf-8').trimEnd();
|
||||
const races = new BoatRace(input);
|
||||
console.log(races.totalNumberOfWaysToBeatRace());
|
||||
}
|
|
@ -1219,6 +1219,11 @@ human-signals@^2.1.0:
|
|||
resolved "https://registry.yarnpkg.com/human-signals/-/human-signals-2.1.0.tgz#dc91fcba42e4d06e4abaed33b3e7a3c02f514ea0"
|
||||
integrity sha512-B4FFZ6q/T2jhhksgkbEW3HBvWIfDW85snkQgawt07S7J5QXTk6BkNV+0yAeZrM5QpMAdYlocGoljn0sJ/WQkFw==
|
||||
|
||||
immutable@^5.0.0-beta.4:
|
||||
version "5.0.0-beta.4"
|
||||
resolved "https://registry.yarnpkg.com/immutable/-/immutable-5.0.0-beta.4.tgz#6c12ac4ad8b16aeec4d064c9d2f4024bb270b7ca"
|
||||
integrity sha512-sl9RE3lqd2LoQSESc8VV0k8qE9y57iT7dinq3Q+8mR2dqReHDZlgUrudzmFfZhDXBLXlNJMVWv3SG1YpQIokig==
|
||||
|
||||
import-local@^3.0.2:
|
||||
version "3.1.0"
|
||||
resolved "https://registry.yarnpkg.com/import-local/-/import-local-3.1.0.tgz#b4479df8a5fd44f6cdce24070675676063c95cb4"
|
||||
|
|
Loading…
Reference in New Issue