Add 2024/day1/part1.js
This commit is contained in:
parent
b31f118b15
commit
57861f4a72
|
@ -0,0 +1,20 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
const list1=[];
|
||||
const list2=[];
|
||||
|
||||
require('fs').readFileSync('input.txt','utf8').split('\n').filter(i=>i).forEach(line=>{
|
||||
const parts = line.split(' ').map(i=>parseInt(i));
|
||||
list1.push(parts[0]);
|
||||
list2.push(parts[1]);
|
||||
});
|
||||
|
||||
list1.sort((a,b)=>a-b);
|
||||
list2.sort((a,b)=>a-b);
|
||||
|
||||
var dist=0;
|
||||
for(let idx in list1) {
|
||||
dist+=Math.abs(list1[idx]-list2[idx]);
|
||||
}
|
||||
|
||||
console.log('Result:',dist);
|
Loading…
Reference in New Issue