-
Notifications
You must be signed in to change notification settings - Fork 1
/
day05.html
380 lines (353 loc) Β· 17.7 KB
/
day05.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
<!DOCTYPE html>
<html lang="en-us">
<head>
<title>2020\day05.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\day05.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>
<h1>--- Day 5: Binary Boarding ---</h1>
<p>parsing is straightforward, considering that encoding is basically binary</p>
<pre><code class="nim hljs"><span class="hljs-keyword">type</span>
<span class="hljs-type">Seat</span> = <span class="hljs-built_in">int16</span>
<span class="hljs-keyword">proc</span> parseSeat(s: <span class="hljs-built_in">string</span>): <span class="hljs-type">Seat</span> =
<span class="hljs-keyword">discard</span> parseBin[<span class="hljs-built_in">int16</span>](s.multiReplace(
[(<span class="hljs-string">"F"</span>, <span class="hljs-string">"0"</span>), (<span class="hljs-string">"B"</span>, <span class="hljs-string">"1"</span>), (<span class="hljs-string">"R"</span>, <span class="hljs-string">"1"</span>), (<span class="hljs-string">"L"</span>, <span class="hljs-string">"0"</span>)]), <span class="hljs-literal">result</span>)
chk parseSeat(<span class="hljs-string">"FBFBBFFRLR"</span>), <span class="hljs-number">357</span></code></pre>
<pre><samp>[OK] parseSeat("FBFBBFFRLR") = 357</samp></pre>
<p>if I want to get back row and col (I will use it for visualization at the end)</p>
<pre><code class="nim hljs"><span class="hljs-keyword">proc</span> row(x: <span class="hljs-type">Seat</span>): <span class="hljs-built_in">int</span> =
x <span class="hljs-keyword">shr</span> <span class="hljs-number">3</span>
<span class="hljs-keyword">proc</span> col(x: <span class="hljs-type">Seat</span>): <span class="hljs-built_in">int</span> =
<span class="hljs-literal">result</span> = x.<span class="hljs-built_in">int</span>
<span class="hljs-literal">result</span>.mask(<span class="hljs-number">0b00000000000000000000000000000111</span>)
chk parseSeat(<span class="hljs-string">"FBFBBFFRLR"</span>).row, <span class="hljs-number">44</span>
chk parseSeat(<span class="hljs-string">"FBFBBFFRLR"</span>).col, <span class="hljs-number">5</span></code></pre>
<pre><samp>[OK] parseSeat("FBFBBFFRLR").row = 44
[OK] parseSeat("FBFBBFFRLR").col = 5</samp></pre>
<p><em>part1</em> is solved during parsing input file</p>
<pre><code class="nim hljs"><span class="hljs-keyword">var</span>
maxSeat = <span class="hljs-number">0</span>
seats: <span class="hljs-built_in">seq</span>[<span class="hljs-type">Seat</span>]
<span class="hljs-keyword">for</span> line <span class="hljs-keyword">in</span> <span class="hljs-string">"2020/input05.txt"</span>.lines:
seats.add parseSeat(line)
<span class="hljs-keyword">if</span> seats[^<span class="hljs-number">1</span>] > maxSeat:
maxSeat = seats[^<span class="hljs-number">1</span>]
<span class="hljs-keyword">echo</span> <span class="hljs-string">"part1: "</span>, maxSeat
chk(maxSeat, <span class="hljs-number">947</span>)</code></pre>
<pre><samp>part1: 947
[OK] maxSeat = 947</samp></pre>
<p>That's the right answer! You are <em class="star">one gold star</em> closer to saving your vacation.</p>
<p><em>part 2</em> is solved by sorting and finding the gap</p>
<pre><code class="nim hljs"><span class="hljs-keyword">var</span> mySeat: <span class="hljs-type">Seat</span>
seats.sort()
<span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> <span class="hljs-number">1</span> ..< seats.len:
<span class="hljs-keyword">if</span> seats[x] != seats[x - <span class="hljs-number">1</span>] + <span class="hljs-number">1</span>:
mySeat = seats[x] - <span class="hljs-number">1</span> <span class="hljs-comment">## first try I forgot the "- 1!</span>
<span class="hljs-keyword">break</span>
<span class="hljs-keyword">echo</span> <span class="hljs-string">"part2: "</span>, mySeat
chk(maxSeat, <span class="hljs-number">636</span>)</code></pre>
<pre><samp>part2: 636
[KO] maxSeat = 947; expected = 636</samp></pre>
<p>That's the right answer! You are <em class="star">one gold star</em> closer to saving your vacation.</p>
<p>what do we need to <em>visualize the plane</em>? I will convert Seat in (row, col)</p>
<pre><code class="nim hljs"><span class="hljs-keyword">type</span>
<span class="hljs-type">RowCol</span> = <span class="hljs-keyword">tuple</span>[row, col: <span class="hljs-built_in">int</span>]
<span class="hljs-keyword">proc</span> toPasses(s: <span class="hljs-built_in">seq</span>[<span class="hljs-type">Seat</span>]): <span class="hljs-type">HashSet</span>[<span class="hljs-type">RowCol</span>] =
<span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> s:
<span class="hljs-literal">result</span>.incl (x.row, x.col).<span class="hljs-type">RowCol</span>
<span class="hljs-keyword">proc</span> showAndTell(s: <span class="hljs-type">HashSet</span>[<span class="hljs-type">RowCol</span>]): <span class="hljs-type">RowCol</span> =
<span class="hljs-keyword">var</span> text = <span class="hljs-string">"____01234567</span><span class="hljs-meta">\n</span><span class="hljs-string">"</span>
<span class="hljs-keyword">for</span> row <span class="hljs-keyword">in</span> <span class="hljs-number">0</span> .. <span class="hljs-number">127</span>:
text.add &<span class="hljs-string">"{row:03} "</span>
<span class="hljs-keyword">for</span> col <span class="hljs-keyword">in</span> <span class="hljs-number">0</span> .. <span class="hljs-number">7</span>:
<span class="hljs-keyword">if</span> (row, col) <span class="hljs-keyword">in</span> s:
text.add <span class="hljs-string">"X"</span>
<span class="hljs-keyword">elif</span> row < <span class="hljs-number">30</span> <span class="hljs-keyword">or</span> row > <span class="hljs-number">100</span>:
text.add <span class="hljs-string">"."</span>
<span class="hljs-keyword">else</span>:
text.add <span class="hljs-string">"<em>O</em>"</span>
<span class="hljs-literal">result</span> = (row, col)
text.add <span class="hljs-string">"</span><span class="hljs-meta">\n</span><span class="hljs-string">"</span>
text.add <span class="hljs-string">"</span><span class="hljs-meta">\n</span><span class="hljs-string">"</span>
<span class="hljs-keyword">echo</span> text
<span class="hljs-keyword">let</span> myRowCol = seats.toPasses.showAndTell</code></pre>
<pre><samp>____01234567
000 ........
001 .......X
002 XXXXXXXX
003 XXXXXXXX
004 XXXXXXXX
005 XXXXXXXX
006 XXXXXXXX
007 XXXXXXXX
008 XXXXXXXX
009 XXXXXXXX
010 XXXXXXXX
011 XXXXXXXX
012 XXXXXXXX
013 XXXXXXXX
014 XXXXXXXX
015 XXXXXXXX
016 XXXXXXXX
017 XXXXXXXX
018 XXXXXXXX
019 XXXXXXXX
020 XXXXXXXX
021 XXXXXXXX
022 XXXXXXXX
023 XXXXXXXX
024 XXXXXXXX
025 XXXXXXXX
026 XXXXXXXX
027 XXXXXXXX
028 XXXXXXXX
029 XXXXXXXX
030 XXXXXXXX
031 XXXXXXXX
032 XXXXXXXX
033 XXXXXXXX
034 XXXXXXXX
035 XXXXXXXX
036 XXXXXXXX
037 XXXXXXXX
038 XXXXXXXX
039 XXXXXXXX
040 XXXXXXXX
041 XXXXXXXX
042 XXXXXXXX
043 XXXXXXXX
044 XXXXXXXX
045 XXXXXXXX
046 XXXXXXXX
047 XXXXXXXX
048 XXXXXXXX
049 XXXXXXXX
050 XXXXXXXX
051 XXXXXXXX
052 XXXXXXXX
053 XXXXXXXX
054 XXXXXXXX
055 XXXXXXXX
056 XXXXXXXX
057 XXXXXXXX
058 XXXXXXXX
059 XXXXXXXX
060 XXXXXXXX
061 XXXXXXXX
062 XXXXXXXX
063 XXXXXXXX
064 XXXXXXXX
065 XXXXXXXX
066 XXXXXXXX
067 XXXXXXXX
068 XXXXXXXX
069 XXXXXXXX
070 XXXXXXXX
071 XXXXXXXX
072 XXXXXXXX
073 XXXXXXXX
074 XXXXXXXX
075 XXXXXXXX
076 XXXXXXXX
077 XXXXXXXX
078 XXXXXXXX
079 XXXX<em>O</em>XXX
080 XXXXXXXX
081 XXXXXXXX
082 XXXXXXXX
083 XXXXXXXX
084 XXXXXXXX
085 XXXXXXXX
086 XXXXXXXX
087 XXXXXXXX
088 XXXXXXXX
089 XXXXXXXX
090 XXXXXXXX
091 XXXXXXXX
092 XXXXXXXX
093 XXXXXXXX
094 XXXXXXXX
095 XXXXXXXX
096 XXXXXXXX
097 XXXXXXXX
098 XXXXXXXX
099 XXXXXXXX
100 XXXXXXXX
101 XXXXXXXX
102 XXXXXXXX
103 XXXXXXXX
104 XXXXXXXX
105 XXXXXXXX
106 XXXXXXXX
107 XXXXXXXX
108 XXXXXXXX
109 XXXXXXXX
110 XXXXXXXX
111 XXXXXXXX
112 XXXXXXXX
113 XXXXXXXX
114 XXXXXXXX
115 XXXXXXXX
116 XXXXXXXX
117 XXXXXXXX
118 XXXX....
119 ........
120 ........
121 ........
122 ........
123 ........
124 ........
125 ........
126 ........
127 ........</samp></pre>
<p>I can check back id</p>
<pre><code class="nim hljs"><span class="hljs-keyword">proc</span> id(x: <span class="hljs-type">RowCol</span>): <span class="hljs-built_in">int</span> =
x.col + <span class="hljs-number">8</span> * x.row
chk id(myRowCol), mySeat</code></pre>
<pre><samp>[OK] id(myRowCol) = 636</samp></pre>
</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
nbInit
nbText: <span class="hljs-string">"# --- Day 5: Binary Boarding ---"</span>
nbText: <span class="hljs-string">"parsing is straightforward, considering that encoding is basically binary"</span>
nbCode:
<span class="hljs-keyword">type</span> <span class="hljs-type">Seat</span> = <span class="hljs-built_in">int16</span>
<span class="hljs-keyword">proc</span> parseSeat(s: <span class="hljs-built_in">string</span>): <span class="hljs-type">Seat</span> =
<span class="hljs-keyword">discard</span> parseBin[<span class="hljs-built_in">int16</span>](s.multiReplace([
(<span class="hljs-string">"F"</span>, <span class="hljs-string">"0"</span>), (<span class="hljs-string">"B"</span>, <span class="hljs-string">"1"</span>), (<span class="hljs-string">"R"</span>, <span class="hljs-string">"1"</span>), (<span class="hljs-string">"L"</span>, <span class="hljs-string">"0"</span>)
]), <span class="hljs-literal">result</span>)
chk parseSeat(<span class="hljs-string">"FBFBBFFRLR"</span>), <span class="hljs-number">357</span>
nbText: <span class="hljs-string">"if I want to get back row and col (I will use it for visualization at the end)"</span>
nbCode:
<span class="hljs-keyword">proc</span> row(x: <span class="hljs-type">Seat</span>): <span class="hljs-built_in">int</span> = x <span class="hljs-keyword">shr</span> <span class="hljs-number">3</span>
<span class="hljs-keyword">proc</span> col(x: <span class="hljs-type">Seat</span>): <span class="hljs-built_in">int</span> =
<span class="hljs-literal">result</span> = x.<span class="hljs-built_in">int</span>
<span class="hljs-literal">result</span>.mask(<span class="hljs-number">0b111</span>)
chk parseSeat(<span class="hljs-string">"FBFBBFFRLR"</span>).row, <span class="hljs-number">44</span>
chk parseSeat(<span class="hljs-string">"FBFBBFFRLR"</span>).col, <span class="hljs-number">5</span>
nbText: <span class="hljs-string">"_part1_ is solved during parsing input file"</span>
nbCode:
<span class="hljs-keyword">var</span>
maxSeat = <span class="hljs-number">0</span>
seats: <span class="hljs-built_in">seq</span>[<span class="hljs-type">Seat</span>]
<span class="hljs-keyword">for</span> line <span class="hljs-keyword">in</span> <span class="hljs-string">"2020/input05.txt"</span>.lines:
seats.add parseSeat(line)
<span class="hljs-keyword">if</span> seats[^<span class="hljs-number">1</span>] > maxSeat:
maxSeat = seats[^<span class="hljs-number">1</span>]
<span class="hljs-keyword">echo</span> <span class="hljs-string">"part1: "</span>, maxSeat
chk(maxSeat, <span class="hljs-number">947</span>)
gotTheStar
nbText: <span class="hljs-string">"_part 2_ is solved by sorting and finding the gap"</span>
nbCode:
<span class="hljs-keyword">var</span> mySeat: <span class="hljs-type">Seat</span>
seats.sort()
<span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> <span class="hljs-number">1</span> ..< seats.len:
<span class="hljs-keyword">if</span> seats[x] != seats[x - <span class="hljs-number">1</span>] + <span class="hljs-number">1</span>:
mySeat = seats[x] - <span class="hljs-number">1</span> <span class="hljs-comment">## first try I forgot the "- 1!</span>
<span class="hljs-keyword">break</span>
<span class="hljs-keyword">echo</span> <span class="hljs-string">"part2: "</span>, mySeat
chk(maxSeat, <span class="hljs-number">636</span>)
gotTheStar
nbText: <span class="hljs-string">"what do we need to _visualize the plane_? I will convert Seat in (row, col)"</span>
nbCode:
<span class="hljs-keyword">type</span> <span class="hljs-type">RowCol</span> = <span class="hljs-keyword">tuple</span>[row, col: <span class="hljs-built_in">int</span>]
<span class="hljs-keyword">proc</span> toPasses(s: <span class="hljs-built_in">seq</span>[<span class="hljs-type">Seat</span>]): <span class="hljs-type">HashSet</span>[<span class="hljs-type">RowCol</span>] =
<span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> s:
<span class="hljs-literal">result</span>.incl (x.row, x.col).<span class="hljs-type">RowCol</span>
<span class="hljs-keyword">proc</span> showAndTell(s: <span class="hljs-type">HashSet</span>[<span class="hljs-type">RowCol</span>]): <span class="hljs-type">RowCol</span> =
<span class="hljs-keyword">var</span>
text = <span class="hljs-string">"____01234567</span><span class="hljs-meta">\n</span><span class="hljs-string">"</span> <span class="hljs-comment"># I need to fix spaces at the beginning of pre!</span>
<span class="hljs-keyword">for</span> row <span class="hljs-keyword">in</span> <span class="hljs-number">0</span> .. <span class="hljs-number">127</span>:
text.add &<span class="hljs-string">"{row:03} "</span>
<span class="hljs-keyword">for</span> col <span class="hljs-keyword">in</span> <span class="hljs-number">0</span> .. <span class="hljs-number">7</span>:
<span class="hljs-keyword">if</span> (row, col) <span class="hljs-keyword">in</span> s:
text.add <span class="hljs-string">"X"</span>
<span class="hljs-keyword">elif</span> row < <span class="hljs-number">30</span> <span class="hljs-keyword">or</span> row > <span class="hljs-number">100</span>: <span class="hljs-comment"># I know my place is in the middle of the plane</span>
text.add <span class="hljs-string">"."</span>
<span class="hljs-keyword">else</span>:
text.add <span class="hljs-string">"<em>O</em>"</span>
<span class="hljs-literal">result</span> = (row, col)
text.add <span class="hljs-string">"</span><span class="hljs-meta">\n</span><span class="hljs-string">"</span>
text.add <span class="hljs-string">"</span><span class="hljs-meta">\n</span><span class="hljs-string">"</span>
<span class="hljs-keyword">echo</span> text
<span class="hljs-keyword">let</span> myRowCol = seats.toPasses.showAndTell
nbtext: <span class="hljs-string">"I can check back id"</span>
nbCode:
<span class="hljs-keyword">proc</span> id(x: <span class="hljs-type">RowCol</span>): <span class="hljs-built_in">int</span> = x.col + <span class="hljs-number">8</span>*x.row
chk id(myRowCol), mySeat
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>