Skip to content

Commit

Permalink
helper: toggle real input, paste testinput keybinds
Browse files Browse the repository at this point in the history
  • Loading branch information
Schlauer-Hax committed Dec 3, 2024
1 parent 19d6e86 commit acb62e0
Showing 1 changed file with 49 additions and 32 deletions.
81 changes: 49 additions & 32 deletions helper/index.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,63 @@
import "jsr:@std/dotenv/load";
import { exists } from "https://deno.land/[email protected]/fs/mod.ts";
import { readKeypress } from "https://deno.land/x/[email protected]/mod.ts";
import { paste } from 'https://deno.land/x/clipboard/mod.ts';

const date = new Date()
if (date.getMonth() === 11) {
console.log('Checking for downloadable data...')
const filename = `S${date.getFullYear().toString().slice(-2)}${date.getDate().toString().padStart(2, '0')}.txt`;
if (Array.from(Deno.readDirSync('../data')).map(entry => entry.name).includes(filename)) {
console.log('Data is already downloaded.')
} else {
console.log('Downloading data...')
const data = await fetch(`https://adventofcode.com/${date.getFullYear()}/day/${date.getDate()}/input`, {
headers: {
cookie: 'session=' + Deno.env.get('AOC_SESSION')
}
}).then(response => response.text())
Deno.writeTextFileSync('../data/' + filename, data.trimEnd())
}
} else {
console.log('Not December, skipping data download.')
}

let realinput = false;
console.log('Connecting to websocket...');
websocket();
function websocket() {
const ws = new WebSocket('ws://localhost:9000/wss');

let latestsolution: string;
ws.onmessage = async (event) => {
ws.onmessage = (event) => {
const json = JSON.parse(event.data);
if (json.type === 'solutions') {
console.log('Solutions received, triggering latest solution...');
latestsolution = json.data.sort((a: string, b: string) => Number(b.replace('S', '')) - Number(a.replace('S', '')))[0]
latestsolution = json.data.sort((a: string, b: string) => Number(b.replace('S', '')) - Number(a.replace('S', '')))[ 0 ]
if (realinput) {
ws.send(JSON.stringify({
type: 'data',
name: latestsolution,
}));
if (await exists('./testinput.txt')) {
const testinput = Deno.readTextFileSync('./testinput.txt');
if (testinput !== '') {
ws.send(JSON.stringify({
type: 'run',
solution: latestsolution,
input: testinput,
}));
}
}
sendTestInput();
} else if (json.type === 'result') {
console.log(`${json.data}`);
} else if (json.type === 'data') {
ws.send(JSON.stringify({ type: 'run', solution: latestsolution, input: json.data }));
if (realinput) {
ws.send(JSON.stringify({ type: 'run', solution: latestsolution, input: json.data }));
}
}
};

ws.onopen = () => {
ws.onopen = async () => {
console.log('Connected to websocket');
console.log("Press 'r' to toggle real input testing, 'v' to copy clipboard to testinput.txt, 'ctrl+c' to exit");
for await (const keypress of readKeypress()) {
if (keypress.key === 'r') {
realinput = !realinput;
console.log(`Turned ${realinput ? 'on' : 'off'} real input testing`);
if (latestsolution) {
sendTestInput();
if (realinput) {
ws.send(JSON.stringify({
type: 'data',
name: latestsolution,
}));
}
}
}
if (keypress.key === 'v') {
const input = await paste();
Deno.writeTextFileSync("testinput.txt", input);
console.log('Copied clipboard to testinput.txt');
sendTestInput();
}
if (keypress.key === 'c' && keypress.ctrlKey) {
Deno.exit();
}
}
}

ws.onclose = () => {
Expand All @@ -67,4 +71,17 @@ function websocket() {
ws.onerror = (error) => {
console.log((error as ErrorEvent).message);
}

async function sendTestInput() {
if (await exists('./testinput.txt')) {
const testinput = Deno.readTextFileSync('./testinput.txt');
if (testinput !== '') {
ws.send(JSON.stringify({
type: 'run',
solution: latestsolution,
input: testinput,
}));
}
}
}
}

0 comments on commit acb62e0

Please sign in to comment.