-
Notifications
You must be signed in to change notification settings - Fork 0
/
day24.js
111 lines (79 loc) · 1.79 KB
/
day24.js
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
const fs = require("fs");
const array = fs.readFileSync("day24.txt", { encoding: "utf-8" });
const reg = new RegExp(
`inp w
mul x 0
add x z
mod x 26
div z (-?\\d+)
add x (-?\\d+)
eql x w
eql x 0
mul y 0
add y 25
mul y x
add y 1
mul z y
mul y 0
add y w
add y (-?\\d+)
mul y x
add z y`,
"g"
);
const formatArray = [...array.matchAll(reg)].map((x) =>
x.slice(1, 11).map((y) => Number(y))
);
const solve = (array) => {
const line = array.shift();
if (line[0] === 26) {
return line[1];
}
const obj = { x: line[2], y: 0, children: [] };
while (array.length) {
const y = solve(array);
if (typeof y == "number") {
obj.y = y;
break;
}
obj.children.push(y);
}
return obj;
};
const resolve = (resolver, value) => {
const { x, y, children } = resolver;
const [xVal, yVal] = value(x, y);
let modelNumber = xVal.toString();
for (child of children) {
modelNumber += resolve(child, value);
}
modelNumber += yVal;
return modelNumber;
};
const myFunction = (array) => {
const resolver = solve([...array]);
const value = (x, y) => {
const diff = x + y;
const xVal = diff > 0 ? 9 - diff : 9;
const yVal = diff < 0 ? 9 + diff : 9;
return [xVal, yVal];
};
const modelNumber = resolve(resolver, value);
return modelNumber;
};
const value = myFunction(formatArray);
console.log(value);
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= //
const myFunction2 = (array) => {
const resolver = solve([...array]);
const value = (x, y) => {
const diff = x + y;
const xVal = diff < 0 ? 1 - diff : 1;
const yVal = diff > 0 ? 1 + diff : 1;
return [xVal, yVal];
};
const modelNumber = resolve(resolver, value);
return modelNumber;
};
const value2 = myFunction2(formatArray);
console.log(value2);