-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
64 lines (52 loc) · 1.91 KB
/
index.ts
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
/**
* ___ __ ___
* | \ __ _ _ _ / \( _ )
* | |) / _` | || | | () / _ \
* |___/\__,_|\_, | \__/\___/
* |__/
*
* "Seven Segment Search"
*
* Challenge:
* There are four-digit seven-segment displays. Segments are lettered A thru G.
* However, the mapping of letter to segment is unknown. [...]
*
* The input contains "unique signal patters" a pipe and "four digit output val"
*
* Digits 1, 4, 7, 8 ea use a unique number of segments so the output is easy to
* determine. Count the number of those digits in the output values (after |).
*/
import { SignalInput } from './SignalInput';
import { SevenSeg } from './SevenSeg';
import { Decoder } from './Decoder';
// Read input
const input = new SignalInput('input.txt');
// Get just the output signals
const output = input.getOutputDigits();
// Tally up how many unique digits there are
let countUniqueDigits = 0;
output.flat()
.forEach(display => countUniqueDigits += SevenSeg.isUnique(display) ? 1 : 0);
console.log(`Of displays, there are ${countUniqueDigits} unique-length digits.\n\n`);
// Part One:
// Of displays, there are 383 unique-length digits.
/**
* ___ _ ___
* | _ \__ _ _ _| |_ |_ )
* | _/ _` | '_| _| / /
* |_| \__,_|_| \__| /___|
*
* Build the mapping for each row's signals to which segments each letter really
* is. And from there, determine the numbers in the display and report the sum.
*
* Note: Each row's input signals have ten unique so there's one for every digit
*/
const fixedNumbers = input.getAll().map(row => {
const fixedSignals = new Decoder(row).resolve().translateOutput();
return new SevenSeg(fixedSignals).getNumber() || 0;
});
const sum = fixedNumbers
.reduce((sum, number) => sum += number, 0);
console.log(`After translating the signals, the sum of displayed numbers is ${sum}`);
// Part Two:
// After translating the signals, the sum of displayed numbers is 998900