-
Notifications
You must be signed in to change notification settings - Fork 0
/
day7.js
164 lines (138 loc) · 5.47 KB
/
day7.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
152
153
154
155
156
157
158
159
160
161
162
163
164
const fs = require('fs');
const main = async () => {
// first input is phase setting, second is the input from the last thing.
const options = [9, 8, 7, 6, 5];
const combinations = getCombinations(options);
let highest = 0;
const promises = [];
let program;
const amplifiers = ['A', 'B', 'C', 'D', 'E'];
for (const combination of combinations) {
const outputMap = {};
for (let i = 0; i < amplifiers.length; i++) {
outputMap[amplifiers[i]] = {output: [], phase: combination[i]};
if (amplifiers[i] === 'E') {
outputMap[amplifiers[i]].output.push(0);
}
}
for (let i = 0; i < amplifiers.length; i++) {
program = fs.readFileSync('day7input.txt').toString().split(',');
promises.push(getOutput(program, amplifiers[i], outputMap));
}
await Promise.all(promises);
const finalNum = outputMap['E'].output[outputMap['E'].output.length - 1];
console.log("GOT A NUM ", finalNum);
if (finalNum > highest) {
highest = finalNum;
}
}
console.log('highestNum is ', highest);
};
const getCombinations = (list) => {
if (list.length === 1) {
const newList = [list];
return newList;
}
const combinations = [];
for (let i = 0; i < list.length; i++) {
const firstItem = list[i];
const leftOverList = list.slice(0);
leftOverList.splice(i, 1);
const lists = getCombinations(leftOverList);
for (const item of lists) {
let newList = [firstItem];
combinations.push(newList.concat(item));
}
}
return combinations;
}
const getOutput = async (program, amplifier, outputMap) => {
return new Promise(async (resolve) => {
let inputList;
if (amplifier === 'A') {
inputList = outputMap['E'].output;
} else if (amplifier === 'B') {
inputList = outputMap['A'].output;
} else if (amplifier === 'C') {
inputList = outputMap['B'].output;
} else if (amplifier === 'D') {
inputList = outputMap['C'].output;
} else if (amplifier === 'E') {
inputList = outputMap['D'].output;
}
let index = 0;
let running = true;
let inputParam = 0;
while (running) {
let instruction = String(program[index]);
const firstNumIndex = Number(program.length > index + 1 ? program[index + 1] : null);
const secondNumIndex = Number(program.length > index + 2 ? program[index + 2] : null);
const answerPosition = Number(program.length > index + 3 ? program[index + 3] : null);
while (instruction.length < 5) {
instruction = '0' + String(instruction);
}
const firstNumMode = instruction[2];
const secondNumMode = instruction[1];
const operation = String(instruction).substr(3, 5);
if (operation === '01') {
let firstNum = firstNumMode === '0' ? Number(program[firstNumIndex]) : firstNumIndex;
let secondNum = secondNumMode === '0' ? Number(program[secondNumIndex]) : secondNumIndex;
program[answerPosition] = firstNum + secondNum;
index = index + 4;
} else if (operation === '02') {
let firstNum = firstNumMode === '0' ? Number(program[firstNumIndex]) : firstNumIndex;
let secondNum = secondNumMode === '0' ? Number(program[secondNumIndex]) : secondNumIndex;
program[answerPosition] = firstNum * secondNum;
index = index + 4;
} else if (operation === '03') {
if (inputParam === 0) {
program[firstNumIndex] = outputMap[amplifier].phase;
inputParam = inputParam + 1;
index = index + 2;
continue;
}
while (inputList.length < inputParam) {
await new Promise(resolve => setTimeout(resolve, 500));
}
program[firstNumIndex] = inputList[inputParam - 1];
inputParam = inputParam + 1;
index = index + 2;
} else if (operation === '04') {
let firstNum = firstNumMode === '0' ? Number(program[firstNumIndex]) : firstNumIndex;
outputMap[amplifier].output.push(firstNum);
index = index + 2;
} else if (operation === '99') {
return resolve();
} else if (operation === '05') {
let firstNum = firstNumMode === '0' ? Number(program[firstNumIndex]) : firstNumIndex;
let secondNum = secondNumMode === '0' ? Number(program[secondNumIndex]) : secondNumIndex;
if (Number(firstNum) !== 0) {
index = secondNum;
} else {
index = index + 3;
}
} else if (operation === '06') {
let firstNum = firstNumMode === '0' ? Number(program[firstNumIndex]) : firstNumIndex;
let secondNum = secondNumMode === '0' ? Number(program[secondNumIndex]) : secondNumIndex;
if (Number(firstNum) === 0) {
index = secondNum;
} else {
index = index + 3;
}
} else if (operation === '07') {
let firstNum = firstNumMode === '0' ? Number(program[firstNumIndex]) : firstNumIndex;
let secondNum = secondNumMode === '0' ? Number(program[secondNumIndex]) : secondNumIndex;
program[answerPosition] = Number(firstNum) < Number(secondNum) ? 1 : 0;
index = index + 4;
} else if (operation === '08') {
let firstNum = firstNumMode === '0' ? Number(program[firstNumIndex]) : firstNumIndex;
let secondNum = secondNumMode === '0' ? Number(program[secondNumIndex]) : secondNumIndex;
program[answerPosition] = Number(firstNum) === Number(secondNum) ? 1 : 0;
index = index + 4;
} else {
throw new Error('bad instruction ' + operation);
}
}
});
};
main();