-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday1pt2.js
40 lines (27 loc) · 842 Bytes
/
day1pt2.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
import * as day1input from './day1input.js'
const lists = day1input.lists
// `40094 37480
// 52117 14510`
// a hashtable containing each number frequency
const frequencyObj = lists.reduce((acc, item) => {
const itemSplt = item.split(" ")
// console.log(acc, itemSplt)
const split2 = parseInt(itemSplt[1])
if (acc.hasOwnProperty(split2))
acc[split2] += 1
else
acc[split2] = 1
return acc
},{},)
console.log(frequencyObj)
//
const similarityTotalSum = lists.reduce((acc, item) => {
const itemSplt = item.split(" ")
// console.log(acc, itemSplt)
const split1 = parseInt(itemSplt[0])
if (frequencyObj.hasOwnProperty(split1))
acc += split1 * frequencyObj[split1]
return acc
},0,)
console.log(similarityTotalSum)
console.assert(similarityTotalSum === '28786472')