From 02e6f1b76ca876f7d64db863c1c98aa41ebf3236 Mon Sep 17 00:00:00 2001 From: Lewis Dale Date: Wed, 6 Dec 2023 08:12:36 +0000 Subject: [PATCH] Day Six complete --- index.ts | 5 ++--- package.json | 1 + src/day_six.test.ts | 21 +++++++++++++++++++++ src/day_six.ts | 43 +++++++++++++++++++++++++++++++++++++++++++ yarn.lock | 5 +++++ 5 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 src/day_six.test.ts create mode 100644 src/day_six.ts diff --git a/index.ts b/index.ts index 1994fcb..429e569 100644 --- a/index.ts +++ b/index.ts @@ -1,3 +1,2 @@ -import {runDayFour} from "./src/day_four"; - -runDayFour(); \ No newline at end of file +import {runDaySix} from "./src/day_six"; +runDaySix() \ No newline at end of file diff --git a/package.json b/package.json index 71b6194..7a745f2 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "typescript": "^5.3.2" }, "dependencies": { + "immutable": "^5.0.0-beta.4", "lodash": "^4.17.21", "parjs": "^0.16.1" } diff --git a/src/day_six.test.ts b/src/day_six.test.ts new file mode 100644 index 0000000..c7a65dd --- /dev/null +++ b/src/day_six.test.ts @@ -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); + }) +}); \ No newline at end of file diff --git a/src/day_six.ts b/src/day_six.ts new file mode 100644 index 0000000..482f96e --- /dev/null +++ b/src/day_six.ts @@ -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()); +} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index f50d58c..b53379b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"