Tidy up Day Eight a bit
This commit is contained in:
parent
f81359618d
commit
b4052d956f
@ -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', () => {
|
||||
|
@ -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());
|
||||
}
|
Loading…
Reference in New Issue
Block a user