-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02-a.js
42 lines (39 loc) · 1.03 KB
/
02-a.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
// https://adventofcode.com/2018/day/2
const boxList = require('fs')
.readFileSync('inputData/02-likelyBoxID.txt', 'utf-8')
.split(require('os').EOL)
const aggregate = boxList
// count the letters
.map(id => {
const counts = {}
for (let i = 0; i < id.length; i++) {
const letter = id[i]
counts[letter] = counts[letter] ? counts[letter] + 1 : 1
}
return counts
})
// find the ones that have two and three matches
.map(counts => {
let hasTwo = 0
let hasThree = 0
for (const letter in counts) {
if (hasTwo && hasThree) {
break
} else if (counts[letter] === 2) {
hasTwo = 1
} else if (counts[letter] === 3) {
hasThree = 1
}
}
return { hasTwo, hasThree }
})
// add up the values
.reduce(
(aggregate, boxValues) => {
aggregate.hasTwo += boxValues.hasTwo
aggregate.hasThree += boxValues.hasThree
return aggregate
},
{ hasTwo: 0, hasThree: 0 }
)
console.log(`Checksum: ${aggregate.hasTwo * aggregate.hasThree}`)