₍^. .^₎⟆
This commit is contained in:
commit
f33dda8916
10 changed files with 158 additions and 0 deletions
59
solutions/01.ts
Normal file
59
solutions/01.ts
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
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())
|
||||
Loading…
Add table
Add a link
Reference in a new issue