fix scaffolding, start d3
This commit is contained in:
parent
048cc9d4c2
commit
af91eb4ed5
6 changed files with 61 additions and 22 deletions
7
day.ts
Normal file
7
day.ts
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
interface Day {
|
||||||
|
day: number
|
||||||
|
part1(): Day;
|
||||||
|
part2(): Day;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type { Day }
|
||||||
18
main.ts
18
main.ts
|
|
@ -1,8 +1,8 @@
|
||||||
import type { Solvable } from "./solvable.ts";
|
import { Day } from "./day.ts";
|
||||||
|
|
||||||
const solutionFiles = Deno.readDirSync('./solutions');
|
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
|
// need to wrap this stuff in a function to not trip the top level await
|
||||||
(async () => {
|
(async () => {
|
||||||
|
|
@ -12,13 +12,21 @@ const solvables: Solvable[] = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
// only solve the latest day
|
// 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:")
|
console.log("Solving latest day only:")
|
||||||
const day = solvables[0];
|
const day = sortedDays.reverse()[0];
|
||||||
console.log(day)
|
|
||||||
|
|
||||||
day.part1()
|
day.part1()
|
||||||
day.part2()
|
day.part2()
|
||||||
})()
|
})()
|
||||||
|
|
||||||
|
|
||||||
export { solvables }
|
export { days as solvables }
|
||||||
|
|
@ -1,12 +1,13 @@
|
||||||
import { solvables } from "../main.ts";
|
import { solvables } from "../main.ts";
|
||||||
|
|
||||||
import type { Solvable } from "../solvable.ts";
|
import { Day } from "../day.ts";
|
||||||
import { readInput } from "../utils.ts";
|
import { readInput } from "../utils.ts";
|
||||||
|
|
||||||
class DayOne implements Solvable {
|
class DayImpl implements Day {
|
||||||
|
day = 1
|
||||||
input = readInput('01')
|
input = readInput('01')
|
||||||
|
|
||||||
public part1(): DayOne {
|
public part1(): Day {
|
||||||
const vals: [string, number][] = this.input
|
const vals: [string, number][] = this.input
|
||||||
.split("\n")
|
.split("\n")
|
||||||
.map(line => [line[0], Number.parseInt(line.slice(1))]);
|
.map(line => [line[0], Number.parseInt(line.slice(1))]);
|
||||||
|
|
@ -27,7 +28,7 @@ class DayOne implements Solvable {
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
public part2(): DayOne {
|
public part2(): Day {
|
||||||
const vals: [string, number][] = this.input
|
const vals: [string, number][] = this.input
|
||||||
.split("\n")
|
.split("\n")
|
||||||
.map(line => [line[0], Number.parseInt(line.slice(1))]);
|
.map(line => [line[0], Number.parseInt(line.slice(1))]);
|
||||||
|
|
@ -56,4 +57,4 @@ class DayOne implements Solvable {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
solvables.push(new DayOne())
|
solvables.push(new DayImpl())
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { solvables } from "../main.ts";
|
import { solvables } from "../main.ts";
|
||||||
|
|
||||||
import type { Solvable } from "../solvable.ts";
|
import type { Day } from "../day.ts";
|
||||||
import { readInput } from "../utils.ts";
|
import { readInput } from "../utils.ts";
|
||||||
|
|
||||||
const checkWindow = (input: number): boolean => {
|
const checkWindow = (input: number): boolean => {
|
||||||
|
|
@ -16,10 +16,11 @@ const checkWindow = (input: number): boolean => {
|
||||||
return left === right;
|
return left === right;
|
||||||
}
|
}
|
||||||
|
|
||||||
class DayTwo implements Solvable {
|
class DayImpl implements Day {
|
||||||
|
day = 2
|
||||||
input = readInput('02')
|
input = readInput('02')
|
||||||
|
|
||||||
public part1(): DayTwo {
|
public part1(): Day {
|
||||||
const vals: [number, number][] = this.input
|
const vals: [number, number][] = this.input
|
||||||
.split(",")
|
.split(",")
|
||||||
.map(range => range.trim().split("-")
|
.map(range => range.trim().split("-")
|
||||||
|
|
@ -31,7 +32,6 @@ class DayTwo implements Solvable {
|
||||||
for (let i = start; i <= end; i++) {
|
for (let i = start; i <= end; i++) {
|
||||||
if (String(i).length % 2 === 1) continue
|
if (String(i).length % 2 === 1) continue
|
||||||
if (checkWindow(i)) {
|
if (checkWindow(i)) {
|
||||||
console.log(i)
|
|
||||||
invalid.push(i);
|
invalid.push(i);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -42,9 +42,9 @@ class DayTwo implements Solvable {
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
|
|
||||||
public part2(): DayTwo {
|
public part2(): Day {
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
solvables.push(new DayTwo())
|
solvables.push(new DayImpl())
|
||||||
29
solutions/03.ts
Normal file
29
solutions/03.ts
Normal file
|
|
@ -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())
|
||||||
|
|
@ -1,6 +0,0 @@
|
||||||
interface Solvable {
|
|
||||||
part1(): Solvable;
|
|
||||||
part2(): Solvable;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type { Solvable }
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue