-
Notifications
You must be signed in to change notification settings - Fork 0
/
day9.js
123 lines (104 loc) · 4.23 KB
/
day9.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
const fs = require('fs');
const main = async () => {
let input = fs.readFileSync('day9input.txt').toString().split(',');
getOutput(input);
};
const getOutput = (input) => {
let index = 0;
let running = true;
let relativeBase = 0;
while (running) {
let instruction = String(input[index]);
const firstNumIndex = Number(input.length > index+1 ? input[index+1] : null);
const secondNumIndex = Number(input.length > index+2 ? input[index+2] : null);
const answerPosition = Number(input.length > index+3 ? input[index+3] : null);
while (instruction.length < 5) {
instruction = '0' + String(instruction);
}
const firstNumMode = instruction[2];
const secondNumMode = instruction[1];
const answerPositionMode = instruction[0];
const operation = String(instruction).substr(3, 5);
if (operation === '01') {
const firstNum = getValueWithMode(input, firstNumIndex, firstNumMode, relativeBase);
const secondNum = getValueWithMode(input, secondNumIndex, secondNumMode, relativeBase);
input[getAnswerPosition(answerPosition, answerPositionMode, relativeBase)] = firstNum + secondNum;
index = index + 4;
} else if (operation === '02') {
const firstNum = getValueWithMode(input, firstNumIndex, firstNumMode, relativeBase);
const secondNum = getValueWithMode(input, secondNumIndex, secondNumMode, relativeBase);
input[getAnswerPosition(answerPosition, answerPositionMode, relativeBase)] = firstNum * secondNum;
index = index + 4;
} else if (operation === '03') {
input[getAnswerPosition(firstNumIndex, firstNumMode, relativeBase)] = 2;
index = index + 2;
} else if (operation === '04') {
const firstNum = getValueWithMode(input, firstNumIndex, firstNumMode, relativeBase);
console.log(firstNum);
index = index + 2;
} else if (operation === '99') {
running = false;
} else if (operation === '05') {
const firstNum = getValueWithMode(input, firstNumIndex, firstNumMode, relativeBase);
const secondNum = getValueWithMode(input, secondNumIndex, secondNumMode, relativeBase);
if (Number(firstNum) !== 0) {
index = secondNum;
} else {
index = index + 3;
}
} else if (operation === '06') {
const firstNum = getValueWithMode(input, firstNumIndex, firstNumMode, relativeBase);
const secondNum = getValueWithMode(input, secondNumIndex, secondNumMode, relativeBase);
if (Number(firstNum) === 0) {
index = secondNum;
} else {
index = index + 3;
}
} else if (operation === '07') {
const firstNum = getValueWithMode(input, firstNumIndex, firstNumMode, relativeBase);
const secondNum = getValueWithMode(input, secondNumIndex, secondNumMode, relativeBase);
input[getAnswerPosition(answerPosition, answerPositionMode, relativeBase)] = Number(firstNum) < Number(secondNum) ? 1 : 0;
index = index + 4;
} else if (operation === '08') {
const firstNum = getValueWithMode(input, firstNumIndex, firstNumMode, relativeBase);
const secondNum = getValueWithMode(input, secondNumIndex, secondNumMode, relativeBase);
input[getAnswerPosition(answerPosition, answerPositionMode, relativeBase)] = Number(firstNum) === Number(secondNum) ? 1 : 0;
index = index + 4;
} else if (operation === '09') {
const firstNum = getValueWithMode(input, firstNumIndex, firstNumMode, relativeBase);
relativeBase = relativeBase + firstNum;
index = index + 2;
} else {
throw new Error('bad instruction' + operation);
}
}
return input[0];
};
const getAnswerPosition = (index, mode, relativeBase) => {
if (mode === '0') {
return index;
} else if (mode === '2') {
return relativeBase + index;
} else {
throw new Error('the answer mode is weird ' + mode);
}
}
const getValueWithMode = (program, index, mode, relativeBase) => {
if (mode === '0') {
if (index >= program.length) {
return 0;
} else {
return Number(program[index]);
}
} else if (mode === '1') {
return Number(index);
} else if (mode === '2') {
let nextIndex = index + relativeBase;
if (nextIndex >= program.length) {
return 0;
} else {
return Number(program[nextIndex]);
}
}
}
main();