-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathday08.js
125 lines (112 loc) · 3.12 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
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
'use strict';
const fs = require('fs');
const cl = console.log;
fs.readFile('day08.txt', 'utf-8', (err, input) => {
if (err) throw err;
let lines = input.trim().split(/\r?\n/);
let notes = lines.map(ln => {
let [varTxt, outTxt] = ln.split(' | ');
return {
variations: varTxt.split(' ').map(normSignal),
output: outTxt.split(' ').map(normSignal)
};
});
cl(sevenSegmentSearch1(notes));
cl(sevenSegmentSearch2(notes));
});
function normSignal(signal) {
return [...signal];
}
function sevenSegmentSearch1(notes) {
return notes.reduce((count, note) =>
count + note.output.filter(isUniqueLength).length
, 0);
}
function isUniqueLength(signal) {
switch (signal.length) {
case 2: // 1
case 4: // 4
case 3: // 7
case 7: // 8
return true;
default:
return false;
}
}
function sevenSegmentSearch2(notes) {
return notes
.map(note => {
deduce(note);
let dict = getDict(note);
let numStr =
note.output.map(out => dict.get(sortString(out))).join('');
return Number(numStr);
}).reduce((a, b) => a + b);
}
function deduce(note) {
let vars =
note.variations.sort(function (a, b) { return a.length - b.length });
note.wires1 = vars[0];
note.wires4 = vars[2];
note.wires7 = vars[1];
note.wires8 = vars[9];
note.fiveSegs = vars.slice(3, 6);
note.sixSegs = vars.slice(6, 9);
findWires6(note);
note.c = complement(note.wires1, note.wires6)[0];
findWires0(note);
note.wires9 = note.sixSegs[0];
findWires5(note);
findWires3(note);
note.wires2 = note.fiveSegs[0];
}
function findWires6(note) {
note.wires6 =
note.sixSegs
.filter(wires => complement(note.wires1, wires).length === 1)[0];
note.sixSegs = complement(note.sixSegs, [note.wires6]);
}
function findWires0(note) {
note.wires0 =
note.sixSegs
.filter(wires => complement(note.wires4, wires).length === 1)[0];
note.sixSegs = complement(note.sixSegs, [note.wires0]);
}
function findWires5(note) {
note.wires5 =
note.fiveSegs
.filter(wires => !wires.includes(note.c))[0];
note.fiveSegs = complement(note.fiveSegs, [note.wires5]);
}
function findWires3(note) {
note.wires3 =
note.fiveSegs
.filter(wires => complement(wires, note.wires7).length === 2)[0];
note.fiveSegs = complement(note.fiveSegs, [note.wires3]);
}
function complement(arr1, arr2) {
return arr1.filter(c => !arr2.includes(c));
}
function getDict(note) {
let keys =
[
note.wires0,
note.wires1,
note.wires2,
note.wires3,
note.wires4,
note.wires5,
note.wires6,
note.wires7,
note.wires8,
note.wires9
].map(key => key.sort().join(''));
let dict = new Map();
for (let i = 0; i < 10; i++) {
dict.set(keys[i], String(i));
}
return dict;
}
function sortString(str) {
return [...str].sort().join('');
}