1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import { loadInput } from "../common.js";
- const _countDictionary = {};
- const input = loadInput(1, parseInput);
- solve(input);
- function parseInput(inputData) {
-
- const lines = inputData.split(/\n/);
- let leftList = [];
- let rightList = [];
-
- for(const line of lines) {
- const [l, r] = line.split(/\s+/).map((num) => parseInt(num, 10));
- leftList.push(l);
- rightList.push(r);
- }
- return [leftList, rightList];
- }
- function solve(parsedInput) {
-
- const [leftList, rightList] = parsedInput;
-
- const similarityValues = [];
- for(const left of leftList) {
- similarityValues.push(left * countFromRightList(left, rightList));
- }
- console.log("List Distance: ", eval(similarityValues.join("+")));
- }
- function countFromRightList(numberToFind, rightList) {
- if(_countDictionary[numberToFind]) {
- return _countDictionary[numberToFind];
- }
- const count = rightList.filter((value) => value == numberToFind).length;
- _countDictionary[numberToFind] = count;
- return count;
- }
|