From 896d607450b1313dadaa77e025ca4fb35da8a48b Mon Sep 17 00:00:00 2001 From: mtrx Date: Sun, 8 Dec 2024 00:46:45 +0100 Subject: [PATCH] Promises --- solutions/01.ts | 8 ++++---- solutions/02.ts | 20 ++++++++++---------- solutions/03.ts | 10 +++++----- solutions/04-a.ts | 8 ++++---- solutions/04-b.ts | 8 ++++---- solutions/05.ts | 8 ++++---- 6 files changed, 31 insertions(+), 31 deletions(-) diff --git a/solutions/01.ts b/solutions/01.ts index 1843cc1..401332f 100644 --- a/solutions/01.ts +++ b/solutions/01.ts @@ -6,7 +6,7 @@ import { readInput } from "../utils.ts"; class DayOne implements Solvable { input = readInput('01') - public part1(): DayOne { + public part1(): Promise { const vals = this.input .split("\n") .map(line => line.split(' ').map(col => Number.parseInt(col))) @@ -20,10 +20,10 @@ class DayOne implements Solvable { const result = diff.reduce((prev, curr) => prev + curr) console.log(result) - return this + return new Promise(() => this) } - public part2(): DayOne { + public part2(): Promise { const vals = this.input.trim().split("\n").map(line => line.split(' ')) const left = vals.map(line => Number.parseInt(line[0])).sort() const right = vals.map(line => Number.parseInt(line[1])).sort() @@ -35,7 +35,7 @@ class DayOne implements Solvable { .map(([left_val, occurences]) => left_val * occurences) .reduce((prev, curr) => prev + curr) console.log(result) - return this + return new Promise(() => this) } } diff --git a/solutions/02.ts b/solutions/02.ts index ab423d5..b5b54c3 100644 --- a/solutions/02.ts +++ b/solutions/02.ts @@ -6,11 +6,11 @@ import { readInput } from "../utils.ts"; class DayTwo implements Solvable { input = readInput('02') - public part1(): DayTwo { + public part1(): Promise { const reports = this.input.trim().split("\n").map(line => line.split(' ').map(level => Number.parseInt(level))) - const slidingWindow = (levels: number[]): boolean => { + const slidingWindow = (levels: number[]): boolean => { const gradients: ("ascending" | "descending")[] = [] - let [i,j] = [0,1] + let [i, j] = [0, 1] while (j < levels.length) { // is unsafe if ascent / descend bigger than 3 or smaller than 1 const diff = Math.abs(levels[i] - levels[j]) @@ -19,7 +19,7 @@ class DayTwo implements Solvable { } // calculate positive or negative gradient if (levels[i] < levels[j]) { - gradients.push("ascending") + gradients.push("ascending") } else if (levels[i] > levels[j]) { gradients.push("descending") } @@ -33,22 +33,22 @@ class DayTwo implements Solvable { const result = reports.map(levels => slidingWindow(levels)).filter(level_safe => level_safe === true).length console.log(result) - return this + return new Promise(() => this) } - public part2(): DayTwo { + public part2(): Promise { const reports = this.input.trim().split("\n").map(line => line.split(' ').map(level => Number.parseInt(level))) - const slidingWindow = (levels: number[]): boolean => { + const slidingWindow = (levels: number[]): boolean => { const gradients: ("ascending" | "descending" | "stagnating")[] = [] const diffs: number[] = [] - let [i,j] = [0,1] + let [i, j] = [0, 1] while (j < levels.length) { // is unsafe if ascent / descend bigger than 3 or smaller than 1 const diff = Math.abs(levels[i] - levels[j]) diffs.push(diff) // calculate positive or negative gradient if (levels[i] < levels[j]) { - gradients.push("ascending") + gradients.push("ascending") } else if (levels[i] > levels[j]) { gradients.push("descending") } else { @@ -76,7 +76,7 @@ class DayTwo implements Solvable { const result = reports.map(levels => slidingWindow(levels)).filter(level_safe => level_safe === true).length console.log(result) - return this + return new Promise(() => this) } } diff --git a/solutions/03.ts b/solutions/03.ts index 3ec544b..d287c9a 100644 --- a/solutions/03.ts +++ b/solutions/03.ts @@ -14,17 +14,17 @@ class DayThree implements Solvable { .map(arr => Number.parseInt(arr)) } - public part1(): Solvable { + public part1(): Promise { const re = /mul\([0-9]{1,3},[0-9]{1,3}\)/g const result = this.input.matchAll(new RegExp(re)) .map(match => this.multiplyInstruction(match[0])) .map(([left, right]) => left * right) .reduce((prev, curr) => prev + curr) - console.log(result) - return this + console.log(result) + return new Promise(() => this) } - public part2(): Solvable { + public part2(): Promise { const re = /(mul\([0-9]{1,3},[0-9]{1,3}\)|do\(\)|don\'t\(\))/g const instructions = this.input.matchAll(new RegExp(re)).map(match => match[0]) let active = true @@ -44,7 +44,7 @@ class DayThree implements Solvable { } } console.log(result) - return this + return new Promise(() => this) } } diff --git a/solutions/04-a.ts b/solutions/04-a.ts index 0ee1110..b27afca 100644 --- a/solutions/04-a.ts +++ b/solutions/04-a.ts @@ -6,7 +6,7 @@ import { readInput } from "../utils.ts"; class DayFour implements Solvable { input = readInput('04') - public part1(): Solvable { + public part1(): Promise { const transpose = (m: string[][]) => m[0].map((_, i) => m.map(x => x[i])) @@ -53,11 +53,11 @@ class DayFour implements Solvable { console.log(horizontal + vertical + downRight + downLeft) - return this + return new Promise(() => this) } - public part2(): Solvable { - return this + public part2(): Promise { + return new Promise(() => this) } } diff --git a/solutions/04-b.ts b/solutions/04-b.ts index 02c0abc..09ec3a5 100644 --- a/solutions/04-b.ts +++ b/solutions/04-b.ts @@ -8,7 +8,7 @@ class DayFour implements Solvable { matrix: string[][] = this.input.trim().split('\n').map(line => line.split('')) - public part1(): Solvable { + public part1(): Promise { const matrix = this.matrix const lookAround = (x: number, y: number) => { @@ -70,10 +70,10 @@ class DayFour implements Solvable { console.log(amountOfMatches) - return this + return new Promise(() => this) } - public part2(): Solvable { + public part2(): Promise { const matrix = this.matrix const checkMas = (x: number, y: number): boolean => { @@ -113,7 +113,7 @@ class DayFour implements Solvable { } console.log(amountOfMatches) - return this + return new Promise(() => this) } } diff --git a/solutions/05.ts b/solutions/05.ts index f0c9a2d..c3c0823 100644 --- a/solutions/05.ts +++ b/solutions/05.ts @@ -27,7 +27,7 @@ class DayFive implements Solvable { .map((update: number[]) => update[Math.floor(update.length / 2)]) .reduce((prev, curr) => prev + curr) - public part1(): Solvable { + public part1(): Promise { const validUpdates: number[][] = [] this.updates.forEach(update => { if (this.followsRules(update)[0]) { @@ -40,10 +40,10 @@ class DayFive implements Solvable { const result = this.middles(validUpdates) console.log(result) - return this + return new Promise(() => this) } - public part2(): Solvable { + public part2(): Promise { const invalidUpdates = this.invalidUpdates const sort = (update: number[]): number[] => { const [compliant, leftIdx, rightIdx] = this.followsRules(update) @@ -59,7 +59,7 @@ class DayFive implements Solvable { const fixedUpdates = invalidUpdates.map(updates => sort(updates)) const result = this.middles(fixedUpdates) console.log(result) - return this + return new Promise(() => this) } }