-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday01.js
42 lines (37 loc) · 1 KB
/
day01.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
41
42
import { read, readEx } from './util.ts';
import './util/prototype-shenanigans.js';
await readEx();
await read();
/** @type string **/
let data;
// 01:58
const partOne = () => {
const lines = data.lines().map(l => l.split(/\s+/).map(n => +n));
const lmin = lines.map(a => a[0]).sort();
const rmin = lines.map(a => a[1]).sort();
console.log(lmin.map((n, i) => Math.abs(n - rmin[i])).sum());
};
// 03:52
const partTwo = () => {
const lines = data.lines().map(l => l.split(/\s+/).map(n => +n));
const lmin = lines.map(a => a[0]);
const rmin = lines.map(a => a[1]);
let sum = 0;
for (const n of lmin) {
let count = rmin.filter(r => r === n).length;
// console.error(n, count);
sum += count * n;
}
console.log(sum);
};
if (Deno.args[0]) {
console.log('Sample Data:');
data = await readEx();
console.log(data.lines().map(l => ' ' + l).join`\n`)
} else {
console.log('Real Data');
data = await read();
}
console.log('Output:');
partOne();
partTwo();