+42
-40
2021/day19.livemd
+42
-40
2021/day19.livemd
···
9
```elixir
10
# File.read!("day19.txt")
11
input =
12
-
File.read!("day19.txt")
13
|> String.split("\n\n")
14
|> Enum.map(fn scanner ->
15
scanner
···
34
34
35
```
36
37
$$
38
\left\{
39
\begin{alignat*}{3}
40
-
b_{x_1} r_x + b_{x_0} = a_{x_1} \\
41
-
b_{x_2} r_x + b_{x_0} = a_{x_2}
42
\end{alignat*}
43
\right.
44
\left\{
45
\begin{alignat*}{3}
46
-
b_{y_1} r_y + b_{y_0} = a_{y_1} \\
47
-
b_{y_2} r_y + b_{y_0} = a_{y_2} \\
48
\end{alignat*}
49
\right.
50
\left\{
51
\begin{alignat*}{3}
52
-
b_{z_1} r_z + b_{z_0} = a_{z_1} \\
53
-
b_{z_2} r_z + b_{z_0} = a_{z_2} \\
54
\end{alignat*}
55
\right.
56
$$
···
59
This mean that we need to solve:
60
61
$$
62
-
B_x x + b_0 = a_x \\
63
-
B_y y + b_0 = a_y \\
64
-
B_z z + b_0 = a_z
65
$$
66
67
Where:
···
71
{b_1}_d & 1 \\
72
{b_2}_d & 1 \\
73
\end{bmatrix}
74
-
d = \begin{bmatrix}
75
-
r_d \\
76
-
b_{d_0}
77
\end{bmatrix}
78
a_d = \begin{bmatrix}
79
{a_1}_d \\
···
82
$$
83
84
By applying [Cramer's Rule](https://en.wikipedia.org/wiki/Cramer%27s_rule) we can solve these
85
-
linear equations with:
86
87
$$
88
-
e_d = \frac{
89
\begin{vmatrix}
90
-
{b_1}_d & 1 \\
91
-
{b_2}_d & 1
92
\end{vmatrix}
93
}{
94
\begin{vmatrix}
95
-
{a_1}_d & 1 \\
96
-
{a_2}_d & 1
97
\end{vmatrix}
98
-
} = \frac{{b_1}_d - {b_2}_d}{{a_1}_d - {a_2}_d} \\
99
b_{d_0} = \frac{
100
\begin{vmatrix}
101
-
{a_1}_d & {b_1}_d \\
102
-
{a_2}_d & {b_2}_d
103
\end{vmatrix}
104
}{
105
\begin{vmatrix}
106
-
{a_1}_d & 1 \\
107
-
{a_2}_d & 1
108
\end{vmatrix}
109
-
} = \frac{{a_1}_d {b_2}_d - {b_1}_d {a_2}_d}{{a_1}_d - {a_2}_d}
110
$$
111
112
```elixir
···
133
b_2 = reaxe(select_common(p0, p2), axes)
134
135
transform =
136
-
for i <- ~w[x y z]a, into: %{} do
137
-
a_1 = a_1[i]
138
-
a_2 = a_2[i]
139
-
b_1 = b_1[i]
140
-
b_2 = b_2[i]
141
det_b = b_1 - b_2
142
r = div(a_1 - a_2, det_b)
143
144
b_0 = div(b_1 * a_2 - a_1 * b_2, det_b)
145
146
-
{i, {r, b_0}}
147
end
148
149
new_points =
···
210
<!-- livebook:{"output":true} -->
211
212
```
213
-
{:module, Day19, <<70, 79, 82, 49, 0, 0, 26, ...>>, {:pairs, 1}}
214
```
215
216
```elixir
···
282
<!-- livebook:{"output":true} -->
283
284
```
285
-
0.002822
286
1: {5, 25}
287
-
0.017052
288
2: {5, 20}
289
-
0.0355
290
3: {7, 13}
291
-
0.134655
292
4: {6, 7}
293
-
0.229952
294
5: {5, 2}
295
-
0.288351
296
6: {1, 1}
297
-
0.076665
298
7: {1, 0}
299
-
0.071942
300
```
301
302
<!-- livebook:{"output":true} -->
···
9
```elixir
10
# File.read!("day19.txt")
11
input =
12
+
File.read!("2021/day19.txt")
13
|> String.split("\n\n")
14
|> Enum.map(fn scanner ->
15
scanner
···
34
34
35
```
36
37
+
To find rotation of point $b_1$ and $b_2$ around shift point $b_0$
38
+
39
$$
40
\left\{
41
\begin{alignat*}{3}
42
+
{b_1}_x r_x + {b_0}_x = {a_1}_x \\
43
+
{b_2}_x r_x + {b_0}_x = {a_2}_x
44
\end{alignat*}
45
\right.
46
\left\{
47
\begin{alignat*}{3}
48
+
{b_1}_y r_y + {b_0}_y = {a_1}_y \\
49
+
{b_2}_y r_y + {b_0}_y = {a_2}_y \\
50
\end{alignat*}
51
\right.
52
\left\{
53
\begin{alignat*}{3}
54
+
{b_1}_z r_z + {b_0}_z = {a_1}_z \\
55
+
{b_2}_z r_z + {b_0}_z = {a_2}_z \\
56
\end{alignat*}
57
\right.
58
$$
···
61
This mean that we need to solve:
62
63
$$
64
+
B_x r_x + b_0 = a_x \\
65
+
B_y r_y + b_0 = a_y \\
66
+
B_z r_z + b_0 = a_z
67
$$
68
69
Where:
···
73
{b_1}_d & 1 \\
74
{b_2}_d & 1 \\
75
\end{bmatrix}
76
+
b_0 = \begin{bmatrix}
77
+
{b_0}_d \\
78
+
{b_0}_d
79
\end{bmatrix}
80
a_d = \begin{bmatrix}
81
{a_1}_d \\
···
84
$$
85
86
By applying [Cramer's Rule](https://en.wikipedia.org/wiki/Cramer%27s_rule) we can solve these
87
+
linear equations with (we are looking for $b_0$ and $r$):
88
89
$$
90
+
r_d = \frac{
91
\begin{vmatrix}
92
+
{a_1}_d & 1 \\
93
+
{a_2}_d & 1
94
\end{vmatrix}
95
}{
96
\begin{vmatrix}
97
+
{b_1}_d & 1 \\
98
+
{b_2}_d & 1
99
\end{vmatrix}
100
+
} = \frac{{a_1}_d - {a_2}_d}{{b_1}_d - {b_2}_d} \\
101
b_{d_0} = \frac{
102
\begin{vmatrix}
103
+
{b_1}_d & {a_1}_d \\
104
+
{b_2}_d & {a_2}_d
105
\end{vmatrix}
106
}{
107
\begin{vmatrix}
108
+
{b_1}_d & 1 \\
109
+
{b_2}_d & 1
110
\end{vmatrix}
111
+
} = \frac{{b_1}_d {a_2}_d - {a_1}_d {b_2}_d}{{b_1}_d - {b_2}_d}
112
$$
113
114
```elixir
···
135
b_2 = reaxe(select_common(p0, p2), axes)
136
137
transform =
138
+
for d <- ~w[x y z]a, into: %{} do
139
+
a_1 = a_1[d]
140
+
a_2 = a_2[d]
141
+
b_1 = b_1[d]
142
+
b_2 = b_2[d]
143
det_b = b_1 - b_2
144
r = div(a_1 - a_2, det_b)
145
146
b_0 = div(b_1 * a_2 - a_1 * b_2, det_b)
147
148
+
{d, {r, b_0}}
149
end
150
151
new_points =
···
212
<!-- livebook:{"output":true} -->
213
214
```
215
+
{:module, Day19, <<70, 79, 82, 49, 0, 0, 29, ...>>, {:pairs, 1}}
216
```
217
218
```elixir
···
284
<!-- livebook:{"output":true} -->
285
286
```
287
+
0.005503
288
1: {5, 25}
289
+
0.029354
290
2: {5, 20}
291
+
0.067284
292
3: {7, 13}
293
+
0.195126
294
4: {6, 7}
295
+
0.292686
296
5: {5, 2}
297
+
0.374403
298
6: {1, 1}
299
+
0.074645
300
7: {1, 0}
301
+
0.090199
302
```
303
304
<!-- livebook:{"output":true} -->