-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday6.js
63 lines (52 loc) · 1.48 KB
/
day6.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
const fs = require('fs')
fs.readFile('./input_day6.txt', 'utf8' , (err, data) => {
if (err) {
console.error(err)
return err;
}
// calculateSeatIDs("BFFFBBFRRR");
// calculateSeatIDs("FFFBBBFRRR");
// calculateSeatIDs("BBFFBBFRLL");
calculateAnswers(data)
})
function calculateAnswers(input) {
let data = input.split("\n");
let groupNo = 0;
let groupData = "";
let groupDataList = [];
let sum = 0;
let sum2 = 0;
let groupSize = 0;
data.forEach((row, i) => {
if (row === "") {
groupNo++;
let orderedAnswers = groupData.split("").sort();
let rawAnswers = orderedAnswers.join();
for (var j = 0; j < orderedAnswers.length - 1; j++) {
if (orderedAnswers[j] === orderedAnswers[j+1]) {
orderedAnswers.splice(j, 1);
j--;
}
}
orderedAnswers = orderedAnswers.join();
while (orderedAnswers.includes(",") || rawAnswers.includes(",")) {
orderedAnswers = orderedAnswers.replace(",","");
rawAnswers = rawAnswers.replace(",","");
}
for (var j = 0; j < rawAnswers.length; j++) {
if (rawAnswers[j] === rawAnswers[j + groupSize - 1]) {
sum2++;
}
}
groupDataList.push(orderedAnswers);
sum += orderedAnswers.length;
groupSize = 0;
groupData = "";
} else {
groupData = groupData.concat(row);
groupSize++;
}
});
console.log("Sum anyone answered", sum);
console.log("Sum everyone answered", sum2);
}