day 3 part 1

This commit is contained in:
mtrx 2024-12-06 02:07:37 +01:00
parent 6c165f08c0
commit 36310a03f6
3 changed files with 35 additions and 1 deletions

View file

@ -68,7 +68,7 @@ class DayTwo implements Solvable {
// accept one unsafe diff if all gradients either ascending or descending
if (ascendingAmount === gradients.length || descendingAmount === gradients.length && unsafeDiffs === 1) return true
// allow for a single wrong gradient
// allow for one wrong gradient
const ascendingExceptOne = ascendingAmount >= gradients.length - 1
const descendingExceptOne = descendingAmount >= gradients.length - 1
return ascendingExceptOne || descendingExceptOne

28
solutions/03.ts Normal file
View file

@ -0,0 +1,28 @@
import { solvables } from "../main.ts";
import type { Solvable } from "../solvable.ts";
import { readInput } from "../utils.ts";
class DayThree implements Solvable {
input = readInput('03')
public part1(): Solvable {
const re = /mul\([0-9]+,[0-9]+\)/g
// get all matches and parse them to int[]
const result = this.input.matchAll(new RegExp(re))
.map(match => match[0]
.slice(4, -1)
.split(',')
.map(arr => Number.parseInt(arr)))
.map(([left, right]) => left * right)
.reduce((prev, curr) => prev + curr)
console.log(result)
return this
}
public part2(): Solvable {
return this
}
}
solvables.push(new DayThree())