Skip to content

Commit

Permalink
leaderboard
Browse files Browse the repository at this point in the history
  • Loading branch information
shahata committed Dec 8, 2023
1 parent f1befa7 commit 3ac00e0
Show file tree
Hide file tree
Showing 4 changed files with 380 additions and 207 deletions.
15 changes: 7 additions & 8 deletions src/2023/day08.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import { lcm } from '../utils/divisors.js';
function parse(input) {
let [steps, maps] = input.split('\n\n');
maps = maps.split('\n').reduce((acc, map) => {
const [, node, L, R] = map.match(/^(.+) = \((.+), (.+)\)$/);
return { ...acc, [node]: { L, R } };
const [, key, L, R] = map.match(/^(.+) = \((.+), (.+)\)$/);
return { ...acc, [key]: { L, R } };
}, {});
return { steps, maps };
}

function walk(current, steps, maps, dest = key => key === 'ZZZ') {
let count = 0;
for (count = 0; !dest(current); count++) {
for (let i = 0; i < steps.length; i++) current = maps[current][steps[i]];
function walk(key, steps, maps, dest = key => key === 'ZZZ') {
let count;
for (count = 0; !dest(key); count++) {
for (let i = 0; i < steps.length; i++) key = maps[key][steps[i]];
}
return count * steps.length;
}
Expand All @@ -25,6 +25,5 @@ export function part1(input) {
export function part2(input) {
const { steps, maps } = parse(input);
const keys = Object.keys(maps).filter(key => key.endsWith('A'));
const counts = keys.map(x => walk(x, steps, maps, key => key.endsWith('Z')));
return lcm(counts);
return lcm(keys.map(key => walk(key, steps, maps, key => key.endsWith('Z'))));
}
12 changes: 6 additions & 6 deletions src/2023/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ <h1 class="title-event">&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">$year=<
</header>
<main>
<style>
.calendar .calendar-color-g { color:#00cc00; }
.calendar .calendar-color-w { color:#ffffff; }
.calendar .calendar-color-s { color:#e3b585; }
.calendar .calendar-color-y { color:#ffff66; text-shadow:0 0 5px #ffff66, 0 0 10px #ffff66; }
.calendar .calendar-color-l { color:#ccccff; }
.calendar .calendar-color-b { color:#5555bb; }
.calendar .calendar-color-n { color:#9b715b; }
.calendar .calendar-color-m { color:#d4dde4; }
.calendar .calendar-color-y { color:#ffff66; text-shadow:0 0 5px #ffff66, 0 0 10px #ffff66; }
.calendar .calendar-color-b { color:#5555bb; }
.calendar .calendar-color-g { color:#00cc00; }
.calendar .calendar-color-k { color:#6b4d3b; }
.calendar .calendar-color-w { color:#ffffff; }
.calendar .calendar-color-s { color:#e3b585; }
</style>
<pre class="calendar"><span aria-hidden="true" class=""> </span>
<span aria-hidden="true" class=""> </span>
Expand All @@ -55,7 +55,7 @@ <h1 class="title-event">&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">$year=<
(function(){
var countdown = document.getElementById("calendar-countdown");
if (!countdown) return;
var server_eta = 84098;
var server_eta = 44992;
var key = "2023-9-"+server_eta;
var now = Math.floor(new Date().getTime()/1000);
var target = server_eta + now;
Expand Down
540 changes: 358 additions & 182 deletions src/2023/leaderboard.html

Large diffs are not rendered by default.

20 changes: 9 additions & 11 deletions src/utils/divisors.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ export function divisors(x) {

//lcm = a*b/gcd(a,b)
export function lcm(numbers) {
return numbers
.map(x => Math.abs(x))
.reduce((a, b) => {
const m = a * b;
while (b) {
const t = b;
b = a % b;
a = t;
}
return m / a;
});
return numbers.reduce((a, b) => {
const m = a * b;
while (b) {
const t = b;
b = a % b;
a = t;
}
return m / a;
});
}

0 comments on commit 3ac00e0

Please sign in to comment.