aoc24/solutions/02.ts

83 lines
3.6 KiB
TypeScript
Raw Permalink Normal View History

2024-12-04 22:27:23 +01:00
import { solvables } from "../main.ts";
import type { Solvable } from "../solvable.ts";
import { readInput } from "../utils.ts";
class DayTwo implements Solvable {
input = readInput('02')
2024-12-08 00:46:45 +01:00
public part1(): Promise<DayTwo> {
2024-12-04 23:15:42 +01:00
const reports = this.input.trim().split("\n").map(line => line.split(' ').map(level => Number.parseInt(level)))
2024-12-08 00:46:45 +01:00
const slidingWindow = (levels: number[]): boolean => {
2024-12-04 23:15:42 +01:00
const gradients: ("ascending" | "descending")[] = []
2024-12-08 00:46:45 +01:00
let [i, j] = [0, 1]
2024-12-04 23:15:42 +01:00
while (j < levels.length) {
// is unsafe if ascent / descend bigger than 3 or smaller than 1
const diff = Math.abs(levels[i] - levels[j])
2024-12-04 23:18:44 +01:00
if (diff > 3 || diff < 1) {
2024-12-04 23:15:42 +01:00
return false
}
// calculate positive or negative gradient
if (levels[i] < levels[j]) {
2024-12-08 00:46:45 +01:00
gradients.push("ascending")
2024-12-04 23:15:42 +01:00
} else if (levels[i] > levels[j]) {
gradients.push("descending")
2024-12-04 23:19:34 +01:00
}
i++
2024-12-04 23:15:42 +01:00
j++
}
// is safe and constant gradient?
const constantGradient = gradients.every((gradient) => gradient === "ascending") || gradients.every((gradient) => gradient === "descending")
return constantGradient
}
const result = reports.map(levels => slidingWindow(levels)).filter(level_safe => level_safe === true).length
console.log(result)
2024-12-08 00:46:45 +01:00
return new Promise(() => this)
2024-12-04 22:27:23 +01:00
}
2024-12-05 00:08:33 +01:00
2024-12-08 00:46:45 +01:00
public part2(): Promise<DayTwo> {
2024-12-05 00:08:33 +01:00
const reports = this.input.trim().split("\n").map(line => line.split(' ').map(level => Number.parseInt(level)))
2024-12-08 00:46:45 +01:00
const slidingWindow = (levels: number[]): boolean => {
2024-12-05 00:08:33 +01:00
const gradients: ("ascending" | "descending" | "stagnating")[] = []
const diffs: number[] = []
2024-12-08 00:46:45 +01:00
let [i, j] = [0, 1]
2024-12-05 00:08:33 +01:00
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]) {
2024-12-08 00:46:45 +01:00
gradients.push("ascending")
2024-12-05 00:08:33 +01:00
} else if (levels[i] > levels[j]) {
gradients.push("descending")
} else {
gradients.push("stagnating")
}
i++
j++
}
// amount of unsafe diffs
const unsafeDiffs = diffs.filter((diff) => diff > 3 || diff < 1).length
// how many gradients are ascending / descending?
const ascendingAmount = gradients.filter((gradient) => gradient === "ascending").length
const descendingAmount = gradients.filter((gradient) => gradient === "descending").length
// accept one unsafe diff if all gradients either ascending or descending
if (ascendingAmount === gradients.length || descendingAmount === gradients.length && unsafeDiffs === 1) return true
2024-12-06 02:07:37 +01:00
// allow for one wrong gradient
2024-12-05 00:08:33 +01:00
const ascendingExceptOne = ascendingAmount >= gradients.length - 1
const descendingExceptOne = descendingAmount >= gradients.length - 1
return ascendingExceptOne || descendingExceptOne
}
const result = reports.map(levels => slidingWindow(levels)).filter(level_safe => level_safe === true).length
console.log(result)
2024-12-08 00:46:45 +01:00
return new Promise(() => this)
2024-12-04 22:27:23 +01:00
}
}
solvables.push(new DayTwo())