-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5.ts
94 lines (80 loc) · 2.47 KB
/
5.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
import * as fs from "fs";
// const input = `47|53
// 97|13
// 97|61
// 97|47
// 75|29
// 61|13
// 75|53
// 29|13
// 97|29
// 53|29
// 61|53
// 97|53
// 61|29
// 47|13
// 75|47
// 97|75
// 47|61
// 75|61
// 47|29
// 75|13
// 53|13
// 75,47,61,53,29
// 97,61,53,29,13
// 75,29,13
// 75,97,47,61,53
// 61,13,29
// 97,13,75,29,47`;
const input = fs.readFileSync("5-input.txt", "utf8");
const lines = input.split("\n");
const part1 = () => {
const rules = lines.filter((line) => line.includes("|")).map((line) => line.split("|").map((nums) => Number(nums)));
const updates = lines.filter((line) => line.includes(",")).map((line) => line.split(",").map((nums) => Number(nums)));
let sum = 0;
for (const update of updates) {
const relevantRules = rules.filter((rule) => {
return update.includes(rule[0]) && update.includes(rule[1]);
});
const correctOrder = relevantRules.every((rule) => update.indexOf(rule[0]) < update.indexOf(rule[1]));
if (correctOrder) {
sum += update.at(Math.floor(update.length / 2))!;
}
}
return sum;
};
const part2 = () => {
const rules = lines.filter((line) => line.includes("|")).map((line) => line.split("|").map((nums) => Number(nums)));
const updates = lines.filter((line) => line.includes(",")).map((line) => line.split(",").map((nums) => Number(nums)));
function isCorrectOrder(rules: number[][], update: number[]): boolean {
return rules.every((rule) => update.indexOf(rule[0]) < update.indexOf(rule[1]));
}
function arrayMove(nums: number[], from: number, to: number) {
const e = nums[from];
nums.splice(from, 1);
nums.splice(to, 0, e);
}
let sum = 0;
for (const update of updates) {
const relevantRules = rules.filter((rule) => {
return update.includes(rule[0]) && update.includes(rule[1]);
});
const correctOrder = isCorrectOrder(relevantRules, update);
if (correctOrder) {
continue;
}
while (!isCorrectOrder(relevantRules, update)) {
for (const rule of relevantRules) {
const [a, b] = [update.indexOf(rule[0]), update.indexOf(rule[1])];
const ok = a < b;
if (!ok) {
arrayMove(update, b, a); // move b in front of a
}
}
}
sum += update.at(Math.floor(update.length / 2))!;
}
return sum;
};
console.log(part1());
console.log(part2());