-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscrap2.js
184 lines (164 loc) · 4.59 KB
/
scrap2.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
function readInput(fileName) {
const fs = require('fs');
const inputArray = [];
try {
const data = fs.readFileSync(fileName, 'UTF-8');
const lines = data.split(/\r?\n/);
lines.forEach((line) => {
console.log(line);
inputArray.push(line);
});
} catch (err) {
console.error(err);
}
console.log(inputArray);
return inputArray;
}
function filterByBitCriteria(inputArray, position) {
// tally nth bits; see which is highest (if equal, use 1)
// make array of lines with same nth bit
// repeat on new, shortened array, not on full array
// stop if there is only one number
// temporaryArray is the oxygen generator
let temporaryArray = [];
// let co2Array = [];
let zeroCount = 0;
let oneCount = 0;
let currentBit = 0;
// for each line
for (let i = 0; i < inputArray.length; i++) {
if (parseInt(inputArray[i][position]) === 0) {
zeroCount++;
} else {
oneCount++;
}
}
if (oneCount >= zeroCount) {
currentBit = 1;
}
// add matching lines
for (let i = 0; i < inputArray.length; i++) {
if (parseInt(inputArray[i][position]) === currentBit) {
temporaryArray.push(inputArray[i]);
} else {
// co2Array.push(inputArray[i]);
}
}
console.log('temporaryArray', temporaryArray);
// console.log('co2Array', co2Array);
return temporaryArray;
}
// function loop(array, index) {
// let newArray = filterByBitCriteria(array, index);
// let answer = 0;
// if (newArray.length === 1) {
// console.log('newArray', newArray);
// console.log('newArray[0]', newArray[0]);
// // return newArray[0];
// answer = newArray[0];
// console.log('answer1', answer);
// return answer;
// } else {
// loop(newArray, index + 1);
// }
// // return newArray[0];
// // return answer;
// }
function loop(array, index) {
let newArray = filterByBitCriteria(array, index);
let answer = 0;
if (newArray.length === 1) {
console.log('newArray', newArray);
console.log('newArray[0]', newArray[0]);
answer = newArray[0];
// answer = newArray[0].toString();
// answer = 2;
console.log('answer1', answer);
// return;
// return answer;
// return newArray[0];
// let test = newArray[0];
// var test = newArray[0];
// return test;
// I'm too tired to figure out why it won't let me reassign a value within this is statement, so I'm just going to finish it here:
let ogString = answer.toString();
console.log(ogString.split(''));
// also findCo2ScrubberRating isn't right
let co2 = findCo2ScrubberRating(ogString.split(''));
console.log(co2)
let oxygenGeneratorDecimal = parseInt(answer, 2);
let co2ScrubberDecimal = parseInt(co2, 2);
console.log(oxygenGeneratorDecimal, co2ScrubberDecimal);
} else {
loop(newArray, index + 1);
}
// return answer;
// console.log(answer);
return answer;
// return test;
}
function findOxygenGeneratorRating(fileName) {
// get full list
const inputArray = readInput(fileName);
// loop(inputArray, 0);
// let answer = loop(inputArray, 0);
// console.log('answer', answer);
// return answer;
return loop(inputArray, 0);
}
// this isn't right - I think I misread something
function findCo2ScrubberRating(oxygenGeneratorRating) {
let co2ScrubberRatingArray = [];
for (let i = 0; i < oxygenGeneratorRating.length; i++) {
if (oxygenGeneratorRating[i] === 0) {
co2ScrubberRatingArray.push('1');
} else {
co2ScrubberRatingArray.push('0');
}
}
console.log(co2ScrubberRatingArray.join(''));
return co2ScrubberRatingArray.join('');
}
function findCo2ScrubberRating2(inputArray, position) {
let temporaryArray = [];
let zeroCount = 0;
let oneCount = 0;
let currentBit = 0;
// for each line
for (let i = 0; i < inputArray.length; i++) {
if (parseInt(inputArray[i][position]) === 0) {
zeroCount++;
} else {
oneCount++;
}
}
if (oneCount < zeroCount) {
currentBit = 1;
}
console.log(currentBit);
// add matching lines
for (let i = 0; i < inputArray.length; i++) {
if (parseInt(inputArray[i][position]) === currentBit) {
temporaryArray.push(inputArray[i]);
} else {
// co2Array.push(inputArray[i]);
}
}
console.log(zeroCount, oneCount);
console.log('temporaryArray', temporaryArray);
// console.log('co2Array', co2Array);
return temporaryArray;
}
const testArray = [
'00100', '11110',
'10110', '10111',
'10101', '01111',
'00111', '11100',
'10000', '11001',
'00010', '01010'
];
function run() {
// findOxygenGeneratorRating('testInput5.txt');
findCo2ScrubberRating2(testArray);
}
run();