Promises
This commit is contained in:
parent
919bdde203
commit
896d607450
6 changed files with 31 additions and 31 deletions
|
@ -6,7 +6,7 @@ import { readInput } from "../utils.ts";
|
|||
class DayOne implements Solvable {
|
||||
input = readInput('01')
|
||||
|
||||
public part1(): DayOne {
|
||||
public part1(): Promise<DayOne> {
|
||||
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<DayOne> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,11 +6,11 @@ import { readInput } from "../utils.ts";
|
|||
class DayTwo implements Solvable {
|
||||
input = readInput('02')
|
||||
|
||||
public part1(): DayTwo {
|
||||
public part1(): Promise<DayTwo> {
|
||||
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<DayTwo> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -14,17 +14,17 @@ class DayThree implements Solvable {
|
|||
.map(arr => Number.parseInt(arr))
|
||||
}
|
||||
|
||||
public part1(): Solvable {
|
||||
public part1(): Promise<Solvable> {
|
||||
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<Solvable> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@ import { readInput } from "../utils.ts";
|
|||
class DayFour implements Solvable {
|
||||
input = readInput('04')
|
||||
|
||||
public part1(): Solvable {
|
||||
public part1(): Promise<Solvable> {
|
||||
|
||||
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<Solvable> {
|
||||
return new Promise(() => this)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Solvable> {
|
||||
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<Solvable> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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<Solvable> {
|
||||
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<Solvable> {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue