-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
34 lines (25 loc) · 839 Bytes
/
index.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
import { zip, unzip, sum, sumBy } from 'lodash';
import { INPUT_PATH, runner, SAMPLE_PATH } from '../../../lib/utils';
const path = `${__dirname}/${INPUT_PATH}`;
const parseInput = (input: string): [number[], number[]] => {
const lines = input.split('\n').slice(0, -1);
const [left, right] = unzip(
lines.map((row) => {
const [x, y] = row.split(' ');
return [Number(x), Number(y)];
}),
).map((list) => list.sort());
return [left, right];
};
export const day01 = (input: string) => {
const [left, right] = parseInput(input);
return sumBy(zip(left, right), ([x, y]) => Math.abs(Number(x) - Number(y)));
};
const day02 = (input: string) => {
const [left, right] = parseInput(input);
return sum(left.map((x) => right.filter((y) => x === y).length * x));
};
runner({
path,
solution: day01,
});