forked from tpatel/advent-of-code-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday13.mjs
105 lines (91 loc) · 2.66 KB
/
day13.mjs
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
import { readFileSync } from "node:fs";
const input = readFileSync("day13.txt", { encoding: "utf-8" }) // read day??.txt content
.replace(/\r/g, "") // remove all \r characters to avoid issues on Windows
.trim(); // Remove starting/ending whitespace
// Return a new object to avoid side effects between part 1 and 2
function getInput1() {
return input.split("\n\n").map((group) => {
const [left, right] = group.split("\n").map((line) => JSON.parse(line));
return {
left,
right,
};
});
}
function getInput2() {
return input
.replace(/\n\n/g, "\n")
.split("\n")
.map((line) => JSON.parse(line));
}
function checkOrder(left, right, result) {
const leftIsNumber = typeof left === "number";
const rightIsNumber = typeof right === "number";
if (leftIsNumber && rightIsNumber) {
if (left < right) {
result.rightOrder = true;
return;
}
if (left > right) {
result.rightOrder = false;
return;
}
} else if (!leftIsNumber && !rightIsNumber) {
let index = 0;
while (true) {
// console.log({ left, right, leftIsNumber, rightIsNumber });
if (index > left.length - 1 && index <= right.length - 1) {
// left ran out of items
result.rightOrder = true;
return;
} else if (index <= left.length - 1 && index > right.length - 1) {
// right ran out of items
result.rightOrder = false;
return;
} else if (index > left.length - 1 && index > right.length - 1) {
// no decision, both lists ran out of items
return;
}
// compare the two elements
checkOrder(left[index], right[index], result);
// if we have set the variable rightOrder, stop
if (typeof result.rightOrder !== "undefined") {
return;
}
index++;
}
} else {
if (leftIsNumber) {
checkOrder([left], right, result);
} else {
checkOrder(left, [right], result);
}
}
}
function part1() {
const groups = getInput1();
const result = groups
.map(({ left, right }, index) => {
let result = {};
checkOrder(left, right, result);
// console.log(result.rightOrder);
return result.rightOrder ? index + 1 : 0;
})
.reduce((a, b) => a + b, 0);
console.log(result);
}
function part2() {
const input = getInput2().concat([[[2]], [[6]]]);
const strings = input
.sort((a, b) => {
const result = {};
checkOrder(a, b, result);
return result.rightOrder ? -1 : 1;
})
.map((x) => JSON.stringify(x));
const position2 = strings.indexOf("[[2]]") + 1;
const position6 = strings.indexOf("[[6]]") + 1;
console.log(position2 * position6);
}
part1();
part2();