Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
shahata committed Dec 9, 2023
1 parent e56a767 commit 13638ca
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 39 deletions.
8 changes: 4 additions & 4 deletions src/2023/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ <h1 class="title-event">&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">$year=<
<main>
<style>
.calendar .calendar-color-s { color:#e3b585; }
.calendar .calendar-color-k { color:#6b4d3b; }
.calendar .calendar-color-l { color:#ccccff; }
.calendar .calendar-color-g { color:#00cc00; }
.calendar .calendar-color-y { color:#ffff66; text-shadow:0 0 5px #ffff66, 0 0 10px #ffff66; }
.calendar .calendar-color-w { color:#ffffff; }
.calendar .calendar-color-m { color:#d4dde4; }
.calendar .calendar-color-k { color:#6b4d3b; }
.calendar .calendar-color-l { color:#ccccff; }
.calendar .calendar-color-n { color:#9b715b; }
.calendar .calendar-color-b { color:#5555bb; }
.calendar .calendar-color-g { color:#00cc00; }
</style>
<pre class="calendar"><span aria-hidden="true" class=""> </span>
<span aria-hidden="true" class=""> </span>
Expand All @@ -53,7 +53,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 = 49679;
var server_eta = 32662;
var key = "2023-10-"+server_eta;
var now = Math.floor(new Date().getTime()/1000);
var target = server_eta + now;
Expand Down
37 changes: 36 additions & 1 deletion src/2023/leaderboard.html

Large diffs are not rendered by default.

20 changes: 4 additions & 16 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
import * as path from 'node:path';
import { readdirSync } from 'node:fs';
import { fileURLToPath } from 'node:url';
import solveAll from './utils/solver.js';
import { solveAll, solveAllYears } from './utils/solver.js';
import { execSync } from 'node:child_process';

async function solveAllYears() {
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const years = readdirSync(__dirname).filter(x => x.match(/^\d\d\d\d$/));
for (const year of years) {
await solveAll(year, undefined, false);
}
}

if (!process.env.ADVENT_SESSION) {
try {
process.env.ADVENT_SESSION = execSync(
'cookies https://adventofcode.com/ session',
).toString();
const session = execSync('cookies https://adventofcode.com/ session');
process.env.ADVENT_SESSION = session.toString();
} catch {
//
}
Expand All @@ -37,7 +25,7 @@ if (process.env.ADVENT_SESSION) {
day = `${dayNum}`;
}
if (year) {
solveAll(`${year}`, day && `${day}`).catch(err => console.error(err.stack));
solveAll(year, day).catch(err => console.error(err.stack));
} else {
solveAllYears().catch(err => console.error(err.stack));
}
Expand Down
7 changes: 2 additions & 5 deletions src/utils/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@ import TimeoutConfirm from '@shahata/inquirer-timeout-confirm-prompt';
function renderTemplate(year, name, extension, model) {
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const src = path.resolve(__dirname, '..');
const prefix = path.join(src, year, isDayName(name) ? name : name);
const template = path.join(src, 'template', isDayName(name) ? 'day' : name);
const fileName = `${prefix}.${extension}`;
const fileName = `${path.join(src, year, name)}.${extension}`;
const result = Object.keys(model).reduce(
(result, key) => {
return result.replace(new RegExp(`{{${key}}}`, 'g'), model[key]);
},
(result, key) => result.replaceAll(`{{${key}}}`, model[key]),
readFileSync(`${template}.template.${extension}`).toString(),
);
writeFileSync(fileName, result);
Expand Down
33 changes: 20 additions & 13 deletions src/utils/solver.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,26 +62,27 @@ function getDays(year) {
}
}

function getAllYears() {
const directory = path.resolve(__dirname, '..');
let years = readdirSync(directory).filter(x => x.match(/^\d\d\d\d$/));
return years.sort((a, b) => parseInt(a) - parseInt(b));
}

async function takeScreenshots(year) {
if (year !== '2023') return;
if (year !== getAllYears().at(-1)) return;
const browser = await chromium.launch();
const clip = { x: 0, y: 0, width: 1030, height: 420 };
const page = await browser.newPage();
await page.goto(resolve(import.meta.url, '../2023/events.html'));
await page.goto(resolve(import.meta.url, `../${year}/events.html`));
await page.waitForTimeout(1000);
await page.screenshot({
path: 'src/static/events-screenshot.png',
clip: { x: 0, y: 0, width: 1030, height: 420 },
});
await page.goto(resolve(import.meta.url, '../2023/solver.html'));
await page.screenshot({ path: 'src/static/events-screenshot.png', clip });
await page.goto(resolve(import.meta.url, `../${year}/solver.html`));
await page.waitForTimeout(1000);
await page.screenshot({
path: 'src/static/solver-screenshot.png',
clip: { x: 0, y: 0, width: 1030, height: 420 },
});
await page.screenshot({ path: 'src/static/solver-screenshot.png', clip });
await browser.close();
}

export default async function solveAll(year, day, run = true) {
export async function solveAll(year, day, run = true) {
if (day) {
tempLeaderboard(year);
const solver = solverFunction(year, day);
Expand All @@ -105,7 +106,6 @@ export default async function solveAll(year, day, run = true) {
bar.tick();
}
await takeScreenshots(year);
console.log('');

if (run) {
for (const day of days) {
Expand All @@ -115,3 +115,10 @@ export default async function solveAll(year, day, run = true) {
}
}
}

export async function solveAllYears() {
const years = getAllYears();
for (const year of years) {
await solveAll(year, undefined, false);
}
}

0 comments on commit 13638ca

Please sign in to comment.