-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday01.ts
42 lines (37 loc) · 1.07 KB
/
day01.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
35
36
37
38
39
40
41
42
import type { Day } from './Day.ts';
import { splitPairs } from './utils.js';
export class DayImpl implements Day {
private readonly input: Array<Array<number>>;
constructor(input: string) {
this.input = this.parseInput(input);
}
parseInput(input: string) {
return input
.trim()
.split('\n')
.map((line: string) => {
return line
.split(/\s+/)
.map(e => Number.parseInt(e, 10));
});
}
partOne() {
const [left, right] = splitPairs(this.input);
left.sort();
right.sort();
return left.reduce((acc: number, value: number, index: number) => {
const rightValue = right[index];
return acc + Math.abs(value - rightValue);
}, 0);
}
partTwo() {
const [left, right] = splitPairs(this.input);
const similarity: Record<number, number> = right.reduce((acc: Record<number, number>, val: number) => {
acc[val] = (acc[val] ?? 0) + 1;
return acc;
}, {});
return left.reduce((acc: number, value: number) => {
return acc + value * (similarity[value] || 0);
}, 0);
}
}