-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathday12.html
453 lines (411 loc) Β· 25.4 KB
/
day12.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
<!DOCTYPE html>
<html lang="en-us">
<head>
<title>2020\day12.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\day12.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 12: Rain risk β β΄</h2>
<p>Another classic AOC in moving according to directions.
Nice change of specs from part1 to part2,
but I just added a proc overload and I was good to go
(although my "legacy" Enum names do not really shine in part2).
I did implement the bare minimum (while being my usual verbose,
I am sure other people have much more synthetic solutions)
and got away with it very quick.</p>
<p>As for imports today is just one. Yep <code>parseEnum</code> and <code>parseInt</code>
are in <code>strutils</code> and not in <code>parseutils</code>.
<code>parseutils</code> contains parse functions that return an <code>int</code>.</p>
<pre><code class="nim hljs"><span class="hljs-keyword">import</span>
strutils</code></pre>
<p>All my types below. Using Vec2 both for pos and dir although I could go fancy and use a distinct UnitVec2</p>
<pre><code class="nim hljs"><span class="hljs-keyword">type</span>
<span class="hljs-type">Vec2</span> = <span class="hljs-keyword">tuple</span>[x, y: <span class="hljs-built_in">int</span>]
<span class="hljs-type">Ship</span> = <span class="hljs-keyword">object</span>
pos: <span class="hljs-type">Vec2</span>
dir: <span class="hljs-type">Vec2</span> <span class="hljs-comment">## it could be a distinct type (UnitVec2)</span>
<span class="hljs-type">InstructionKind</span> = <span class="hljs-keyword">enum</span>
goNorth = <span class="hljs-string">"N"</span>, goEast = <span class="hljs-string">"E"</span>, goSouth = <span class="hljs-string">"S"</span>, goWest = <span class="hljs-string">"W"</span>, goForward = <span class="hljs-string">"F"</span>,
rotLeft = <span class="hljs-string">"L"</span>, rotRight = <span class="hljs-string">"R"</span> <span class="hljs-comment">## here I am breaking from naming convention of enums</span>
<span class="hljs-type">Instruction</span> = <span class="hljs-keyword">object</span>
kind: <span class="hljs-type">InstructionKind</span>
val: <span class="hljs-built_in">int</span></code></pre>
<p>template will be useful between example and input and also for part2</p>
<pre><code class="nim hljs"><span class="hljs-keyword">var</span> ship: <span class="hljs-type">Ship</span>
<span class="hljs-keyword">template</span> resetShip() =
ship.pos = (<span class="hljs-number">0</span>, <span class="hljs-number">0</span>)
ship.dir = (<span class="hljs-number">1</span>, <span class="hljs-number">0</span>)
resetShip</code></pre>
<p>no need to come up with fancy names. <code>parse</code> is fine.</p>
<pre><code class="nim hljs"><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">Instruction</span>] =
<span class="hljs-keyword">var</span> instr: <span class="hljs-type">Instruction</span>
<span class="hljs-keyword">for</span> line <span class="hljs-keyword">in</span> text.splitLines:
instr.kind = parseEnum[<span class="hljs-type">InstructionKind</span>](line[<span class="hljs-number">0</span> .. <span class="hljs-number">0</span>])
instr.val = parseInt(line[<span class="hljs-number">1</span> .. ^<span class="hljs-number">1</span>])
<span class="hljs-literal">result</span>.add instr
<span class="hljs-keyword">let</span> example = <span class="hljs-string">"""F10
N3
F7
R90
F11"""</span>
<span class="hljs-keyword">echo</span> parse example</code></pre>
<pre><samp>@[(kind: F, val: 10), (kind: N, val: 3), (kind: F, val: 7), (kind: R, val: 90), (kind: F, val: 11)]</samp></pre>
<p>my set of functions for Vec2, I will support only left rotation...</p>
<pre><code class="nim hljs"><span class="hljs-keyword">func</span> `+`(v, w: <span class="hljs-type">Vec2</span>): <span class="hljs-type">Vec2</span> =
(v.x + w.x, v.y + w.y)
<span class="hljs-keyword">func</span> `*`(s: <span class="hljs-built_in">int</span>; v: <span class="hljs-type">Vec2</span>): <span class="hljs-type">Vec2</span> =
(s * v.x, s * v.y)
<span class="hljs-keyword">func</span> rotL(v: <span class="hljs-type">Vec2</span>; deg: <span class="hljs-built_in">int</span>): <span class="hljs-type">Vec2</span> =
<span class="hljs-keyword">case</span> deg
<span class="hljs-keyword">of</span> <span class="hljs-number">180</span>, -<span class="hljs-number">180</span>:
(-v.x, -v.y)
<span class="hljs-keyword">of</span> <span class="hljs-number">90</span>, -<span class="hljs-number">270</span>:
(-v.y, v.x) <span class="hljs-comment">## (1, 0) -> (0, 1) -> (-1, 0)</span>
<span class="hljs-keyword">of</span> -<span class="hljs-number">90</span>, <span class="hljs-number">270</span>:
(v.y, -v.x) <span class="hljs-comment">## (1, 0) -> (0, -1) -> (-1, 0)</span>
<span class="hljs-keyword">else</span>:
debugEcho <span class="hljs-string">"ERROR not supported"</span>
(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>)
<span class="hljs-keyword">template</span> `+=`(a, b: <span class="hljs-built_in">untyped</span>) =
a = a + b
<span class="hljs-keyword">func</span> manhattan(ship: <span class="hljs-type">Ship</span>): <span class="hljs-built_in">int</span> =
<span class="hljs-comment">## I could do it for a Vec2 but I will use it only for the ship, so...</span>
abs(ship.pos.x) + abs(ship.pos.y)</code></pre>
<p>heart of the solution is this, rather straightforward now:</p>
<pre><code class="nim hljs"><span class="hljs-keyword">proc</span> move(ship: <span class="hljs-keyword">var</span> <span class="hljs-type">Ship</span>; instr: <span class="hljs-type">Instruction</span>) =
<span class="hljs-keyword">case</span> instr.kind
<span class="hljs-keyword">of</span> goForward:
ship.pos += instr.val * ship.dir
<span class="hljs-keyword">of</span> goNorth:
ship.pos += instr.val * (<span class="hljs-number">0</span>, <span class="hljs-number">1</span>)
<span class="hljs-keyword">of</span> goEast:
ship.pos += instr.val * (<span class="hljs-number">1</span>, <span class="hljs-number">0</span>)
<span class="hljs-keyword">of</span> goSouth:
ship.pos += instr.val * (<span class="hljs-number">0</span>, -<span class="hljs-number">1</span>)
<span class="hljs-keyword">of</span> goWest:
ship.pos += instr.val * (-<span class="hljs-number">1</span>, <span class="hljs-number">0</span>)
<span class="hljs-keyword">of</span> rotLeft:
ship.dir = ship.dir.rotL(instr.val)
<span class="hljs-keyword">of</span> rotRight:
ship.dir = ship.dir.rotL(-instr.val)</code></pre>
<p>Let's move according to example instructions, printing out in case I need to debug</p>
<pre><code class="nim hljs"><span class="hljs-keyword">echo</span> ship
<span class="hljs-keyword">for</span> instr <span class="hljs-keyword">in</span> parse(example):
ship.move instr
<span class="hljs-keyword">echo</span> <span class="hljs-string">"-> "</span>, instr
<span class="hljs-keyword">echo</span> ship
<span class="hljs-keyword">echo</span> manhattan ship <span class="hljs-comment">## 25</span></code></pre>
<pre><samp>(pos: (x: 0, y: 0), dir: (x: 1, y: 0))
-> (kind: F, val: 10)
(pos: (x: 10, y: 0), dir: (x: 1, y: 0))
-> (kind: N, val: 3)
(pos: (x: 10, y: 3), dir: (x: 1, y: 0))
-> (kind: F, val: 7)
(pos: (x: 17, y: 3), dir: (x: 1, y: 0))
-> (kind: R, val: 90)
(pos: (x: 17, y: 3), dir: (x: 0, y: -1))
-> (kind: F, val: 11)
(pos: (x: 17, y: -8), dir: (x: 0, y: -1))
25</samp></pre>
<p>all good, now the input!</p>
<pre><code class="nim hljs"><span class="hljs-keyword">let</span> input = <span class="hljs-string">"2020/input12.txt"</span>.readFile
resetShip
<span class="hljs-keyword">for</span> instr <span class="hljs-keyword">in</span> parse(input):
ship.move instr
<span class="hljs-keyword">echo</span> manhattan ship <span class="hljs-comment">## 1601</span></code></pre>
<pre><samp>1601</samp></pre>
<p>That's the right answer! You are <em class="star">one gold star</em> closer to saving your vacation.</p>
<h3>Part2</h3>
<p>I just add a waypoint var and add overload for <code>move</code>:</p>
<pre><code class="nim hljs"><span class="hljs-keyword">var</span> waypoint: <span class="hljs-type">Vec2</span>
<span class="hljs-keyword">template</span> resetWaypoint() =
waypoint = (<span class="hljs-number">10</span>, <span class="hljs-number">1</span>)
resetWaypoint
<span class="hljs-keyword">proc</span> move(ship: <span class="hljs-keyword">var</span> <span class="hljs-type">Ship</span>; waypoint: <span class="hljs-keyword">var</span> <span class="hljs-type">Vec2</span>; instr: <span class="hljs-type">Instruction</span>) =
<span class="hljs-keyword">case</span> instr.kind
<span class="hljs-keyword">of</span> goForward:
ship.pos += instr.val * waypoint
<span class="hljs-keyword">of</span> goNorth:
waypoint += instr.val * (<span class="hljs-number">0</span>, <span class="hljs-number">1</span>)
<span class="hljs-keyword">of</span> goEast:
waypoint += instr.val * (<span class="hljs-number">1</span>, <span class="hljs-number">0</span>)
<span class="hljs-keyword">of</span> goSouth:
waypoint += instr.val * (<span class="hljs-number">0</span>, -<span class="hljs-number">1</span>)
<span class="hljs-keyword">of</span> goWest:
waypoint += instr.val * (-<span class="hljs-number">1</span>, <span class="hljs-number">0</span>)
<span class="hljs-keyword">of</span> rotLeft:
waypoint = waypoint.rotL(instr.val)
<span class="hljs-keyword">of</span> rotRight:
waypoint = waypoint.rotL(-instr.val)
resetShip
<span class="hljs-keyword">echo</span> ship
<span class="hljs-keyword">for</span> instr <span class="hljs-keyword">in</span> parse(example):
ship.move(waypoint, instr)
<span class="hljs-keyword">echo</span> <span class="hljs-string">"W: "</span>, waypoint
<span class="hljs-keyword">echo</span> <span class="hljs-string">"-> "</span>, instr
<span class="hljs-keyword">echo</span> ship
<span class="hljs-keyword">echo</span> manhattan ship
resetShip
resetWaypoint
<span class="hljs-keyword">for</span> instr <span class="hljs-keyword">in</span> parse(input):
ship.move(waypoint, instr)
<span class="hljs-keyword">echo</span> manhattan ship</code></pre>
<pre><samp>(pos: (x: 0, y: 0), dir: (x: 1, y: 0))
W: (x: 10, y: 1)
-> (kind: F, val: 10)
(pos: (x: 100, y: 10), dir: (x: 1, y: 0))
W: (x: 10, y: 4)
-> (kind: N, val: 3)
(pos: (x: 100, y: 10), dir: (x: 1, y: 0))
W: (x: 10, y: 4)
-> (kind: F, val: 7)
(pos: (x: 170, y: 38), dir: (x: 1, y: 0))
W: (x: 4, y: -10)
-> (kind: R, val: 90)
(pos: (x: 170, y: 38), dir: (x: 1, y: 0))
W: (x: 4, y: -10)
-> (kind: F, val: 11)
(pos: (x: 214, y: -72), dir: (x: 1, y: 0))
286
13340</samp></pre>
<p>That's the right answer! You are <em class="star">one gold star</em> closer to saving your vacation.</p>
<p>I was a bit nervous about this day 12 since last two years day 12 is where
I started to lag behind.
Instead it was a piece of cake π° and my fastest β‘ submission with no bugs π.</p>
<p>I probably am becoming a little better at this, but I do have a feeling like
this year is slightly easier, maybe Eric is cutting us some slack because of, you know, 2020...</p>
<p>Of course I will regret just saying this tomorrow and the next days... π±</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 12: Rain risk :umbrella: :ferry:
Another classic AOC in moving according to directions.
Nice change of specs from part1 to part2,
but I just added a proc overload and I was good to go
(although my "legacy" Enum names do not really shine in part2).
I did implement the bare minimum (while being my usual verbose,
I am sure other people have much more synthetic solutions)
and got away with it very quick.
As for imports today is just one. Yep `parseEnum` and `parseInt`
are in `strutils` and not in `parseutils`.
`parseutils` contains parse functions that return an `int`.
"""</span>.emojize
nbCode:
<span class="hljs-keyword">import</span> strutils
nbText: <span class="hljs-string">"All my types below. Using Vec2 both for pos and dir although I could go fancy and use a distinct UnitVec2"</span>
nbCode:
<span class="hljs-keyword">type</span>
<span class="hljs-type">Vec2</span> = <span class="hljs-keyword">tuple</span>[x, y: <span class="hljs-built_in">int</span>]
<span class="hljs-type">Ship</span> = <span class="hljs-keyword">object</span>
pos: <span class="hljs-type">Vec2</span>
dir: <span class="hljs-type">Vec2</span> <span class="hljs-comment">## it could be a distinct type (UnitVec2)</span>
<span class="hljs-type">InstructionKind</span> = <span class="hljs-keyword">enum</span>
goNorth = <span class="hljs-string">"N"</span>, goEast = <span class="hljs-string">"E"</span>, goSouth = <span class="hljs-string">"S"</span>, goWest = <span class="hljs-string">"W"</span>,
goForward = <span class="hljs-string">"F"</span>, rotLeft = <span class="hljs-string">"L"</span>, rotRight = <span class="hljs-string">"R"</span>
<span class="hljs-comment">## here I am breaking from naming convention of enums</span>
<span class="hljs-type">Instruction</span> = <span class="hljs-keyword">object</span>
kind: <span class="hljs-type">InstructionKind</span>
val: <span class="hljs-built_in">int</span>
nbText: <span class="hljs-string">"template will be useful between example and input and also for part2"</span>
nbCode:
<span class="hljs-keyword">var</span> ship: <span class="hljs-type">Ship</span>
<span class="hljs-keyword">template</span> resetShip =
ship.pos = (<span class="hljs-number">0</span>, <span class="hljs-number">0</span>)
ship.dir = (<span class="hljs-number">1</span>, <span class="hljs-number">0</span>)
resetShip
nbText: <span class="hljs-string">"no need to come up with fancy names. `parse` is fine."</span>
nbCode:
<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">Instruction</span>] =
<span class="hljs-keyword">var</span> instr: <span class="hljs-type">Instruction</span>
<span class="hljs-keyword">for</span> line <span class="hljs-keyword">in</span> text.splitLines:
instr.kind = parseEnum[<span class="hljs-type">InstructionKind</span>](line[<span class="hljs-number">0.</span>.<span class="hljs-number">0</span>])
instr.val = parseInt(line[<span class="hljs-number">1</span> .. ^<span class="hljs-number">1</span>])
<span class="hljs-literal">result</span>.add instr
<span class="hljs-keyword">let</span> example = <span class="hljs-string">"""F10
N3
F7
R90
F11"""</span>
<span class="hljs-keyword">echo</span> parse example
nbText: <span class="hljs-string">"my set of functions for Vec2, I will support only left rotation..."</span>
nbCode:
<span class="hljs-keyword">func</span> `+`(v,w: <span class="hljs-type">Vec2</span>): <span class="hljs-type">Vec2</span> = (v.x + w.x, v.y + w.y)
<span class="hljs-keyword">func</span> `*`(s: <span class="hljs-built_in">int</span>, v: <span class="hljs-type">Vec2</span>): <span class="hljs-type">Vec2</span> = (s*v.x, s*v.y)
<span class="hljs-keyword">func</span> rotL(v: <span class="hljs-type">Vec2</span>, deg: <span class="hljs-built_in">int</span>): <span class="hljs-type">Vec2</span> =
<span class="hljs-keyword">case</span> deg:
<span class="hljs-keyword">of</span> <span class="hljs-number">180</span>, -<span class="hljs-number">180</span>:
(-v.x, -v.y)
<span class="hljs-keyword">of</span> <span class="hljs-number">90</span>, -<span class="hljs-number">270</span>:
(-v.y, v.x) <span class="hljs-comment">## (1, 0) -> (0, 1) -> (-1, 0)</span>
<span class="hljs-keyword">of</span> -<span class="hljs-number">90</span>, <span class="hljs-number">270</span>:
(v.y, -v.x) <span class="hljs-comment">## (1, 0) -> (0, -1) -> (-1, 0)</span>
<span class="hljs-keyword">else</span>:
debugEcho <span class="hljs-string">"ERROR not supported"</span>
(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>)
<span class="hljs-keyword">template</span> `+=`(a, b: <span class="hljs-built_in">untyped</span>) =
a = a + b
<span class="hljs-keyword">func</span> manhattan(ship: <span class="hljs-type">Ship</span>): <span class="hljs-built_in">int</span> =
<span class="hljs-comment">## I could do it for a Vec2 but I will use it only for the ship, so...</span>
abs(ship.pos.x) + abs(ship.pos.y)
nbText: <span class="hljs-string">"heart of the solution is this, rather straightforward now:"</span>
nbCode:
<span class="hljs-keyword">proc</span> move(ship: <span class="hljs-keyword">var</span> <span class="hljs-type">Ship</span>, instr: <span class="hljs-type">Instruction</span>) =
<span class="hljs-keyword">case</span> instr.kind:
<span class="hljs-keyword">of</span> goForward:
ship.pos += instr.val*ship.dir
<span class="hljs-keyword">of</span> goNorth:
ship.pos += instr.val*(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>)
<span class="hljs-keyword">of</span> goEast:
ship.pos += instr.val*(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>)
<span class="hljs-keyword">of</span> goSouth:
ship.pos += instr.val*(<span class="hljs-number">0</span>, -<span class="hljs-number">1</span>)
<span class="hljs-keyword">of</span> goWest:
ship.pos += instr.val*(-<span class="hljs-number">1</span>, <span class="hljs-number">0</span>)
<span class="hljs-keyword">of</span> rotLeft:
ship.dir = ship.dir.rotL(instr.val)
<span class="hljs-keyword">of</span> rotRight:
ship.dir = ship.dir.rotL(-instr.val)
nbText: <span class="hljs-string">"Let's move according to example instructions, printing out in case I need to debug"</span>
nbCode:
<span class="hljs-keyword">echo</span> ship
<span class="hljs-keyword">for</span> instr <span class="hljs-keyword">in</span> parse(example):
ship.move instr
<span class="hljs-keyword">echo</span> <span class="hljs-string">"-> "</span>, instr
<span class="hljs-keyword">echo</span> ship
<span class="hljs-keyword">echo</span> manhattan ship <span class="hljs-comment">## 25</span>
nbText: <span class="hljs-string">"all good, now the input!"</span>
nbCode:
<span class="hljs-keyword">let</span> input = <span class="hljs-string">"2020/input12.txt"</span>.readFile
resetShip
<span class="hljs-keyword">for</span> instr <span class="hljs-keyword">in</span> parse(input):
ship.move instr
<span class="hljs-keyword">echo</span> manhattan ship <span class="hljs-comment">## 1601</span>
gotTheStar
nbText: <span class="hljs-string">"### Part2</span><span class="hljs-meta">\n</span><span class="hljs-string">I just add a waypoint var and add overload for `move`:"</span>
nbCode:
<span class="hljs-keyword">var</span> waypoint: <span class="hljs-type">Vec2</span>
<span class="hljs-keyword">template</span> resetWaypoint =
waypoint = (<span class="hljs-number">10</span>, <span class="hljs-number">1</span>)
resetWaypoint
<span class="hljs-keyword">proc</span> move(ship: <span class="hljs-keyword">var</span> <span class="hljs-type">Ship</span>, waypoint: <span class="hljs-keyword">var</span> <span class="hljs-type">Vec2</span>, instr: <span class="hljs-type">Instruction</span>) =
<span class="hljs-keyword">case</span> instr.kind:
<span class="hljs-keyword">of</span> goForward:
ship.pos += instr.val*waypoint
<span class="hljs-keyword">of</span> goNorth:
waypoint += instr.val*(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>)
<span class="hljs-keyword">of</span> goEast:
waypoint += instr.val*(<span class="hljs-number">1</span>, <span class="hljs-number">0</span>)
<span class="hljs-keyword">of</span> goSouth:
waypoint += instr.val*(<span class="hljs-number">0</span>, -<span class="hljs-number">1</span>)
<span class="hljs-keyword">of</span> goWest:
waypoint += instr.val*(-<span class="hljs-number">1</span>, <span class="hljs-number">0</span>)
<span class="hljs-keyword">of</span> rotLeft:
waypoint = waypoint.rotL(instr.val)
<span class="hljs-keyword">of</span> rotRight:
waypoint = waypoint.rotL(-instr.val)
resetShip
<span class="hljs-keyword">echo</span> ship
<span class="hljs-keyword">for</span> instr <span class="hljs-keyword">in</span> parse(example):
ship.move(waypoint, instr)
<span class="hljs-keyword">echo</span> <span class="hljs-string">"W: "</span>, waypoint
<span class="hljs-keyword">echo</span> <span class="hljs-string">"-> "</span>, instr
<span class="hljs-keyword">echo</span> ship
<span class="hljs-keyword">echo</span> manhattan ship
resetShip
resetWaypoint
<span class="hljs-keyword">for</span> instr <span class="hljs-keyword">in</span> parse(input):
ship.move(waypoint, instr)
<span class="hljs-keyword">echo</span> manhattan ship
gotTheStar
nbText: <span class="hljs-string">"""
I was a bit nervous about this day 12 since last two years day 12 is where
I started to lag behind.
Instead it was a piece of cake :cake: and my fastest :zap: submission with no bugs :beetle:.
I probably am becoming a little better at this, but I do have a feeling like
this year is slightly easier, maybe Eric is cutting us some slack because of, you know, 2020...
Of course I will regret just saying this tomorrow and the next days... :scream:
"""</span>.emojize
<span class="hljs-comment"># the only error I got today was when compiling after the nimib version:</span>
<span class="hljs-comment"># the usual error on parsing the example (forgot to dedent the multiline)</span>
nbSave
<span class="hljs-comment"># I probably should add to nimib something like the following:</span>
<span class="hljs-comment">#[
nbSaveCode # saves a nim file with just code
nbAddCodeSource # adds to context the content of code blocks (it should be then made available from html for easier extraction)
# in particular all code snippets should have easy pasting of code and you should also have a complete pasting for the whole code snippets
]#</span></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>