-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday05_printer.ts
152 lines (133 loc) · 3.64 KB
/
day05_printer.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import assert from "node:assert";
import fs from "fs";
import { Counter } from "./utilities/data_structures";
type PageOrderRule = {
before: number;
after: number;
};
type PrinterUpdateData = {
updates: number[][];
pageOrderRules: PageOrderRule[];
};
const parseInput = (input: string): PrinterUpdateData => {
const parts = input.trim().split("\n\n");
const pageOrderRules: PageOrderRule[] = parts[0]
.split("\n")
.map((line) => line.split("|").map(Number))
.map((nums) => {
return {
before: nums[0],
after: nums[1],
};
});
const updates = parts[1]
.split("\n")
.map((line) => line.split(",").map(Number));
return {
updates,
pageOrderRules,
};
};
const isUpdateValid = (update: number[], pageOrderRules: PageOrderRule[]) => {
return update
.map((value, idx) => {
return pageOrderRules
.filter((rule) => rule.before === value || rule.after === value)
.map((rule) => {
if (rule.before === value) {
if (update.indexOf(rule.after) === -1) return true;
return update.indexOf(rule.after) > idx;
}
if (update.indexOf(rule.before) === -1) return true;
return update.indexOf(rule.before) < idx;
})
.reduce((a, b) => a && b, true);
})
.reduce((a, b) => a && b, true);
};
const filterUpdates = (
printerData: PrinterUpdateData,
{ isValid }: { isValid: boolean },
) => {
const { updates, pageOrderRules } = printerData;
return updates.filter((update) => {
const isListValid = isUpdateValid(update, pageOrderRules);
return isValid ? isListValid : !isListValid;
});
};
const sumMiddlePageInUpdate = (updates: number[][]) => {
return updates
.map((update) => update.at(Math.floor(update.length / 2))!)
.reduce((a, b) => a + b);
};
const correctOrderUpdates = (
updates: number[][],
pageOrderRules: PageOrderRule[],
) => {
return updates.map((update) => {
const relevantRules = pageOrderRules.filter(
(rule) =>
update.indexOf(rule.before) > -1 && update.indexOf(rule.after) > -1,
);
const beforeValues = relevantRules.map((rule) => rule.before);
const beforeCount = new Counter(beforeValues);
const afterValues = relevantRules.map((rule) => rule.after);
const afterCount = new Counter(afterValues);
const correctOrder = beforeCount.sorted("desc").map(([value, _]) => value);
correctOrder.push(afterCount.sorted("desc")[0][0]);
return correctOrder;
});
};
const TEST_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
`;
let testPrinterData = parseInput(TEST_INPUT);
const testValidUpdates = filterUpdates(testPrinterData, { isValid: true });
assert(
sumMiddlePageInUpdate(testValidUpdates) === 143,
"part 1 test input incorrect",
);
const testInvalidUpdates = filterUpdates(testPrinterData, { isValid: false });
const testFixedUpdates = correctOrderUpdates(
testInvalidUpdates,
testPrinterData.pageOrderRules,
);
assert(
sumMiddlePageInUpdate(testFixedUpdates) === 123,
"part 2 test input incorrect",
);
const puzzleInput = fs.readFileSync("data/day05_input.txt").toString();
const printerData = parseInput(puzzleInput);
const validUpdates = filterUpdates(printerData, { isValid: true });
console.log("Part 1:", sumMiddlePageInUpdate(validUpdates));
const invalidUpdates = filterUpdates(printerData, { isValid: false });
const fixedUpdates = correctOrderUpdates(
invalidUpdates,
printerData.pageOrderRules,
);
console.log("Part 2:", sumMiddlePageInUpdate(fixedUpdates));