1
+ import * as path from "path" ;
2
+ import * as fs from "fs" ;
3
+
4
+ process . stdout . write ( "\u001b[2J\u001b[0;0H" ) ;
5
+
6
+ const sessionid = Math . random ( ) ;
7
+
8
+ export function fhmain ( filename ) {
9
+ console . log ( `${ sessionid } - ${ new Date ( ) . getTime ( ) }
10
+ ====================================
11
+ ${ path . basename ( filename ) . replace ( / \. (?: j s | t s ) / , "" ) }
12
+ ====================================` ) ;
13
+ let txt_name = path . dirname ( filename ) + "/" + path . basename ( path . dirname ( filename ) ) + ".txt" ;
14
+
15
+ global . input = fs . readFileSync ( txt_name , "utf-8" ) ;
16
+ global . lines = global . input . split ( "\n" ) ;
17
+ global . dblines = global . input . split ( "\n\n" ) ;
18
+ global . output = undefined ;
19
+ global . start_time = Date . now ( ) ;
20
+ }
21
+
22
+ global . highlight = msg => "\x1b[31m" + msg + "\x1b(B\x1b[m" ;
23
+ global . copy = text => {
24
+ require ( "clipboardy" ) . writeSync ( text ) ;
25
+ return text ;
26
+ } ;
27
+ global . print = ( ...a ) => console . log ( ...a ) ;
28
+ global . error = ( ...a ) => { throw new Error ( highlight ( a . join ( " " ) ) ) } ,
29
+ global . clearScreen = ( ) => process . stdout . write ( "\u001b[2J\u001b[0;0H" ) ;
30
+
31
+ process . on ( "exit" , ( ) => {
32
+ let total_ms = Date . now ( ) - start_time ;
33
+ console . log ( "\n\x1b[90mCompleted in " + total_ms + " ms. (" + sessionid + ")\x1b(B\x1b[m\x1b[A\x1b[A" )
34
+ } ) ;
35
+ process . on ( "uncaughtException" , ( e ) => {
36
+ console . log ( ) ;
37
+ console . log ( e . stack . replace ( e . message , highlight ( e . message ) ) ) ;
38
+ process . exit ( 1 ) ;
39
+ } ) ;
40
+
41
+ function _defproto ( thing , name , cb ) {
42
+ Object . defineProperty ( thing . prototype , name , {
43
+ enumerable : false ,
44
+ value : function ( ...args ) {
45
+ return cb (
46
+ this ,
47
+ ...args ,
48
+ ) ;
49
+ } ,
50
+ } ) ;
51
+ }
52
+ _defproto ( Object , "defproto" , _defproto ) ;
53
+
54
+ Number . defproto ( "mod" , ( m , n ) => ( ( m % n ) + n ) % n ) ;
55
+ // Object.defproto("dwth", (me, cb) => (cb(me), me));
56
+ _defproto ( Object , "dwth" , ( me , cb ) => ( cb ( me ) , me ) ) ;
57
+ Object . defproto ( "use" , ( me , cb ) => cb ( me ) ) ;
58
+ global . log = ( ...a ) => {
59
+ console . log ( ...a . map ( w => {
60
+ if ( w instanceof Number ) return + w ;
61
+ if ( w instanceof String ) return "" + w ;
62
+ return w ;
63
+ } ) ) ;
64
+ } ;
65
+
66
+ global . vec = ( ...a ) => a . flat ( ) ;
67
+ global . dupe = ( a ) => [ ...a ] ;
68
+ Array . defproto ( "op" , ( a , b , cb ) => a . map ( ( v , i ) => cb ( v , b [ i ] , i , a , b ) ) ) ;
69
+ Array . defproto ( "add" , ( a , b ) => a . op ( b , ( a , b ) => a + b ) ) ;
70
+ Array . defproto ( "sub" , ( a , b ) => a . op ( b , ( a , b ) => a - b ) ) ;
71
+ Array . defproto ( "mul" , ( a , b ) => a . op ( b , ( a , b ) => a * b ) ) ;
72
+ Array . defproto ( "div" , ( a , b ) => a . op ( b , ( a , b ) => a / b ) ) ;
73
+ Array . defproto ( "mod" , ( a , b ) => a . op ( b , ( a , b ) => a . mod ( b ) ) ) ;
74
+ Array . prototype . mapt = Array . prototype . map ;
75
+ for ( const [ index , name ] of [ "x" , "y" , "z" , "a" ] . entries ( ) ) {
76
+ Object . defineProperty ( Array . prototype , name , {
77
+ enumerable : false ,
78
+ get : function ( ) { return this [ index ] } ,
79
+ set : function ( v ) { this [ index ] = v } ,
80
+ } ) ;
81
+ }
82
+
83
+ global . cardinals = [ [ 1 , 0 ] , [ - 1 , 0 ] , [ 0 , 1 ] , [ 0 , - 1 ] ] ;
84
+ global . diagonals = [ [ - 1 , - 1 ] , [ - 1 , 1 ] , [ 1 , - 1 ] , [ 1 , 1 ] ] ;
85
+ global . adjacents = [ ...cardinals , ...diagonals ] ;
86
+
87
+ global . range = ( ...a ) => {
88
+ const [ start , end , step ] = ( a . length === 1 ? [ 0 , a [ 0 ] , 1 ] : a . length === 2 ? [ ...a , 1 ] : a ) ;
89
+ return Array . from ( { length : Math . ceil ( ( end - start ) / step ) } , ( _ , i ) => i * step + start ) ;
90
+ }
0 commit comments