From 633bfcdee59c9797df46f4bcd8647eb63794ceb5 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Sat, 9 Dec 2023 21:03:38 +0100 Subject: [PATCH] calculate least common multiple of all cycle sizes --- Day8/Part2.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Day8/Part2.js b/Day8/Part2.js index b321aa6..e9b6abf 100644 --- a/Day8/Part2.js +++ b/Day8/Part2.js @@ -28,7 +28,11 @@ const cycles = maps } return { start: e, end: current, length: count }; }); -console.log(cycles); +const sizes = cycles.map((e) => e.length); +const gcd = (a, b) => (b == 0 ? a : gcd(b, a % b)); +const lcm = (a, b) => (a / gcd(a, b)) * b; +const total = sizes.reduce(lcm, 1); +console.log(total); const t1 = performance.now(); console.log(`Runtime: ${t1 - t0}ms`);