-
Notifications
You must be signed in to change notification settings - Fork 0
/
01.ts
51 lines (46 loc) · 1.27 KB
/
01.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
export function getListTotalDistance(
left: Array<number>,
right: Array<number>,
): number {
let result = 0;
const leftSorted = left.toSorted();
const rightSorted = right.toSorted();
for (var i = 0; i < leftSorted.length; i++) {
result += Math.abs(leftSorted[i] - rightSorted[i]);
}
return result;
}
export function getListSimilarityScore(
left: Array<number>,
right: Array<number>,
): number {
const getCount = (needle: number, haystack: Array<number>): number => {
return haystack.reduce((acc: number, cur: number) => {
return acc + Number(cur === needle);
}, 0);
};
let result = 0;
for (const num of left) {
let count = 0;
for (const other of right) {
count += Number(other === num);
}
result += num * count;
}
return result;
}
if (import.meta.main) {
const input = await Deno.readTextFile("01_input.txt");
let leftList = [];
let rightList = [];
for (const line of input.split("\n")) {
const res = line.split(/\s/);
leftList.push(parseInt(res.at(0) as string, 10));
rightList.push(parseInt(res.at(-1) as string, 10));
}
console.log("total distance = ", getListTotalDistance(leftList, rightList));
console.log(
"similarity score = ",
getListSimilarityScore(leftList, rightList),
);
}