diff --git a/day.ts b/day.ts new file mode 100644 index 0000000..ca918f5 --- /dev/null +++ b/day.ts @@ -0,0 +1,7 @@ +interface Day { + day: number + part1(): Day; + part2(): Day; +} + +export type { Day } \ No newline at end of file diff --git a/main.ts b/main.ts index ccbe83a..e7a290a 100644 --- a/main.ts +++ b/main.ts @@ -1,8 +1,8 @@ -import type { Solvable } from "./solvable.ts"; +import { Day } from "./day.ts"; const solutionFiles = Deno.readDirSync('./solutions'); -const solvables: Solvable[] = []; +const days: Day[] = []; // need to wrap this stuff in a function to not trip the top level await (async () => { @@ -12,13 +12,21 @@ const solvables: Solvable[] = []; } // only solve the latest day + const sortedDays = days.sort((dayL: Day, dayR: Day) => { + if (dayL.day < dayR.day) { + return -1 + } else if (dayL.day > dayR.day) { + return 1 + } else { + return 0 + } + }) console.log("Solving latest day only:") - const day = solvables[0]; - console.log(day) + const day = sortedDays.reverse()[0]; day.part1() day.part2() })() -export { solvables } \ No newline at end of file +export { days as solvables } \ No newline at end of file diff --git a/solutions/01.ts b/solutions/01.ts index 13e20eb..4ab16a8 100644 --- a/solutions/01.ts +++ b/solutions/01.ts @@ -1,12 +1,13 @@ import { solvables } from "../main.ts"; -import type { Solvable } from "../solvable.ts"; +import { Day } from "../day.ts"; import { readInput } from "../utils.ts"; -class DayOne implements Solvable { +class DayImpl implements Day { + day = 1 input = readInput('01') - public part1(): DayOne { + public part1(): Day { const vals: [string, number][] = this.input .split("\n") .map(line => [line[0], Number.parseInt(line.slice(1))]); @@ -27,7 +28,7 @@ class DayOne implements Solvable { return this } - public part2(): DayOne { + public part2(): Day { const vals: [string, number][] = this.input .split("\n") .map(line => [line[0], Number.parseInt(line.slice(1))]); @@ -56,4 +57,4 @@ class DayOne implements Solvable { } } -solvables.push(new DayOne()) \ No newline at end of file +solvables.push(new DayImpl()) \ No newline at end of file diff --git a/solutions/02.ts b/solutions/02.ts index 42a3976..6a01d60 100644 --- a/solutions/02.ts +++ b/solutions/02.ts @@ -1,6 +1,6 @@ import { solvables } from "../main.ts"; -import type { Solvable } from "../solvable.ts"; +import type { Day } from "../day.ts"; import { readInput } from "../utils.ts"; const checkWindow = (input: number): boolean => { @@ -16,10 +16,11 @@ const checkWindow = (input: number): boolean => { return left === right; } -class DayTwo implements Solvable { +class DayImpl implements Day { + day = 2 input = readInput('02') - public part1(): DayTwo { + public part1(): Day { const vals: [number, number][] = this.input .split(",") .map(range => range.trim().split("-") @@ -31,7 +32,6 @@ class DayTwo implements Solvable { for (let i = start; i <= end; i++) { if (String(i).length % 2 === 1) continue if (checkWindow(i)) { - console.log(i) invalid.push(i); } } @@ -42,9 +42,9 @@ class DayTwo implements Solvable { return this } - public part2(): DayTwo { + public part2(): Day { return this } } -solvables.push(new DayTwo()) \ No newline at end of file +solvables.push(new DayImpl()) \ No newline at end of file diff --git a/solutions/03.ts b/solutions/03.ts new file mode 100644 index 0000000..9412798 --- /dev/null +++ b/solutions/03.ts @@ -0,0 +1,29 @@ +import { solvables } from "../main.ts"; + +import type { Day } from "../day.ts"; +import { readInput } from "../utils.ts"; + +class DayImpl implements Day { + day = 3 + input = readInput('03') + + public part1(): Day { + const vals: string[] = this.input + .split("\n") + + const result = vals.map(battery => { + const sorted = battery.split("").sort() + return Number.parseInt(sorted[0]) + Number.parseInt(sorted[1]) + }).reduce((a, b) => a + b, 0) + + console.log(result) + + return this + } + + public part2(): Day { + return this + } +} + +solvables.push(new DayImpl()) \ No newline at end of file diff --git a/solvable.ts b/solvable.ts deleted file mode 100644 index 413dcf0..0000000 --- a/solvable.ts +++ /dev/null @@ -1,6 +0,0 @@ -interface Solvable { - part1(): Solvable; - part2(): Solvable; -} - -export type { Solvable } \ No newline at end of file