aoc25/solutions/01.ts
2025-12-03 10:24:15 +01:00

60 lines
No EOL
1.7 KiB
TypeScript

import { solvables } from "../main.ts";
import { Day } from "../day.ts";
import { readInput } from "../utils.ts";
class DayImpl implements Day {
day = 1
input = readInput('01')
public part1(): Day {
const vals: [string, number][] = this.input
.split("\n")
.map(line => [line[0], Number.parseInt(line.slice(1))]);
let state = 50;
let zerooccurences = 0;
for (const [direction, amount] of vals) {
if (direction === 'R') {
state += amount % 100;
state %= 100;
} else if (direction === 'L') {
state -= amount % 100;
if (state < 0) state += 100;
}
state === 0 ? zerooccurences++ : null;
}
console.log(zerooccurences)
return this
}
public part2(): Day {
const vals: [string, number][] = this.input
.split("\n")
.map(line => [line[0], Number.parseInt(line.slice(1))]);
let state = 50;
let zerooccurences = 0;
for (const [direction, amount] of vals) {
for (let i = 0; i < amount; i++) {
state === 0 ? zerooccurences++ : null;
if (direction === 'R') {
state += 1;
if (state === 100) {
state = 0;
}
} else if (direction === 'L') {
state -= 1;
if (state === -1) {
state = 99;
}
}
}
}
console.log(zerooccurences)
return this
}
}
solvables.push(new DayImpl())