59 lines
No EOL
1.7 KiB
TypeScript
59 lines
No EOL
1.7 KiB
TypeScript
import { solvables } from "../main.ts";
|
|
|
|
import type { Solvable } from "../solvable.ts";
|
|
import { readInput } from "../utils.ts";
|
|
|
|
class DayOne implements Solvable {
|
|
input = readInput('01')
|
|
|
|
public part1(): DayOne {
|
|
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(): DayOne {
|
|
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 DayOne()) |