-
Notifications
You must be signed in to change notification settings - Fork 0
/
1.js
31 lines (25 loc) · 947 Bytes
/
1.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
import input from "./input.js";
const binaryDiagnostic = (binarySequences) => {
let gammaRates = binarySequences[0].split('').map(() => 0)
let epsilonRates = binarySequences[0].split('').map(() => 0)
binarySequences.forEach(binarySequence => {
for (let i = 0; i < binarySequence.length; i++) {
if (0 === parseInt(binarySequence.charAt(i))) {
epsilonRates[i]++
} else {
gammaRates[i]++
}
}
})
const gammaRate = parseInt(gammaRates.map((rate => 500 > rate ? '1' : '0')).join(''), 2)
const epsilonRate = parseInt(epsilonRates.map((rate => 500 > rate ? '1' : '0')).join(''), 2)
return gammaRate * epsilonRate
}
const output = binaryDiagnostic(input)
console.log({
title: 'Binary Diagnostic [3.1]',
url: 'https://adventofcode.com/2021/day/3',
inputPreview: input.slice(0, 5),
inputLength: input.length,
output,
})