-
Notifications
You must be signed in to change notification settings - Fork 1
/
day13.html
479 lines (439 loc) Β· 28.6 KB
/
day13.html
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
<!DOCTYPE html>
<html lang="en-us">
<head>
<title>2020\day13.nim</title>
<link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2280%22>π³</text></svg>">
<meta content="text/html; charset=utf-8" http-equiv="content-type">
<meta content="width=device-width, initial-scale=1" name="viewport">
<link rel='stylesheet' href='https://unpkg.com/normalize.css/'>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/dark.min.css">
<link rel='stylesheet' href='https://cdn.jsdelivr.net/gh/pietroppeter/nimib/assets/androidstudio.css'>
<style>
.nb-box {
display: flex;
align-items: center;
justify-content: space-between;
}
.nb-small {
font-size: 0.8rem;
}
button.nb-small {
float: right;
padding: 2px;
padding-right: 5px;
padding-left: 5px;
}
section#source {
display:none
}
</style>
<script async defer data-domain="pietroppeter.github.io/adventofnim" src="https://plausible.io/js/plausible.js"></script>
<style>
a {
text-decoration: none;
color: #009900;
}
a:hover {
color: #99ff99;
}
em {
color: #ffffff;
font-style: normal;
text-shadow: 0 0 5px #ffffff;
}
em.star {
font-style: normal;
color: #ffff66;
text-shadow: 0 0 5px #ffff66;
}
</style>
</head>
<body>
<header>
<div class="nb-box">
<span><a href="..">π‘</a></span>
<span><code>2020\day13.nim</code></span>
<span><a href="https://github.com/pietroppeter/adventofnim"><svg aria-hidden="true" width="1.2em" height="1.2em" style="vertical-align: middle; fill: #fff" preserveAspectRatio="xMidYMid meet" viewBox="0 0 16 16"><path fill-rule="evenodd" d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0016 8c0-4.42-3.58-8-8-8z"></path></svg></a></span>
</div>
<hr>
</header><main>
<h2>2020, day 13: Shuttle Search π π</h2>
<p>Today was another mathematical day and the main character was the
<a href="https://en.wikipedia.org/wiki/Chinese_remainder_theorem">Chinese Remainder Theorem</a> (CRT from now on).</p>
<p>Even though the day was rather straightforward for everyone which was able to recall that,
I find still very nice the fact that people could (and did) come up with very good solutions
without knowing about CRT. In particular most people who did solved the problem without knowing about CRT
did implement a very nice method to find solutions,
<a href="https://en.wikipedia.org/wiki/Chinese_remainder_theorem#Search_by_sieving">search by sieving</a>.</p>
<p>The CRT did click from me rightaway, so I did decide not to look for existing implementation
and decided to use the coefficient given by BΓ©zout's identity, that can be computed with
<a href="https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm">Extended Euclidean Algorithm</a>.</p>
<p>I also knew beforehand that I might run into overflow issues and I might need to use
<a href="https://github.com/def-/nim-bigints">bigints</a>. I decided to first implement with standard <code>int</code>s and only
later upgrade to bigints if needed.</p>
<p>In the end I found out that the solution was below <code>int.high</code> but my specific methods using BΓ©zout's coefficients
did ran into an overflow, so I just applied a quick patch without changing proc's signature.</p>
<p>In the end I think that the solution with the sieve is the most elegant and practical, since it would not need this.</p>
<pre><code class="nim hljs"><span class="hljs-keyword">import</span>
strutils, sequtils
<span class="hljs-keyword">import</span>
sugar</code></pre>
<p>part 1 was rather straightforward. I solved first for the example then wrapped that in a template
and used it also to solve it for the real input. No reading from file this, just parsing directly in source and also very basic parsing.</p>
<pre><code class="nim hljs"><span class="hljs-keyword">template</span> solve1() =
<span class="hljs-keyword">echo</span> $buses <span class="hljs-comment">## why nimsuggest complains when no $?</span>
<span class="hljs-keyword">var</span> id, wait, minWait: <span class="hljs-built_in">int</span>
minWait = <span class="hljs-built_in">int</span>.<span class="hljs-keyword">high</span>
<span class="hljs-keyword">for</span> bus <span class="hljs-keyword">in</span> buses:
wait = bus - (timestamp <span class="hljs-keyword">mod</span> bus)
<span class="hljs-keyword">echo</span> bus, <span class="hljs-string">": "</span>, wait
<span class="hljs-keyword">if</span> wait < minWait:
minWait = wait
id = bus
<span class="hljs-keyword">echo</span> (id, minWait, id * minWait)
<span class="hljs-keyword">block</span> example:
<span class="hljs-keyword">let</span>
timestamp = <span class="hljs-number">939</span>
buses = <span class="hljs-string">"7,13,x,x,59,x,31,19"</span>.replace(<span class="hljs-string">",x"</span>, <span class="hljs-string">""</span>).split(<span class="hljs-string">","</span>).map(parseInt)
solve1
<span class="hljs-keyword">block</span> part1:
<span class="hljs-keyword">let</span>
timestamp = <span class="hljs-number">1003055</span>
buses = <span class="hljs-string">"37,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,41,x,x,x,x,x,x,x,x,x,433,x,x,x,x,x,x,x,23,x,x,x,x,x,x,x,x,17,x,19,x,x,x,x,x,x,x,x,x,29,x,593,x,x,x,x,x,x,x,x,x,x,x,x,13"</span>.replace(
<span class="hljs-string">",x"</span>, <span class="hljs-string">""</span>).split(<span class="hljs-string">","</span>).map(parseInt)
solve1</code></pre>
<pre><samp>@[7, 13, 59, 31, 19]
7: 6
13: 10
59: 5
31: 22
19: 11
(59, 5, 295)
@[37, 41, 433, 23, 17, 19, 29, 593, 13]
37: 15
41: 10
433: 206
23: 21
17: 13
19: 12
29: 26
593: 301
13: 12
(41, 10, 410)</samp></pre>
<p>That's the right answer! You are <em class="star">one gold star</em> closer to saving your vacation.</p>
<h3>Part 2</h3>
<p>As mentioned I need Extended Euclidean Algorithm. I went with a rather straightforward implementation.
I do not think my <code>if</code> and <code>return</code> here do conform to
<a href="https://gist.github.com/disruptek/6d0cd6774d05adaa894db4deb646fc1d#miscellaneous">disruptek's tips and tricks</a>.</p>
<p>I did have some debugging to do, and Wikipedia's worked example was very useful</p>
<pre><code class="nim hljs"><span class="hljs-keyword">type</span>
<span class="hljs-type">Eq</span> = <span class="hljs-keyword">tuple</span>[modulus, remainder: <span class="hljs-built_in">int</span>]
<span class="hljs-keyword">func</span> extEuclid(n1, n2: <span class="hljs-built_in">int</span>): (<span class="hljs-built_in">int</span>, <span class="hljs-built_in">int</span>, <span class="hljs-built_in">int</span>) =
<span class="hljs-keyword">let</span> (a, b) = <span class="hljs-keyword">if</span> n1 > n2:
(n1, n2) <span class="hljs-keyword">else</span>:
(n2, n1)
<span class="hljs-keyword">var</span>
r0 = a
r1 = b
s0 = <span class="hljs-number">1</span>
s1 = <span class="hljs-number">0</span>
t0 = <span class="hljs-number">0</span>
t1 = <span class="hljs-number">1</span>
q1 = r0 <span class="hljs-keyword">div</span> r1
r2 = r0 - q1 * r1
s2 = s0 - q1 * s1
t2 = t0 - q1 * t1
<span class="hljs-keyword">when</span> <span class="hljs-literal">false</span>:
dump (r0, r1, r2)
dump q1
dump (s0, s1, s2)
dump (t0, t1, t2)
<span class="hljs-keyword">while</span> r2 != <span class="hljs-number">0</span>:
<span class="hljs-keyword">when</span> <span class="hljs-literal">false</span>:
debugecho <span class="hljs-string">"iterate:"</span>
r0 = r1
r1 = r2
s0 = s1
s1 = s2
t0 = t1
t1 = t2
q1 = r0 <span class="hljs-keyword">div</span> r1
r2 = r0 - q1 * r1
s2 = s0 - q1 * s1
t2 = t0 - q1 * t1
<span class="hljs-keyword">when</span> <span class="hljs-literal">false</span>:
dump (r0, r1, r2)
dump q1
dump (s0, s1, s2)
dump (t0, t1, t2)
<span class="hljs-keyword">return</span> <span class="hljs-keyword">if</span> n1 > n2:
(r1, s1, t1) <span class="hljs-keyword">else</span>:
(r1, t1, s1)
<span class="hljs-keyword">echo</span> extEuclid(<span class="hljs-number">240</span>, <span class="hljs-number">46</span>) <span class="hljs-comment">## (2, -9, 47)</span></code></pre>
<pre><samp>(2, -9, 47)</samp></pre>
<p>my CRT solution algorithm did ran into overflows and I applied a patch inside using bigints</p>
<pre><code class="nim hljs"><span class="hljs-keyword">import</span>
bigints
<span class="hljs-comment">## why can it have side effects?</span>
<span class="hljs-keyword">proc</span> crt(eqs: <span class="hljs-built_in">seq</span>[<span class="hljs-type">Eq</span>]): <span class="hljs-built_in">int</span> =
<span class="hljs-keyword">if</span> eqs.len == <span class="hljs-number">1</span>:
<span class="hljs-keyword">return</span> eqs[<span class="hljs-number">0</span>].remainder
<span class="hljs-keyword">if</span> eqs.len == <span class="hljs-number">2</span>:
<span class="hljs-keyword">let</span>
(n1, a1) = eqs[<span class="hljs-number">0</span>]
(n2, a2) = eqs[<span class="hljs-number">1</span>]
(gcd, m1, m2) = extEuclid(n1, n2)
<span class="hljs-keyword">if</span> gcd != <span class="hljs-number">1</span>:
<span class="hljs-keyword">echo</span> <span class="hljs-string">"ERROR n1, n2 not coprime: "</span>, (n1, n2, gcd)
<span class="hljs-comment">## gives me overflow here but result is good for int64. let's just patch this part with bigints:</span>
<span class="hljs-keyword">let</span>
ba1 = a1.initBigInt
bm2 = m2.initBigInt
bn2 = n2.initBigInt
ba2 = a2.initBigInt
bm1 = m1.initBigInt
bn1 = n1.initBigInt
bres = (ba1 * bm2 * bn2 + ba2 * bm1 * bn1) <span class="hljs-keyword">mod</span> (bn1 * bn2)
<span class="hljs-literal">result</span> = parseInt($bres)
<span class="hljs-keyword">return</span> <span class="hljs-literal">result</span>
<span class="hljs-keyword">let</span>
a12 = crt(eqs[<span class="hljs-number">0</span> .. <span class="hljs-number">1</span>])
n12 = eqs[<span class="hljs-number">0</span>].modulus * eqs[<span class="hljs-number">1</span>].modulus
<span class="hljs-keyword">var</span> eqs = @[(n12, a12)] & eqs[<span class="hljs-number">2</span> .. eqs.<span class="hljs-keyword">high</span>]
<span class="hljs-keyword">return</span> crt(eqs)
dump crt(@[(<span class="hljs-number">3</span>, <span class="hljs-number">2</span>), (<span class="hljs-number">5</span>, <span class="hljs-number">3</span>), (<span class="hljs-number">7</span>, <span class="hljs-number">2</span>)]) <span class="hljs-comment">## from sun-tzu: 23</span>
dump crt(@[(<span class="hljs-number">3</span>, <span class="hljs-number">0</span>), (<span class="hljs-number">4</span>, <span class="hljs-number">3</span>), (<span class="hljs-number">5</span>, <span class="hljs-number">4</span>)]) <span class="hljs-comment">## wiki: 39</span></code></pre>
<pre><samp>crt(@[(3, 2), (5, 3), (7, 2)]) = 23
crt(@[(3, 0), (4, 3), (5, 4)]) = 39</samp></pre>
<p>Once I got both implementations right I applied to the example
(first manually parsed than I wrote the parse which also preprocesses stuff).
Of course here the good strategy is, while parsing, to keep track of position and add remainder to my data.</p>
<pre><code class="nim hljs"><span class="hljs-keyword">let</span>
<span class="hljs-comment">## buses = "7,13,x,x,59,x,31,19"</span>
example2 = @[(<span class="hljs-number">7</span>, <span class="hljs-number">0</span>), (<span class="hljs-number">13</span>, <span class="hljs-number">13</span> - <span class="hljs-number">1</span>), (<span class="hljs-number">59</span>, <span class="hljs-number">59</span> - <span class="hljs-number">4</span>), (<span class="hljs-number">31</span>, <span class="hljs-number">31</span> - <span class="hljs-number">6</span>), (<span class="hljs-number">19</span>, <span class="hljs-number">19</span> - <span class="hljs-number">7</span>)]
<span class="hljs-keyword">echo</span> crt(example2)
<span class="hljs-keyword">proc</span> parse(text: <span class="hljs-built_in">string</span>): <span class="hljs-built_in">seq</span>[<span class="hljs-type">Eq</span>] =
<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> n <span class="hljs-keyword">in</span> text.split(<span class="hljs-string">","</span>):
<span class="hljs-keyword">if</span> n != <span class="hljs-string">"x"</span>:
<span class="hljs-literal">result</span>.add (parseInt(n), i)
inc i
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">1</span> .. <span class="hljs-literal">result</span>.<span class="hljs-keyword">high</span>:
<span class="hljs-literal">result</span>[i][<span class="hljs-number">1</span>] = <span class="hljs-literal">result</span>[i][<span class="hljs-number">0</span>] - <span class="hljs-literal">result</span>[i][<span class="hljs-number">1</span>]
<span class="hljs-keyword">echo</span> parse <span class="hljs-string">"7,13,x,x,59,x,31,19"</span></code></pre>
<pre><samp>1068781
@[(modulus: 7, remainder: 0), (modulus: 13, remainder: 12), (modulus: 59, remainder: 55), (modulus: 31, remainder: 25), (modulus: 19, remainder: 12)]</samp></pre>
<p>to make sure at least the solution was below int.high I did print my expected upper bound. Ah, I did assume all modulus were coprime and I actually suspect they are all prime but did not check</p>
<pre><code class="nim hljs"><span class="hljs-keyword">let</span> part2input = parse <span class="hljs-string">"37,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,41,x,x,x,x,x,x,x,x,x,433,x,x,x,x,x,x,x,23,x,x,x,x,x,x,x,x,17,x,19,x,x,x,x,x,x,x,x,x,29,x,593,x,x,x,x,x,x,x,x,x,x,x,x,13"</span>
<span class="hljs-keyword">echo</span> part2input
<span class="hljs-keyword">var</span> n = <span class="hljs-number">1</span>
<span class="hljs-keyword">for</span> eq <span class="hljs-keyword">in</span> part2input:
n *= eq.modulus
<span class="hljs-keyword">echo</span> n
<span class="hljs-keyword">echo</span> <span class="hljs-built_in">int</span>.<span class="hljs-keyword">high</span>
<span class="hljs-keyword">echo</span> <span class="hljs-string">"part2: "</span>, crt(part2input)</code></pre>
<pre><samp>@[(modulus: 37, remainder: 0), (modulus: 41, remainder: 14), (modulus: 433, remainder: 396), (modulus: 23, remainder: -22), (modulus: 17, remainder: -37), (modulus: 19, remainder: -37), (modulus: 29, remainder: -37), (modulus: 593, remainder: 525), (modulus: 13, remainder: -68)]
37
1517
656861
15107803
256832651
4879820369
141514790701
83918270885693
1090937521514009
9223372036854775807
part2: 600691418730595</samp></pre>
<p>That's the right answer! You are <em class="star">one gold star</em> closer to saving your vacation.</p>
</main>
<footer>
<hr>
<div class="nb-box">
<span><span class="nb-small">made with <a href="https://pietroppeter.github.io/nimib/">nimib π³</a></span></span>
<span></span>
<span><button class="nb-small" id="show" onclick="toggleSourceDisplay()">Show Source</button></span>
</div>
</footer>
<section id="source">
<pre><code class="nim hljs"><span class="hljs-keyword">import</span> nimib, animu, nimoji
nbInit
nbText: <span class="hljs-string">"""## 2020, day 13: Shuttle Search :mag: :rocket:
Today was another mathematical day and the main character was the
[Chinese Remainder Theorem](https://en.wikipedia.org/wiki/Chinese_remainder_theorem) (CRT from now on).
Even though the day was rather straightforward for everyone which was able to recall that,
I find still very nice the fact that people could (and did) come up with very good solutions
without knowing about CRT. In particular most people who did solved the problem without knowing about CRT
did implement a very nice method to find solutions,
[search by sieving](https://en.wikipedia.org/wiki/Chinese_remainder_theorem#Search_by_sieving).
The CRT did click from me rightaway, so I did decide not to look for existing implementation
and decided to use the coefficient given by BΓ©zout's identity, that can be computed with
[Extended Euclidean Algorithm](https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm).
I also knew beforehand that I might run into overflow issues and I might need to use
[bigints](https://github.com/def-/nim-bigints). I decided to first implement with standard `int`s and only
later upgrade to bigints if needed.
In the end I found out that the solution was below `int.high` but my specific methods using BΓ©zout's coefficients
did ran into an overflow, so I just applied a quick patch without changing proc's signature.
In the end I think that the solution with the sieve is the most elegant and practical, since it would not need this.
"""</span>.emojize
nbCode:
<span class="hljs-keyword">import</span> strutils, sequtils
<span class="hljs-keyword">import</span> sugar
nbText: <span class="hljs-string">"""part 1 was rather straightforward. I solved first for the example then wrapped that in a template
and used it also to solve it for the real input. No reading from file this, just parsing directly in source and also very basic parsing."""</span>
nbCode:
<span class="hljs-keyword">template</span> solve1 =
<span class="hljs-keyword">echo</span> $buses <span class="hljs-comment">## why nimsuggest complains when no $?</span>
<span class="hljs-keyword">var</span>
id, wait, minWait: <span class="hljs-built_in">int</span>
minWait = <span class="hljs-built_in">int</span>.<span class="hljs-keyword">high</span>
<span class="hljs-keyword">for</span> bus <span class="hljs-keyword">in</span> buses:
wait = bus - (timestamp <span class="hljs-keyword">mod</span> bus)
<span class="hljs-keyword">echo</span> bus, <span class="hljs-string">": "</span>, wait
<span class="hljs-keyword">if</span> wait < minWait:
minWait = wait
id = bus
<span class="hljs-keyword">echo</span> (id, minWait, id*minWait)
<span class="hljs-keyword">block</span> example:
<span class="hljs-keyword">let</span>
timestamp = <span class="hljs-number">939</span>
buses = <span class="hljs-string">"7,13,x,x,59,x,31,19"</span>.replace(<span class="hljs-string">",x"</span>, <span class="hljs-string">""</span>).split(<span class="hljs-string">","</span>).map(parseInt)
solve1
<span class="hljs-keyword">block</span> part1:
<span class="hljs-keyword">let</span>
timestamp = <span class="hljs-number">1003055</span>
buses = <span class="hljs-string">"37,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,41,x,x,x,x,x,x,x,x,x,433,x,x,x,x,x,x,x,23,x,x,x,x,x,x,x,x,17,x,19,x,x,x,x,x,x,x,x,x,29,x,593,x,x,x,x,x,x,x,x,x,x,x,x,13"</span>.replace(<span class="hljs-string">",x"</span>, <span class="hljs-string">""</span>).split(<span class="hljs-string">","</span>).map(parseInt)
solve1
gotTheStar
nbText: <span class="hljs-string">"""### Part 2
As mentioned I need Extended Euclidean Algorithm. I went with a rather straightforward implementation.
I do not think my `if` and `return` here do conform to
[disruptek's tips and tricks](https://gist.github.com/disruptek/6d0cd6774d05adaa894db4deb646fc1d#miscellaneous).
I did have some debugging to do, and Wikipedia's worked example was very useful
"""</span>
nbCode:
<span class="hljs-keyword">type</span> <span class="hljs-type">Eq</span> = <span class="hljs-keyword">tuple</span>[modulus, remainder: <span class="hljs-built_in">int</span>]
<span class="hljs-keyword">func</span> extEuclid(n1, n2: <span class="hljs-built_in">int</span>): (<span class="hljs-built_in">int</span>, <span class="hljs-built_in">int</span>, <span class="hljs-built_in">int</span>) =
<span class="hljs-keyword">let</span> (a,b) = <span class="hljs-keyword">if</span> n1 > n2:
(n1, n2)
<span class="hljs-keyword">else</span>:
(n2, n1)
<span class="hljs-keyword">var</span>
<span class="hljs-comment"># intialize</span>
r0 = a
r1 = b
s0 = <span class="hljs-number">1</span>
s1 = <span class="hljs-number">0</span>
t0 = <span class="hljs-number">0</span>
t1 = <span class="hljs-number">1</span>
<span class="hljs-comment"># iterate</span>
q1 = r0 <span class="hljs-keyword">div</span> r1
r2 = r0 - q1*r1 <span class="hljs-comment"># = r0 mod r1</span>
s2 = s0 - q1*s1
t2 = t0 - q1*t1
<span class="hljs-keyword">when</span> <span class="hljs-literal">false</span>:
dump (r0, r1, r2)
dump q1
dump (s0, s1, s2)
dump (t0, t1, t2)
<span class="hljs-keyword">while</span> r2 != <span class="hljs-number">0</span>:
<span class="hljs-keyword">when</span> <span class="hljs-literal">false</span>: debugecho <span class="hljs-string">"iterate:"</span>
<span class="hljs-comment"># shift</span>
r0 = r1
r1 = r2
s0 = s1
s1 = s2
t0 = t1
t1 = t2
<span class="hljs-comment"># iterate</span>
q1 = r0 <span class="hljs-keyword">div</span> r1
r2 = r0 - q1*r1 <span class="hljs-comment"># = r0 mod r1</span>
s2 = s0 - q1*s1
t2 = t0 - q1*t1
<span class="hljs-keyword">when</span> <span class="hljs-literal">false</span>:
dump (r0, r1, r2)
dump q1
dump (s0, s1, s2)
dump (t0, t1, t2)
<span class="hljs-keyword">return</span> <span class="hljs-keyword">if</span> n1 > n2:
(r1, s1, t1)
<span class="hljs-keyword">else</span>:
(r1, t1, s1)
<span class="hljs-keyword">echo</span> extEuclid(<span class="hljs-number">240</span>, <span class="hljs-number">46</span>) <span class="hljs-comment">## (2, -9, 47)</span>
nbText: <span class="hljs-string">"my CRT solution algorithm did ran into overflows and I applied a patch inside using bigints"</span>
nbCode:
<span class="hljs-keyword">import</span> bigints
<span class="hljs-comment">## why can it have side effects?</span>
<span class="hljs-keyword">proc</span> crt(eqs: <span class="hljs-built_in">seq</span>[<span class="hljs-type">Eq</span>]): <span class="hljs-built_in">int</span> =
<span class="hljs-keyword">if</span> eqs.len == <span class="hljs-number">1</span>:
<span class="hljs-keyword">return</span> eqs[<span class="hljs-number">0</span>].remainder
<span class="hljs-keyword">if</span> eqs.len == <span class="hljs-number">2</span>:
<span class="hljs-comment">#dump eqs</span>
<span class="hljs-keyword">let</span>
(n1, a1) = eqs[<span class="hljs-number">0</span>]
(n2, a2) = eqs[<span class="hljs-number">1</span>]
(gcd, m1, m2) = extEuclid(n1, n2)
<span class="hljs-keyword">if</span> gcd != <span class="hljs-number">1</span>:
<span class="hljs-keyword">echo</span> <span class="hljs-string">"ERROR n1, n2 not coprime: "</span>, (n1, n2, gcd)
<span class="hljs-comment">## gives me overflow here but result is good for int64. let's just patch this part with bigints:</span>
<span class="hljs-keyword">let</span>
ba1 = a1.initBigInt
bm2 = m2.initBigInt
bn2 = n2.initBigInt
ba2 = a2.initBigInt
bm1 = m1.initBigInt
bn1 = n1.initBigInt
bres = (ba1*bm2*bn2 + ba2*bm1*bn1) <span class="hljs-keyword">mod</span> (bn1*bn2)
<span class="hljs-literal">result</span> = parseInt($bres)
<span class="hljs-keyword">return</span> <span class="hljs-literal">result</span>
<span class="hljs-keyword">let</span>
a12 = crt(eqs[<span class="hljs-number">0.</span>.<span class="hljs-number">1</span>])
n12 = eqs[<span class="hljs-number">0</span>].modulus*eqs[<span class="hljs-number">1</span>].modulus
<span class="hljs-comment">#dump (n12, a12)</span>
<span class="hljs-keyword">var</span> eqs = @[(n12, a12)] & eqs[<span class="hljs-number">2</span> .. eqs.<span class="hljs-keyword">high</span>]
<span class="hljs-keyword">return</span> crt(eqs)
dump crt(@[(<span class="hljs-number">3</span>, <span class="hljs-number">2</span>), (<span class="hljs-number">5</span>, <span class="hljs-number">3</span>), (<span class="hljs-number">7</span>, <span class="hljs-number">2</span>)]) <span class="hljs-comment">## from sun-tzu: 23</span>
dump crt(@[(<span class="hljs-number">3</span>, <span class="hljs-number">0</span>), (<span class="hljs-number">4</span>, <span class="hljs-number">3</span>), (<span class="hljs-number">5</span>, <span class="hljs-number">4</span>)]) <span class="hljs-comment">## wiki: 39</span>
nbText: <span class="hljs-string">"""Once I got both implementations right I applied to the example
(first manually parsed than I wrote the parse which also preprocesses stuff).
Of course here the good strategy is, while parsing, to keep track of position and add remainder to my data.
"""</span>
nbCode:
<span class="hljs-keyword">let</span>
<span class="hljs-comment">## buses = "7,13,x,x,59,x,31,19"</span>
example2 = @[(<span class="hljs-number">7</span>, <span class="hljs-number">0</span>),(<span class="hljs-number">13</span>, <span class="hljs-number">13</span> - <span class="hljs-number">1</span>),(<span class="hljs-number">59</span>, <span class="hljs-number">59</span> - <span class="hljs-number">4</span>),(<span class="hljs-number">31</span>, <span class="hljs-number">31</span> - <span class="hljs-number">6</span>),(<span class="hljs-number">19</span>, <span class="hljs-number">19</span> - <span class="hljs-number">7</span>)]
<span class="hljs-keyword">echo</span> crt(example2) <span class="hljs-comment"># 1068781</span>
<span class="hljs-keyword">proc</span> parse(text: <span class="hljs-built_in">string</span>): <span class="hljs-built_in">seq</span>[<span class="hljs-type">Eq</span>] =
<span class="hljs-keyword">var</span> i = <span class="hljs-number">0</span>
<span class="hljs-keyword">for</span> n <span class="hljs-keyword">in</span> text.split(<span class="hljs-string">","</span>):
<span class="hljs-keyword">if</span> n != <span class="hljs-string">"x"</span>:
<span class="hljs-literal">result</span>.add (parseInt(n), i)
inc i
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">1.</span>.<span class="hljs-literal">result</span>.<span class="hljs-keyword">high</span>:
<span class="hljs-literal">result</span>[i][<span class="hljs-number">1</span>] = <span class="hljs-literal">result</span>[i][<span class="hljs-number">0</span>] - <span class="hljs-literal">result</span>[i][<span class="hljs-number">1</span>]
<span class="hljs-keyword">echo</span> parse <span class="hljs-string">"7,13,x,x,59,x,31,19"</span>
nbText: <span class="hljs-string">"to make sure at least the solution was below int.high I did print my expected upper bound. Ah, I did assume all modulus were coprime and I actually suspect they are all prime but did not check"</span>
nbCode:
<span class="hljs-keyword">let</span> part2input = parse <span class="hljs-string">"37,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,41,x,x,x,x,x,x,x,x,x,433,x,x,x,x,x,x,x,23,x,x,x,x,x,x,x,x,17,x,19,x,x,x,x,x,x,x,x,x,29,x,593,x,x,x,x,x,x,x,x,x,x,x,x,13"</span>
<span class="hljs-keyword">echo</span> part2input
<span class="hljs-keyword">var</span> n = <span class="hljs-number">1</span>
<span class="hljs-keyword">for</span> eq <span class="hljs-keyword">in</span> part2input:
n *= eq.modulus
<span class="hljs-keyword">echo</span> n
<span class="hljs-keyword">echo</span> <span class="hljs-built_in">int</span>.<span class="hljs-keyword">high</span>
<span class="hljs-keyword">echo</span> <span class="hljs-string">"part2: "</span>, crt(part2input)
gotTheStar
nbSave</code></pre>
</section><script>
function toggleSourceDisplay() {
var btn = document.getElementById("show")
var source = document.getElementById("source");
if (btn.innerHTML=="Show Source") {
btn.innerHTML = "Hide Source";
source.style.display = "block";
} else {
btn.innerHTML = "Show Source";
source.style.display = "none";
}
}
</script></body>
</html>