forked from tpatel/advent-of-code-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
day08.js
96 lines (84 loc) · 2.33 KB
/
day08.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
const fs = require("fs");
const lines = fs
.readFileSync("day08.txt", { encoding: "utf-8" }) // read day??.txt content
.replace(/\r/g, "") // remove all \r characters to avoid issues on Windows
.split("\n") // Split on newline
.filter(Boolean) // Remove empty lines
.map((line) => {
const [signalPatterns, outputValue] = line.split(" | ").map((x) =>
x.split(" ").map((string) => {
const letters = [...string];
letters.sort();
return letters.join``;
})
);
return {
signalPatterns,
outputValue,
};
});
// number => nb of segments
// 0 => 6
// 1 => 2 #
// 2 => 5
// 3 => 5
// 4 => 4 #
// 5 => 5
// 6 => 6
// 7 => 3 #
// 8 => 7 #
// 9 => 6
function part1() {
let counter = 0;
for (const line of lines) {
const matches = line.outputValue.filter((v) =>
[2, 4, 3, 7].includes(v.length)
);
counter += matches.length;
}
console.log(counter);
}
part1();
// a includes all from b
function includes(a, b) {
const set = new Set([...a]);
return [...b].every((x) => set.has(x));
}
function part2() {
let total = 0;
for (const line of lines) {
const matches = {
1: line.signalPatterns.find((x) => x.length === 2),
4: line.signalPatterns.find((x) => x.length === 4),
7: line.signalPatterns.find((x) => x.length === 3),
8: line.signalPatterns.find((x) => x.length === 7),
};
matches[6] = line.signalPatterns.find(
(x) => x.length === 6 && !includes(x, matches[1])
);
matches[9] = line.signalPatterns.find(
(x) => x.length === 6 && x !== matches[6] && includes(x, matches[4])
);
matches[0] = line.signalPatterns.find(
(x) => x.length === 6 && x !== matches[6] && x !== matches[9]
);
matches[3] = line.signalPatterns.find(
(x) => x.length === 5 && includes(x, matches[1])
);
matches[5] = line.signalPatterns.find(
(x) => x.length === 5 && x !== matches[3] && includes(matches[6], x)
);
matches[2] = line.signalPatterns.find(
(x) => x.length === 5 && x !== matches[3] && x !== matches[5]
);
const translationTable = Object.fromEntries(
Object.entries(matches).map((x) => x.reverse())
);
const translated = Number(
line.outputValue.map((signal) => translationTable[signal]).join``
);
total += translated;
}
console.log(total);
}
part2();