-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.js
44 lines (35 loc) · 980 Bytes
/
part2.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
const { getInputString } = require('../utils/getInputString.js');
const file = getInputString(3, false);
const sum = arr => arr.reduce((acc, cur) => {
acc += cur;
return acc;
}, 0);
const rucksacks = file.split('\n');
// add space at start to act as value 0 lol
const priorities = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
// sort rucksacks into groups of 3
const groups = [];
let group = [];
for(const rucksack of rucksacks) {
if(group.length === 3) {
groups.push([...group]);
group = [];
}
group.push(rucksack);
}
groups.push([...group]);
// find the common char in the 3 rucksacks of each group
const priorityScores = [];
groups.forEach(group => {
const [r1, r2, r3] = group;
let match;
for(const char of r1) {
if(r2.includes(char) && r3.includes(char)) {
match = char;
break;
}
}
const priorityScore = priorities.indexOf(match);
priorityScores.push(priorityScore);
});
console.log(sum(priorityScores))