Compare commits
2 Commits
16a4bdffd7
...
35f5601f1e
Author | SHA1 | Date | |
---|---|---|---|
|
35f5601f1e | ||
|
08947d2c47 |
4
index.ts
4
index.ts
@ -1,3 +1,3 @@
|
||||
import {runDaySeven} from "./src/day_seven";
|
||||
import {runDayEight} from "./src/day_eight";
|
||||
|
||||
runDaySeven();
|
||||
runDayEight();
|
29
src/day_eight.test.ts
Normal file
29
src/day_eight.test.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import {DesertMap} from "./day_eight";
|
||||
|
||||
describe('Day Eight', () => {
|
||||
const input = `RL
|
||||
|
||||
AAA = (BBB, CCC)
|
||||
BBB = (DDD, EEE)
|
||||
CCC = (ZZZ, GGG)
|
||||
DDD = (DDD, DDD)
|
||||
EEE = (EEE, EEE)
|
||||
GGG = (GGG, GGG)
|
||||
ZZZ = (ZZZ, ZZZ)`;
|
||||
|
||||
const repeatedInput = `LLR
|
||||
|
||||
AAA = (BBB, BBB)
|
||||
BBB = (AAA, ZZZ)
|
||||
ZZZ = (ZZZ, ZZZ)`
|
||||
|
||||
it('should calculate the number of steps needed to reach ZZZ', () => {
|
||||
const map = new DesertMap(input);
|
||||
expect(map.stepsTo('ZZZ')).toEqual(2);
|
||||
});
|
||||
|
||||
it('should repeat the pattern', () => {
|
||||
const map = new DesertMap(repeatedInput);
|
||||
expect(map.stepsTo('ZZZ')).toEqual(6);
|
||||
})
|
||||
});
|
66
src/day_eight.ts
Normal file
66
src/day_eight.ts
Normal file
@ -0,0 +1,66 @@
|
||||
import {anyCharOf, newline, uniLetter, whitespace} from "parjs";
|
||||
import {between, exactly, manySepBy, manyTill, stringify, then} from "parjs/combinators";
|
||||
import fs from "fs";
|
||||
|
||||
const patternParser = anyCharOf("LR").pipe(manyTill(newline().pipe(exactly(2))));
|
||||
|
||||
const nodeNameParser = uniLetter().pipe(exactly(3), stringify());
|
||||
const childParser = nodeNameParser.pipe(manySepBy(", "), exactly(2), between("(", ")"));
|
||||
const nodeParser = nodeNameParser.pipe(then(childParser.pipe(between(" = ", whitespace()))))
|
||||
|
||||
const parser = patternParser.pipe(then(nodeParser.pipe(manySepBy(whitespace()))));
|
||||
|
||||
type Maybe<T> = T | undefined;
|
||||
|
||||
type Instruction = "L" | "R";
|
||||
type NodeName = string;
|
||||
type NodeChildren = [Maybe<NodeName>, Maybe<NodeName>];
|
||||
|
||||
export class DesertMap {
|
||||
private readonly pattern: Instruction[];
|
||||
|
||||
private map: Record<NodeName, NodeChildren> = {};
|
||||
|
||||
constructor(input: string) {
|
||||
const [pattern, nodes] = parser.parse(input).value;
|
||||
|
||||
this.pattern = pattern as Instruction[];
|
||||
|
||||
for (const [name, [[leftNode, rightNode]]] of nodes) {
|
||||
if (!this.map[name]) {
|
||||
this.map[name] = [undefined, undefined];
|
||||
}
|
||||
const children = [leftNode !== name ? leftNode : undefined, rightNode !== name ? rightNode : undefined];
|
||||
this.map[name] = children as NodeChildren;
|
||||
}
|
||||
}
|
||||
|
||||
public stepsTo(node: string): number {
|
||||
let step = 0;
|
||||
let curr = "AAA";
|
||||
|
||||
while (curr !== node) {
|
||||
const instruction = this.pattern[step % this.pattern.length];
|
||||
|
||||
const [left, right] = this.map[curr];
|
||||
|
||||
if (instruction === "L" && left) {
|
||||
curr = left;
|
||||
} else if (instruction === "R" && right) {
|
||||
curr = right;
|
||||
}
|
||||
|
||||
if (!curr) return 0;
|
||||
|
||||
step++;
|
||||
}
|
||||
return step;
|
||||
}
|
||||
}
|
||||
|
||||
export const runDayEight = () => {
|
||||
const input = fs.readFileSync('./inputs/day_eight_input.txt', 'utf-8').trimEnd();
|
||||
|
||||
const map = new DesertMap(input);
|
||||
console.log(map.stepsTo('ZZZ'));
|
||||
}
|
Loading…
Reference in New Issue
Block a user