diff --git a/scripts/aoc2015/day14/input.txt b/scripts/aoc2015/day14/input.txt new file mode 100644 index 0000000..1f40ea0 --- /dev/null +++ b/scripts/aoc2015/day14/input.txt @@ -0,0 +1,9 @@ +Vixen can fly 8 km/s for 8 seconds, but then must rest for 53 seconds. +Blitzen can fly 13 km/s for 4 seconds, but then must rest for 49 seconds. +Rudolph can fly 20 km/s for 7 seconds, but then must rest for 132 seconds. +Cupid can fly 12 km/s for 4 seconds, but then must rest for 43 seconds. +Donner can fly 9 km/s for 5 seconds, but then must rest for 38 seconds. +Dasher can fly 10 km/s for 4 seconds, but then must rest for 37 seconds. +Comet can fly 3 km/s for 37 seconds, but then must rest for 76 seconds. +Prancer can fly 9 km/s for 12 seconds, but then must rest for 97 seconds. +Dancer can fly 37 km/s for 1 seconds, but then must rest for 36 seconds. \ No newline at end of file diff --git a/scripts/aoc2015/day14/puzzle1.ts b/scripts/aoc2015/day14/puzzle1.ts new file mode 100644 index 0000000..02de162 --- /dev/null +++ b/scripts/aoc2015/day14/puzzle1.ts @@ -0,0 +1,56 @@ +/* +--- Day 14: Reindeer Olympics --- +This year is the Reindeer Olympics! Reindeer can fly at high speeds, but must rest occasionally to recover their energy. Santa would like to know which of his reindeer is fastest, and so he has them race. + +Reindeer can only either be flying (always at their top speed) or resting (not moving at all), and always spend whole seconds in either state. + +For example, suppose you have the following Reindeer: + +Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds. +Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds. +After one second, Comet has gone 14 km, while Dancer has gone 16 km. After ten seconds, Comet has gone 140 km, while Dancer has gone 160 km. On the eleventh second, Comet begins resting (staying at 140 km), and Dancer continues on for a total distance of 176 km. On the 12th second, both reindeer are resting. They continue to rest until the 138th second, when Comet flies for another ten seconds. On the 174th second, Dancer flies for another 11 seconds. + +In this example, after the 1000th second, both reindeer are resting, and Comet is in the lead at 1120 km (poor Dancer has only gotten 1056 km by that point). So, in this situation, Comet would win (if the race ended at 1000 seconds). + +Given the descriptions of each reindeer (in your puzzle input), after exactly 2503 seconds, what distance has the winning reindeer traveled? + + +ALGORITHM: + + +What distance has reindeer traveled after 2503 seconds? +What distance has winning reindeer traveled? - What is the max distance on the board? + + + */ + +import { parseReindeerLine, Reindeer } from "./utils.ts"; + +async function processFile(filename: string, finalTime: number): Promise { + // read input file + const input = await Deno.readTextFile(filename); + // Lets have a board with each reindeer (check class) + const board: Reindeer[] = []; + // init all reindeer's + // + for (const line of input.split("\n")) { + const data = parseReindeerLine(line); + if (data) { + board.push(new Reindeer(data)); + } + } + // After each second, we should "tick" on reindeer + for (let i = 0; i < finalTime; i++) { + for (const reindeer of board) { + reindeer.tick(); + } + } + console.log( + "max distance", + Math.max(...board.map((reindeer) => reindeer.distanceTraveled)), + ); +} + +processFile("simpleInput.txt", 1000); + +processFile("input.txt", 2503); diff --git a/scripts/aoc2015/day14/puzzle2.ts b/scripts/aoc2015/day14/puzzle2.ts new file mode 100644 index 0000000..ee2ea8e --- /dev/null +++ b/scripts/aoc2015/day14/puzzle2.ts @@ -0,0 +1,79 @@ +/* +--- Day 14: Reindeer Olympics --- +This year is the Reindeer Olympics! Reindeer can fly at high speeds, but must rest occasionally to recover their energy. Santa would like to know which of his reindeer is fastest, and so he has them race. + +Reindeer can only either be flying (always at their top speed) or resting (not moving at all), and always spend whole seconds in either state. + +For example, suppose you have the following Reindeer: + +Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds. +Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds. +After one second, Comet has gone 14 km, while Dancer has gone 16 km. After ten seconds, Comet has gone 140 km, while Dancer has gone 160 km. On the eleventh second, Comet begins resting (staying at 140 km), and Dancer continues on for a total distance of 176 km. On the 12th second, both reindeer are resting. They continue to rest until the 138th second, when Comet flies for another ten seconds. On the 174th second, Dancer flies for another 11 seconds. + +In this example, after the 1000th second, both reindeer are resting, and Comet is in the lead at 1120 km (poor Dancer has only gotten 1056 km by that point). So, in this situation, Comet would win (if the race ended at 1000 seconds). + +Given the descriptions of each reindeer (in your puzzle input), after exactly 2503 seconds, what distance has the winning reindeer traveled? + +--- Part Two --- +Seeing how reindeer move in bursts, Santa decides he's not pleased with the old scoring system. + +Instead, at the end of each second, he awards one point to the reindeer currently in the lead. (If there are multiple reindeer tied for the lead, they each get one point.) He keeps the traditional 2503 second time limit, of course, as doing otherwise would be entirely ridiculous. + +Given the example reindeer from above, after the first second, Dancer is in the lead and gets one point. He stays in the lead until several seconds into Comet's second burst: after the 140th second, Comet pulls into the lead and gets his first point. Of course, since Dancer had been in the lead for the 139 seconds before that, he has accumulated 139 points by the 140th second. + +After the 1000th second, Dancer has accumulated 689 points, while poor Comet, our old champion, only has 312. So, with the new scoring system, Dancer would win (if the race ended at 1000 seconds). + +Again given the descriptions of each reindeer (in your puzzle input), after exactly 2503 seconds, how many points does the winning reindeer have? + +ALGORITHM: + +after each second +tick on board +find currentWinningDistance on the board +award reindeers with winning distances with one point + + */ + +import { parseReindeerLine, Reindeer } from "./utils.ts"; + +async function processFile(filename: string, finalTime: number): Promise { + // read input file + const input = await Deno.readTextFile(filename); + // Lets have a board with each reindeer (check class) + const board: Reindeer[] = []; + // init all reindeer's + // + for (const line of input.split("\n")) { + const data = parseReindeerLine(line); + if (data) { + board.push(new Reindeer(data)); + } + } + // After each second, we should "tick" on reindeer + for (let i = 0; i < finalTime; i++) { + for (const reindeer of board) { + reindeer.tick(); + } + // find max distance + const maxDistance = Math.max( + ...board.map((reindeer) => reindeer.distanceTraveled), + ); + // award each dear with travelDistance equal to max distance + for ( + const reindeer of board.filter((reindeer) => + reindeer.distanceTraveled === maxDistance + ) + ) { + reindeer.addPoint(); + } + } + + console.log( + "max points", + Math.max(...board.map((reindeer) => reindeer.points)), + ); +} + +processFile("simpleInput.txt", 1000); + +processFile("input.txt", 2503); diff --git a/scripts/aoc2015/day14/simpleInput.txt b/scripts/aoc2015/day14/simpleInput.txt new file mode 100644 index 0000000..3ef0b13 --- /dev/null +++ b/scripts/aoc2015/day14/simpleInput.txt @@ -0,0 +1,2 @@ +Comet can fly 14 km/s for 10 seconds, but then must rest for 127 seconds. +Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds. \ No newline at end of file diff --git a/scripts/aoc2015/day14/utils.test.ts b/scripts/aoc2015/day14/utils.test.ts new file mode 100644 index 0000000..860112d --- /dev/null +++ b/scripts/aoc2015/day14/utils.test.ts @@ -0,0 +1,74 @@ +import { assertEquals } from "@std/assert"; +import { beforeEach, describe, it } from "@std/testing/bdd"; +import { parseReindeerLine, Reindeer, ReindeerActivity } from "./utils.ts"; + +describe("parseReindeerLine", function () { + it("should return null for invalid line", function () { + assertEquals(parseReindeerLine(""), null); + }); + it("should return object for valid line", function () { + assertEquals( + parseReindeerLine( + "Dancer can fly 16 km/s for 11 seconds, but then must rest for 162 seconds.", + ), + { + name: "Dancer", + speed: 16, + flyLimit: 11, + restLimit: 162, + }, + ); + }); +}); + +describe("Reindeer", function () { + let reindeer: Reindeer; + beforeEach(function () { + reindeer = new Reindeer({ + name: "Dancer", + speed: 16, + flyLimit: 11, + restLimit: 162, + }); + }); + + it("After one second Dancer has gone 16 km.", function () { + reindeer.tick(); + assertEquals(reindeer.currentActivity, ReindeerActivity.Flying); + assertEquals(reindeer.distanceTraveled, 16); + assertEquals(reindeer.timeSpentOnActivity, 1); + }); + it("After 10 seconds Dancer has gone 160 km.", function () { + for (let i = 0; i < 10; i++) { + reindeer.tick(); + } + assertEquals(reindeer.currentActivity, ReindeerActivity.Flying); + assertEquals(reindeer.distanceTraveled, 160); + assertEquals(reindeer.timeSpentOnActivity, 10); + }); + + it("After 11 seconds Dancer has gone 176 km.", function () { + for (let i = 0; i < 11; i++) { + reindeer.tick(); + } + assertEquals(reindeer.currentActivity, ReindeerActivity.Resting); + assertEquals(reindeer.distanceTraveled, 176); + assertEquals(reindeer.timeSpentOnActivity, 0); + }); + it("After 12 seconds Dancer has gone 176 km.", function () { + for (let i = 0; i < 12; i++) { + reindeer.tick(); + } + assertEquals(reindeer.currentActivity, ReindeerActivity.Resting); + assertEquals(reindeer.distanceTraveled, 176); + assertEquals(reindeer.timeSpentOnActivity, 1); + }); + it("After 174 seconds Dancer has gone 192 km.", function () { + for (let i = 0; i < 174; i++) { + reindeer.tick(); + } + assertEquals(reindeer.currentActivity, ReindeerActivity.Flying); + assertEquals(reindeer.distanceTraveled, 192); + assertEquals(reindeer.timeSpentOnActivity, 1); + }); +}); diff --git a/scripts/aoc2015/day14/utils.ts b/scripts/aoc2015/day14/utils.ts new file mode 100644 index 0000000..f2ac49d --- /dev/null +++ b/scripts/aoc2015/day14/utils.ts @@ -0,0 +1,87 @@ +const reindeerRegex = + /(\w+) can fly (\d+) km\/s for (\d+) seconds, but then must rest for (\d+) seconds./; + +export function parseReindeerLine(line: string): { + name: string; + speed: number; + restLimit: number; + flyLimit: number; +} | null { + const match = line.match(reindeerRegex); + if (!match) { + return null; + } + return { + name: match[1], + speed: parseInt(match[2], 10), + flyLimit: parseInt(match[3], 10), + restLimit: parseInt(match[4], 10), + }; +} + +export enum ReindeerActivity { + Flying, + Resting, +} + +export class Reindeer { + private name: string; + // fly speed (km/s) + private speed: number; + // fly limit (s) + private flyLimit: number; + // rest limit (s) + private restLimit: number; + // currentActivity: Flying/Resting + currentActivity: ReindeerActivity; + // timeSpentOnActivity (s) + timeSpentOnActivity: number; + // distanceTraveled: (km) + distanceTraveled: number; + points: number; + constructor({ name, speed, restLimit, flyLimit }: { + name: string; + speed: number; + restLimit: number; + flyLimit: number; + }) { + this.name = name; + this.speed = speed; + this.flyLimit = flyLimit; + this.restLimit = restLimit; + this.currentActivity = ReindeerActivity.Flying; + this.timeSpentOnActivity = 0; + this.distanceTraveled = 0; + this.points = 0; + } + + tick(): void { + // increment time spent on Activity ++1 + this.timeSpentOnActivity++; + // -if currentActivity is FLying + if (this.currentActivity === ReindeerActivity.Flying) { + // update distance traveled + this.distanceTraveled = this.distanceTraveled + this.speed; + // if time spent on activity is equal to fly limit, + if (this.timeSpentOnActivity >= this.flyLimit) { + // switch to resting activity + this.currentActivity = ReindeerActivity.Resting; + // set time spent on Activity to 0 + this.timeSpentOnActivity = 0; + } + // else currentActivity is Resting + } else { + // if time spent on activity is equal to rest limit, + if (this.timeSpentOnActivity >= this.restLimit) { + // switch to flying activity + this.currentActivity = ReindeerActivity.Flying; + // set time spent on Activity to 0 + this.timeSpentOnActivity = 0; + } + } + } + + addPoint(): void { + this.points++; + } +}