-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadventOfCode.js
144 lines (117 loc) · 3.72 KB
/
adventOfCode.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
const path = require('path');
const fs = require('fs/promises');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const argv = yargs(hideBin(process.argv)).argv;
const inquirer = require('inquirer');
global.dayChosen = null;
async function getDayChoices() {
return (await fs.readdir(path.join(__dirname, 'src')))
.sort((a, b) => +a.match(/Day (\d+) - .+/)[1] - +b.match(/Day (\d+) - .+/)[1]);
}
function getDayFromCli(dayChoices) {
const days = dayChoices.map(day => +day.match(/^Day (\d+) - /)[1]);
if (!argv.day) {
return null;
}
if (!days.includes(argv.day)) {
return null;
}
return dayChoices[days.indexOf(argv.day)];
}
async function dayPrompt(choices) {
return inquirer.prompt([
{
type: 'list',
name: 'day',
message: 'Select a day:',
choices,
pageSize: choices.length
},
]).then(answer => answer.day);
}
function getPartFromCli(partChoices) {
if (!argv.part) {
return null;
}
const part = (String(argv.part)).toLowerCase()
if (!partChoices.map(part => part.toLowerCase()).includes(part)) {
return null;
}
return part;
}
async function partPrompt(choices) {
return inquirer.prompt([
{
type: 'list',
name: 'part',
message: 'Select the Quiz Part:',
choices,
default: choices.length - 1
},
]).then(answer => answer.part.toLowerCase());
}
function getInputTypeFromCli(inputTypeChoices) {
if (!argv.inputType) {
return null;
}
const inputType = (String(argv.inputType)).toLowerCase()
if (!inputTypeChoices.map(inputType => inputType.toLowerCase()).includes(inputType)) {
return null;
}
return inputType;
}
async function inputTypePrompt(choices) {
return inquirer.prompt([
{
type: 'list',
name: 'inputType',
message: 'Select the input type:',
choices,
default: choices.length - 1
},
]).then(answer => answer.inputType.toLowerCase());
}
async function init() {
const dayChoices = await getDayChoices();
const partChoices = ['One', 'Two', 'Both'];
const inputTypeChoices = ['Sample', 'Main', 'Both'];
dayChosen = getDayFromCli(dayChoices);
if (!dayChosen) {
dayChosen = await dayPrompt(dayChoices);
}
let partChosen = getPartFromCli(partChoices);
if (!partChosen) {
partChosen = await partPrompt(partChoices);
}
let inputTypeChosen = getInputTypeFromCli(inputTypeChoices);
if (!inputTypeChosen) {
inputTypeChosen = await inputTypePrompt(inputTypeChoices);
}
console.log();
const quiz = require(`./src/${dayChosen}/index`);
if (partChosen === 'one' || partChosen === 'both') {
console.log('Part 1:');
if (inputTypeChosen === 'sample' || inputTypeChosen === 'both') {
console.log('sample answer:');
console.log(await quiz.partOne('sample'));
}
if (inputTypeChosen === 'main' || inputTypeChosen === 'both') {
console.log('main answer:');
console.log(await quiz.partOne('main'));
}
}
if (partChosen === 'both') console.log();
if (partChosen === 'two' || partChosen === 'both') {
console.log('Part 2:');
if (inputTypeChosen === 'sample' || inputTypeChosen === 'both') {
console.log('sample answer:');
console.log(await quiz.partTwo('sample'));
}
if (inputTypeChosen === 'main' || inputTypeChosen === 'both') {
console.log('main answer:');
console.log(await quiz.partTwo('main'));
}
}
}
init();