mirror of
https://github.com/Baipyrus/AoC-23.git
synced 2024-12-27 20:41:45 +00:00
calculate cycle sizes for each starting position
This commit is contained in:
parent
f59e1530a5
commit
f7accd2a13
@ -12,18 +12,23 @@ const maps = input.slice(1).map((a) => {
|
|||||||
const [_, L, R] = mapping;
|
const [_, L, R] = mapping;
|
||||||
return { name, mapping: { L, R } };
|
return { name, mapping: { L, R } };
|
||||||
});
|
});
|
||||||
let count = 0,
|
const cycles = maps
|
||||||
index = 0;
|
.filter((e) => e.name.endsWith("A"))
|
||||||
let current = maps.find((e) => e.name === "AAA");
|
.map((e) => {
|
||||||
while (current.name !== "ZZZ") {
|
let count = 0,
|
||||||
const direction = instructions[index];
|
index = 0;
|
||||||
const mapping = current.mapping[direction];
|
let current = e;
|
||||||
|
while (!current.name.endsWith("Z")) {
|
||||||
|
const direction = instructions[index];
|
||||||
|
const mapping = current.mapping[direction];
|
||||||
|
|
||||||
current = maps.find((e) => e.name === mapping);
|
current = maps.find((a) => a.name === mapping);
|
||||||
index = (index + 1) % instructions.length;
|
index = (index + 1) % instructions.length;
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
console.log(count);
|
return { start: e, end: current, length: count };
|
||||||
|
});
|
||||||
|
console.log(cycles);
|
||||||
|
|
||||||
const t1 = performance.now();
|
const t1 = performance.now();
|
||||||
console.log(`Runtime: ${t1 - t0}ms`);
|
console.log(`Runtime: ${t1 - t0}ms`);
|
||||||
|
Loading…
Reference in New Issue
Block a user