-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.ts
95 lines (87 loc) · 2.94 KB
/
solver.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import { PuzzleSolver } from "../types/puzzle-solver.ts";
type OrderingRule = {
numbersBefore: Set<number>;
numbersAfter: Set<number>;
};
export default class Solver24D5 implements PuzzleSolver {
private rules = new Map<number, OrderingRule>();
private updates: number[][] = [];
parseInput(input: string[]) {
let firstHalf = true;
input.forEach((line) => {
if (line === "") {
firstHalf = false;
return;
}
if (firstHalf) {
const [left, right] = line.split("|").map(Number);
const leftRule = this.rules.get(left) ?? {
numbersBefore: new Set<number>(),
numbersAfter: new Set<number>(),
};
const rightRule = this.rules.get(right) ?? {
numbersBefore: new Set<number>(),
numbersAfter: new Set<number>(),
};
leftRule.numbersAfter.add(right);
rightRule.numbersBefore.add(left);
this.rules.set(left, leftRule);
this.rules.set(right, rightRule);
} else {
const update = line.split(",").map(Number);
this.updates.push(update);
}
});
}
solvePart1() {
const correctUpdates = this.updates.filter((update) =>
this.isCorrectOrdering(update)
);
let sum = 0;
correctUpdates.forEach((update) => {
const middleNumber = update[Math.floor(update.length / 2)];
sum += middleNumber;
});
return sum;
}
solvePart2() {
const incorrectUpdates = this.updates.filter(
(update) => !this.isCorrectOrdering(update)
);
let sum = 0;
incorrectUpdates.forEach((update) => {
const fixedUpdate = this.fixOrdering(update);
const middleNumber =
fixedUpdate[Math.floor(fixedUpdate.length / 2)];
sum += middleNumber;
});
return sum;
}
private fixOrdering(update: number[]): number[] {
return update.sort((a, b) => {
const rulesA = this.rules.get(a);
if (rulesA?.numbersAfter.has(b)) {
return -1;
}
if (rulesA?.numbersBefore.has(b)) {
return 1;
}
return 0;
});
}
private isCorrectOrdering(update: number[]): boolean {
for (let i = 0; i < update.length; i++) {
const rule = this.rules.get(update[i]);
if (!rule) continue;
const before = update.slice(0, i);
if (before.some((num) => rule.numbersAfter.has(num))) {
return false;
}
const after = update.slice(i + 1);
if (after.some((num) => rule.numbersBefore.has(num))) {
return false;
}
}
return true;
}
}