1
1
import "jsr:@std/dotenv/load" ;
2
2
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' ;
3
5
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 ;
23
7
console . log ( 'Connecting to websocket...' ) ;
24
8
websocket ( ) ;
25
9
function websocket ( ) {
26
10
const ws = new WebSocket ( 'ws://localhost:9000/wss' ) ;
27
11
28
12
let latestsolution : string ;
29
- ws . onmessage = async ( event ) => {
13
+ ws . onmessage = ( event ) => {
30
14
const json = JSON . parse ( event . data ) ;
31
15
if ( json . type === 'solutions' ) {
32
16
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 ) {
34
19
ws . send ( JSON . stringify ( {
35
20
type : 'data' ,
36
21
name : latestsolution ,
37
22
} ) ) ;
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
- }
47
23
}
24
+ sendTestInput ( ) ;
48
25
} else if ( json . type === 'result' ) {
49
26
console . log ( `${ json . data } ` ) ;
50
27
} 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
+ }
52
31
}
53
32
} ;
54
33
55
- ws . onopen = ( ) => {
34
+ ws . onopen = async ( ) => {
56
35
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
+ }
57
61
}
58
62
59
63
ws . onclose = ( ) => {
@@ -67,4 +71,17 @@ function websocket() {
67
71
ws . onerror = ( error ) => {
68
72
console . log ( ( error as ErrorEvent ) . message ) ;
69
73
}
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
+ }
70
87
}
0 commit comments