47 lines
823 B
TypeScript
47 lines
823 B
TypeScript
|
|
const fs = require('fs');
|
||
|
|
const _ = require('lodash');
|
||
|
|
|
||
|
|
const input = fs.readFileSync(__dirname + '/input.txt', 'utf8');
|
||
|
|
|
||
|
|
const arr1: Array<number> = [];
|
||
|
|
const arr2: Array<number> = [];
|
||
|
|
|
||
|
|
input.split("\n").forEach((n: string) => {
|
||
|
|
if (n.match('^\s*$')) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
const ns = n.split(' ');
|
||
|
|
arr1.push(parseInt(ns[0]));
|
||
|
|
arr2.push(parseInt(ns[1]));
|
||
|
|
});
|
||
|
|
|
||
|
|
// Part 1
|
||
|
|
// arr1.sort();
|
||
|
|
// arr2.sort();
|
||
|
|
|
||
|
|
// let sum = 0;
|
||
|
|
|
||
|
|
// arr1.forEach((v, k) => {
|
||
|
|
// sum += Math.max(v, arr2[k]) - Math.min(v, arr2[k]);
|
||
|
|
// });
|
||
|
|
|
||
|
|
// console.log(sum);
|
||
|
|
|
||
|
|
const map: { [key: number]: number } = {};
|
||
|
|
|
||
|
|
arr2.forEach((v) => {
|
||
|
|
if (typeof map[v] === 'undefined') {
|
||
|
|
map[v] = 1;
|
||
|
|
} else {
|
||
|
|
map[v]++;
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
let score = 0;
|
||
|
|
|
||
|
|
arr1.forEach((v) => {
|
||
|
|
score += (v * (map[v] || 0));
|
||
|
|
});
|
||
|
|
|
||
|
|
console.log(score);
|