-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
151 lines (126 loc) · 4.38 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
const fs = require( 'fs' );
/**
* Input processing methods
*/
constructCoordString = ( x, y ) => {
return `${x},${y}`;
}
splitCoordString = ( coordString ) => {
return coordString.split( ',' ).map( n => parseInt( n, 10 ) );
}
getCoordFromString = ( xy ) => {
let coordSplit = splitCoordString( xy );
return constructCoordObject( coordSplit[ 0 ], coordSplit[ 1 ], xy );
}
constructCoordObject = ( x, y, xy = null ) => {
return { x: x, y: y, name: ( ( xy ) ? xy : constructCoordString( x, y ) ) };
}
coordDelta = ( start, neighbor ) => {
return { x: neighbor.x - start.x, y: neighbor.y - start.y };
}
/**
* Performance and debug measure methods
*/
getCurrentTime = () => {
return new Date().getTime();
}
getDuration = ( startTime ) => {
return ( getCurrentTime() - startTime );
}
consoleLogSameLine = ( line ) => {
process.stdout.clearLine();
process.stdout.cursorTo( 0 );
process.stdout.write( line );
}
/**
* Input file retrieval and creation
*/
constructInputFileName = ( year, day, inputMode = null ) => {
if( inputMode == null ){ inputMode = getInputRunmode(); }
return day + '_Data_' + inputMode;
}
getInputFileLocation = ( year, day, inputMode = null ) => {
return '../challenges/' + year + '/' + constructInputFileName( year, day, inputMode );
}
getInputLines = ( year, day ) => {
const fileContent = fs.readFileSync( getInputFileLocation( year, day ), 'UTF-8' );
return fileContent.split( /\r?\n/ );
}
// Outdated method used in 2021 for Apex implementations
getInputLinesApex = ( staticResourceName ) => {
const fileContent = fs.readFileSync( '../apex-solutions/force-app/main/default/staticresources/'+ staticResourceName + '.resource', 'UTF-8' );
return fileContent.split( /\r?\n/ );
}
constructInputFileNameApex = ( year, day, inputMode = null ) => {
if( inputMode == null ){ inputMode = getInputRunmode(); }
// Added year indication, since stored as static resources in single org
return 'AOC' + year + '_Day' + day + '_' + inputMode;
}
createInputFiles = ( year, day ) => {
fs.writeFile( getInputFileLocation( year, day, MODE_EXAMPLE ), '', ( err, file ) => { if( err ){ throw err; } } );
fs.writeFile( getInputFileLocation( year, day, MODE_REAL ), '', ( err, file ) => { if( err ){ throw err; } } );
}
createScriptFile = ( year, day ) => {
let scriptFileName = year + '_Day' + day + '.js';
fs.writeFile(
scriptFileName,
SCRIPT_BOILERPLATE.replaceAll( '{day}', day ).replaceAll( '{year}', year ).replaceAll( '{scriptName}', scriptFileName ),
( err, file ) => { if( err ){ throw err; } }
);
}
const MODE_REAL = 'Input';
const MODE_EXAMPLE = 'Example';
getInputRunmode = () => {
let scriptParams = process.argv; // [ {Node.exe path}, {script path}, {parameters} ]
if( scriptParams.length == 2 ){
return MODE_REAL;
} else if( scriptParams.length == 3 ){
let runMode = scriptParams[ 2 ]; // real or example
if( runMode === '0' ){
return MODE_EXAMPLE;
} else if( runMode === '1' ){
return MODE_REAL;
} else if( runMode != MODE_REAL && runMode != MODE_EXAMPLE ){
throw Exception( 'Please provide a valid run mode' );
}
return runMode;
} else{
throw Exception( 'please call the script either including runMode, OR without to default to example' );
}
}
module.exports = {
// Coordinates processing
constructCoordString, splitCoordString,
getCoordFromString, constructCoordObject, coordDelta,
// Input processing
constructInputFileName, getInputLines, getInputRunmode,
// Duration processing
getCurrentTime, getDuration,
// Debug/print functions
consoleLogSameLine,
// File construction
createInputFiles, createScriptFile
};
const SCRIPT_BOILERPLATE =
`/**
* Script for Day {day}
* Call script with expected runmode
* node {scriptName} 0
* node {scriptName} 1
*/
const utils = require( './utils.js' );
const inputLines = utils.getInputLines( '{year}', '{day}' );
solvePuzzle01 = () => {
inputLines.forEach( ( line, i ) => {
} );
return null;
}
solvePuzzle02 = () => {
inputLines.forEach( ( line, i ) => {
} );
return null;
}
let startTime = utils.getCurrentTime();
console.log( 'Answer part 1: ' + solvePuzzle01() );
console.log( 'Answer part 2: ' + solvePuzzle02() );
console.log( 'Completed in', utils.getDuration( startTime ) );`