Skip to content

Commit acb62e0

Browse files
committed
helper: toggle real input, paste testinput keybinds
1 parent 19d6e86 commit acb62e0

File tree

1 file changed

+49
-32
lines changed

1 file changed

+49
-32
lines changed

helper/index.ts

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,63 @@
11
import "jsr:@std/dotenv/load";
22
import { exists } from "https://deno.land/[email protected]/fs/mod.ts";
3+
import { readKeypress } from "https://deno.land/x/[email protected]/mod.ts";
4+
import { paste } from 'https://deno.land/x/clipboard/mod.ts';
35

4-
const date = new Date()
5-
if (date.getMonth() === 11) {
6-
console.log('Checking for downloadable data...')
7-
const filename = `S${date.getFullYear().toString().slice(-2)}${date.getDate().toString().padStart(2, '0')}.txt`;
8-
if (Array.from(Deno.readDirSync('../data')).map(entry => entry.name).includes(filename)) {
9-
console.log('Data is already downloaded.')
10-
} else {
11-
console.log('Downloading data...')
12-
const data = await fetch(`https://adventofcode.com/${date.getFullYear()}/day/${date.getDate()}/input`, {
13-
headers: {
14-
cookie: 'session=' + Deno.env.get('AOC_SESSION')
15-
}
16-
}).then(response => response.text())
17-
Deno.writeTextFileSync('../data/' + filename, data.trimEnd())
18-
}
19-
} else {
20-
console.log('Not December, skipping data download.')
21-
}
22-
6+
let realinput = false;
237
console.log('Connecting to websocket...');
248
websocket();
259
function websocket() {
2610
const ws = new WebSocket('ws://localhost:9000/wss');
2711

2812
let latestsolution: string;
29-
ws.onmessage = async (event) => {
13+
ws.onmessage = (event) => {
3014
const json = JSON.parse(event.data);
3115
if (json.type === 'solutions') {
3216
console.log('Solutions received, triggering latest solution...');
33-
latestsolution = json.data.sort((a: string, b: string) => Number(b.replace('S', '')) - Number(a.replace('S', '')))[0]
17+
latestsolution = json.data.sort((a: string, b: string) => Number(b.replace('S', '')) - Number(a.replace('S', '')))[ 0 ]
18+
if (realinput) {
3419
ws.send(JSON.stringify({
3520
type: 'data',
3621
name: latestsolution,
3722
}));
38-
if (await exists('./testinput.txt')) {
39-
const testinput = Deno.readTextFileSync('./testinput.txt');
40-
if (testinput !== '') {
41-
ws.send(JSON.stringify({
42-
type: 'run',
43-
solution: latestsolution,
44-
input: testinput,
45-
}));
46-
}
4723
}
24+
sendTestInput();
4825
} else if (json.type === 'result') {
4926
console.log(`${json.data}`);
5027
} else if (json.type === 'data') {
51-
ws.send(JSON.stringify({ type: 'run', solution: latestsolution, input: json.data }));
28+
if (realinput) {
29+
ws.send(JSON.stringify({ type: 'run', solution: latestsolution, input: json.data }));
30+
}
5231
}
5332
};
5433

55-
ws.onopen = () => {
34+
ws.onopen = async () => {
5635
console.log('Connected to websocket');
36+
console.log("Press 'r' to toggle real input testing, 'v' to copy clipboard to testinput.txt, 'ctrl+c' to exit");
37+
for await (const keypress of readKeypress()) {
38+
if (keypress.key === 'r') {
39+
realinput = !realinput;
40+
console.log(`Turned ${realinput ? 'on' : 'off'} real input testing`);
41+
if (latestsolution) {
42+
sendTestInput();
43+
if (realinput) {
44+
ws.send(JSON.stringify({
45+
type: 'data',
46+
name: latestsolution,
47+
}));
48+
}
49+
}
50+
}
51+
if (keypress.key === 'v') {
52+
const input = await paste();
53+
Deno.writeTextFileSync("testinput.txt", input);
54+
console.log('Copied clipboard to testinput.txt');
55+
sendTestInput();
56+
}
57+
if (keypress.key === 'c' && keypress.ctrlKey) {
58+
Deno.exit();
59+
}
60+
}
5761
}
5862

5963
ws.onclose = () => {
@@ -67,4 +71,17 @@ function websocket() {
6771
ws.onerror = (error) => {
6872
console.log((error as ErrorEvent).message);
6973
}
74+
75+
async function sendTestInput() {
76+
if (await exists('./testinput.txt')) {
77+
const testinput = Deno.readTextFileSync('./testinput.txt');
78+
if (testinput !== '') {
79+
ws.send(JSON.stringify({
80+
type: 'run',
81+
solution: latestsolution,
82+
input: testinput,
83+
}));
84+
}
85+
}
86+
}
7087
}

0 commit comments

Comments
 (0)