-
Notifications
You must be signed in to change notification settings - Fork 0
/
day3.js
72 lines (60 loc) · 2.23 KB
/
day3.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
import { getAlphabet, parseLinesFromFile, writeAnswer } from './helpers.js';
function buildPriorities() {
const alphabet = getAlphabet().split('');
return alphabet.reduce((priorities, letter, index) => {
priorities[letter] = index + 1;
priorities[letter.toUpperCase()] = index + 27;
return priorities;
}, {});
}
function findCommon(list1, list2, list3) {
const common12 = findCommonItems(list1, list2);
const common23 = findCommonItems(list2, list3);
const common13 = findCommonItems(list1, list3);
if (common12.length !== 1 || common23.length !== 1 || common13.length !== 1) {
return findCommon(common12.join(''), common23.join(''), common13.join(''));
} else {
return common12[0];
}
}
function findCommonMatches(list1, list2) {
const regex = new RegExp(`[${list2}]`, 'g');
return list1.match(regex);
}
function findCommonItems(list1, list2) {
const matches = findCommonMatches(list1, list2);
return [...new Set(matches)];
}
function findCommonItem(list1, list2) {
const matches = findCommonMatches(list1, list2);
return matches[0];
}
function splitRucksackIntoCompartments(sackContents) {
const half = sackContents.length / 2;
return [sackContents.slice(0, half), sackContents.slice(half)];
}
function solveProblem1(filename) {
const priorityScores = buildPriorities();
const input = parseLinesFromFile(filename);
const priorities = input.reduce((score, rucksack) => {
const [compartment1, compartment2] = splitRucksackIntoCompartments(rucksack);
const commonItem = findCommonItem(compartment1, compartment2);
return score + priorityScores[commonItem];
}, 0);
writeAnswer(priorities);
}
function solveProblem2(filename) {
const priorityScores = buildPriorities();
const input = parseLinesFromFile(filename);
const priorities = input.reduce((score, _rucksack, index) => {
if ((index+1) % 3 === 0) {
const badge = findCommon(input[index], input[index-1], input[index-2]);
return score + priorityScores[badge];
}
return score;
}, 0);
writeAnswer(priorities, 2);
}
const filename = './day3.puzzle.txt';
solveProblem1(filename);
solveProblem2(filename);