Tidy up Day Eight a bit

This commit is contained in:
Lewis Dale 2023-12-08 10:08:22 +00:00
parent f81359618d
commit b4052d956f
2 changed files with 3 additions and 25 deletions

View File

@ -30,12 +30,12 @@ XXX = (XXX, XXX)`
it('should calculate the number of steps needed to reach ZZZ', () => {
const map = new DesertMap(input);
expect(map.stepsTo('ZZZ')).toEqual(2);
expect(map.stepsToZ('AAA')).toEqual(2);
});
it('should repeat the pattern', () => {
const map = new DesertMap(repeatedInput);
expect(map.stepsTo('ZZZ')).toEqual(6);
expect(map.stepsToZ('AAA')).toEqual(6);
});
it('should count how many steps it takes to get from every node beginning with A, to every node ending in Z simultaneously', () => {

View File

@ -45,28 +45,6 @@ export class DesertMap {
}
}
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;
}
public stepsToZ(from: string): number {
let step = 0;
let curr = from;
@ -101,7 +79,7 @@ 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'));
console.log(map.stepsToZ('AAA'));
console.log(map.ghostStepsToZ());
}