-
Notifications
You must be signed in to change notification settings - Fork 1
/
day08.html
378 lines (346 loc) Β· 21.7 KB
/
day08.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
<!DOCTYPE html>
<html lang="en-us">
<head>
<title>2020\day08.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\day08.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>2020, Day 8: Handheld Halting</h1>
<p>For a better solution than mine look at the solution shared
by fxn on the forum (<a href="https://gist.github.com/fxn/bbd90877bcd27581ee5f7720495c3c3f">gist</a>).</p>
<p>I use a inelegant global trace (instead of an <code>intsets</code> of visted indices).
This solution is not that bad anyway.</p>
<p>Listing the imports (usually I hide them from the document).</p>
<pre><code class="nim hljs"><span class="hljs-keyword">import</span>
strutils, parseutils, strformat, std / enumerate</code></pre>
<p>rather standard types. I regretted very soon naming instructions
in the program as <code>instrs</code>, what a bad name!</p>
<pre><code class="nim hljs"><span class="hljs-keyword">type</span>
<span class="hljs-type">OpCode</span> = <span class="hljs-keyword">enum</span>
opNop = <span class="hljs-string">"nop"</span>, opAcc = <span class="hljs-string">"acc"</span>, opJmp = <span class="hljs-string">"jmp"</span>
<span class="hljs-type">Instruction</span> = <span class="hljs-keyword">tuple</span>[op: <span class="hljs-type">OpCode</span>, val: <span class="hljs-built_in">int</span>]
<span class="hljs-type">Program</span> = <span class="hljs-keyword">object</span>
tr: <span class="hljs-built_in">int</span> <span class="hljs-comment">## p.tr: pointer</span>
acc: <span class="hljs-built_in">int</span> <span class="hljs-comment">## global register</span>
instrs: <span class="hljs-built_in">seq</span>[<span class="hljs-type">Instruction</span>]
<span class="hljs-type">Trace</span> = <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">seq</span>[<span class="hljs-built_in">int</span>]]</code></pre>
<p>the parsing is clean, since opcodes are 3 letters do not even need scanf (enumerate is used for debugging)</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>
op: <span class="hljs-type">OpCode</span>
val: <span class="hljs-built_in">int</span>
<span class="hljs-keyword">for</span> i, line <span class="hljs-keyword">in</span> enumerate(text.splitLines):
<span class="hljs-keyword">try</span>:
op = parseEnum[<span class="hljs-type">OpCode</span>](line[<span class="hljs-number">0</span> .. <span class="hljs-number">2</span>])
<span class="hljs-keyword">discard</span> parseInt(line[<span class="hljs-number">4</span> .. ^<span class="hljs-number">1</span>], val)
<span class="hljs-literal">result</span>.add (op, val)
<span class="hljs-keyword">except</span>:
<span class="hljs-keyword">echo</span> <span class="hljs-string">"ERROR parsing "</span>, i, <span class="hljs-string">" "</span>, line</code></pre>
<p>testing the first lines of code on the example</p>
<pre><code class="nim hljs"><span class="hljs-keyword">let</span> example = <span class="hljs-string">"""nop +0
acc +1
jmp +4
acc +3
jmp -3
acc -99
acc +1
jmp -4
acc +6"""</span>
<span class="hljs-keyword">var</span> exp = <span class="hljs-type">Program</span>(instrs: parse example)
<span class="hljs-keyword">echo</span> exp</code></pre>
<pre><samp>(tr: 0, acc: 0, instrs: @[(op: nop, val: 0), (op: acc, val: 1), (op: jmp, val: 4), (op: acc, val: 3), (op: jmp, val: -3), (op: acc, val: -99), (op: acc, val: 1), (op: jmp, val: -4), (op: acc, val: 6)])</samp></pre>
<p>as mentioned, I decided to go with a global trace variable for simplicty</p>
<pre><code class="nim hljs"><span class="hljs-keyword">proc</span> initTrace(p: <span class="hljs-type">Program</span>): <span class="hljs-type">Trace</span> =
<span class="hljs-literal">result</span>.setlen(p.instrs.len)
<span class="hljs-literal">result</span>[p.tr].add <span class="hljs-number">1</span>
<span class="hljs-keyword">var</span> tr = initTrace(exp)
<span class="hljs-keyword">echo</span> tr
<span class="hljs-keyword">template</span> trace(): <span class="hljs-built_in">bool</span> =
tr[p.tr].add(i + <span class="hljs-number">1</span>)
tr[p.tr].len == <span class="hljs-number">1</span> <span class="hljs-comment">## somewhat cryptic. If you visited more than once length will be 2</span></code></pre>
<pre><samp>@[@[1], @[], @[], @[], @[], @[], @[], @[], @[]]</samp></pre>
<p>and here is the code for the execution of the program (single step)</p>
<pre><code class="nim hljs"><span class="hljs-keyword">proc</span> op(p: <span class="hljs-type">Program</span>): <span class="hljs-type">OpCode</span> =
p.instrs[p.tr].op
<span class="hljs-keyword">proc</span> val(p: <span class="hljs-type">Program</span>): <span class="hljs-built_in">int</span> =
p.instrs[p.tr].val
<span class="hljs-keyword">proc</span> endd(p: <span class="hljs-type">Program</span>): <span class="hljs-built_in">bool</span> =
(p.tr == p.instrs.len) <span class="hljs-comment">## end is keyword</span>
<span class="hljs-keyword">proc</span> exec(p: <span class="hljs-keyword">var</span> <span class="hljs-type">Program</span>): <span class="hljs-built_in">bool</span> =
<span class="hljs-keyword">let</span> i = tr[p.tr][^<span class="hljs-number">1</span>]
<span class="hljs-keyword">case</span> p.op
<span class="hljs-keyword">of</span> opNop:
inc p.tr
<span class="hljs-keyword">of</span> opAcc:
p.acc += p.val
inc p.tr
<span class="hljs-keyword">of</span> opJmp:
inc p.tr, p.val
<span class="hljs-keyword">if</span> p.endd:
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>
trace</code></pre>
<p>this was a template at the beginning
but I forgot strformat has known <a href="https://nim-lang.org/docs/strformat.html#limitations">limitations with template arguments</a>.</p>
<p>Of this I do like the name <code>echoo</code> (this is throwaway stuff for debugging purposes).</p>
<pre><code class="nim hljs"><span class="hljs-keyword">proc</span> echoo(p: <span class="hljs-type">Program</span>; tr: <span class="hljs-type">Trace</span>) =
<span class="hljs-keyword">echo</span> p.op, <span class="hljs-string">" "</span>, <span class="hljs-string">fmt"{p.val:3}"</span>, <span class="hljs-string">" | "</span>, tr[p.tr]
<span class="hljs-keyword">discard</span>
echoo(exp, tr)
<span class="hljs-keyword">while</span> exec(exp):
echoo(exp, tr)
echoo(exp, tr)
<span class="hljs-keyword">echo</span> <span class="hljs-string">"part1: "</span>, exp.acc <span class="hljs-comment">## 5, ok</span>
exp.acc = <span class="hljs-number">0</span> <span class="hljs-comment">## reset for part2</span></code></pre>
<pre><samp>nop 0 | @[1]
acc 1 | @[2]
jmp 4 | @[3]
acc 1 | @[4]
jmp -4 | @[5]
acc 3 | @[6]
jmp -3 | @[7]
acc 1 | @[2, 8]
part1: 5</samp></pre>
<p>part 2 appears first for the example. It is straightforward test of fixing iteratively all positions</p>
<pre><code class="nim hljs"><span class="hljs-keyword">var</span> q: <span class="hljs-type">Program</span>
<span class="hljs-keyword">var</span> k: <span class="hljs-built_in">int</span>
<span class="hljs-keyword">template</span> fix(p: <span class="hljs-type">Program</span>) =
k = <span class="hljs-number">0</span>
q = p
<span class="hljs-keyword">while</span> <span class="hljs-keyword">not</span> q.endd:
q = p
<span class="hljs-keyword">if</span> q.acc != <span class="hljs-number">0</span>:
<span class="hljs-keyword">echo</span> <span class="hljs-string">"ERROR it should be zero!"</span>
<span class="hljs-keyword">while</span> p.instrs[k].op == opAcc:
inc k
<span class="hljs-keyword">if</span> p.instrs[k].op == opJmp:
q.instrs[k].op = opNop
<span class="hljs-keyword">elif</span> p.instrs[k].op == opNop:
q.instrs[k].op = opJmp
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">echo</span> <span class="hljs-string">"ERROR should not be here"</span>
<span class="hljs-comment">## echo "trying to fix instruction ", k</span>
tr = initTrace(q)
<span class="hljs-comment">## echo "q.acc: ", q.acc</span>
<span class="hljs-keyword">while</span> exec(q):
<span class="hljs-keyword">discard</span>
inc k
<span class="hljs-keyword">echo</span> <span class="hljs-string">"part2: "</span>, q.acc
fix exp <span class="hljs-comment">## 8, ok</span></code></pre>
<pre><samp>part2: 8</samp></pre>
<p>Finally let's collect the stars!</p>
<pre><code class="nim hljs"><span class="hljs-keyword">let</span> input = <span class="hljs-string">"2020/input08.txt"</span>.readFile
<span class="hljs-keyword">var</span> inp = <span class="hljs-type">Program</span>(instrs: parse input)
tr = initTrace(inp)
<span class="hljs-keyword">while</span> exec(inp):
<span class="hljs-keyword">discard</span>
<span class="hljs-keyword">echo</span> <span class="hljs-string">"part1: "</span>, inp.acc <span class="hljs-comment">## 1810</span>
tr = initTrace(inp)
inp.acc = <span class="hljs-number">0</span>
inp.tr = <span class="hljs-number">0</span>
fix inp <span class="hljs-comment">## 969</span></code></pre>
<pre><samp>part1: 1810
part2: 969</samp></pre>
<p>you know your solution is bad when trying to publish it you incur in all sorts of bugs...</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
nbInit
nbText: <span class="hljs-string">"# 2020, Day 8: Handheld Halting"</span>
nbText: <span class="hljs-string">"""For a better solution than mine look at the solution shared
by fxn on the forum ([gist](https://gist.github.com/fxn/bbd90877bcd27581ee5f7720495c3c3f)).
I use a inelegant global trace (instead of an `intsets` of visted indices).
This solution is not that bad anyway.
Listing the imports (usually I hide them from the document).
"""</span>
nbCode:
<span class="hljs-keyword">import</span> strutils, parseutils, strformat, std/enumerate
nbText: <span class="hljs-string">"""rather standard types. I regretted very soon naming instructions
in the program as `instrs`, what a bad name!"""</span>
nbCode:
<span class="hljs-keyword">type</span>
<span class="hljs-type">OpCode</span> = <span class="hljs-keyword">enum</span>
opNop = <span class="hljs-string">"nop"</span>, opAcc = <span class="hljs-string">"acc"</span>, opJmp = <span class="hljs-string">"jmp"</span>
<span class="hljs-type">Instruction</span> = <span class="hljs-keyword">tuple</span>[op: <span class="hljs-type">OpCode</span>, val: <span class="hljs-built_in">int</span>]
<span class="hljs-type">Program</span> = <span class="hljs-keyword">object</span>
tr: <span class="hljs-built_in">int</span> <span class="hljs-comment">## p.tr: pointer</span>
acc: <span class="hljs-built_in">int</span> <span class="hljs-comment">## global register</span>
instrs: <span class="hljs-built_in">seq</span>[<span class="hljs-type">Instruction</span>]
<span class="hljs-type">Trace</span> = <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">seq</span>[<span class="hljs-built_in">int</span>]]
nbText: <span class="hljs-string">"the parsing is clean, since opcodes are 3 letters do not even need scanf (enumerate is used for debugging)"</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>
op: <span class="hljs-type">OpCode</span>
val: <span class="hljs-built_in">int</span>
<span class="hljs-keyword">for</span> i, line <span class="hljs-keyword">in</span> enumerate(text.splitLines):
<span class="hljs-keyword">try</span>:
op = parseEnum[<span class="hljs-type">OpCode</span>](line[<span class="hljs-number">0</span> .. <span class="hljs-number">2</span>])
<span class="hljs-keyword">discard</span> parseInt(line[<span class="hljs-number">4</span> .. ^<span class="hljs-number">1</span>], val)
<span class="hljs-literal">result</span>.add (op, val)
<span class="hljs-keyword">except</span>:
<span class="hljs-keyword">echo</span> <span class="hljs-string">"ERROR parsing "</span>, i, <span class="hljs-string">" "</span>, line
nbText: <span class="hljs-string">"testing the first lines of code on the example"</span>
nbCode:
<span class="hljs-keyword">let</span> example = <span class="hljs-string">"""nop +0
acc +1
jmp +4
acc +3
jmp -3
acc -99
acc +1
jmp -4
acc +6"""</span>
<span class="hljs-keyword">var</span> exp = <span class="hljs-type">Program</span>(instrs: parse example)
<span class="hljs-keyword">echo</span> exp
nbText: <span class="hljs-string">"as mentioned, I decided to go with a global trace variable for simplicty"</span>
nbCode:
<span class="hljs-keyword">proc</span> initTrace(p: <span class="hljs-type">Program</span>): <span class="hljs-type">Trace</span> =
<span class="hljs-literal">result</span>.setlen(p.instrs.len)
<span class="hljs-literal">result</span>[p.tr].add <span class="hljs-number">1</span>
<span class="hljs-comment"># I will keep it global</span>
<span class="hljs-keyword">var</span> tr = initTrace(exp)
<span class="hljs-keyword">echo</span> tr
<span class="hljs-keyword">template</span> trace: <span class="hljs-built_in">bool</span> =
tr[p.tr].add(i + <span class="hljs-number">1</span>)
tr[p.tr].len == <span class="hljs-number">1</span> <span class="hljs-comment">## somewhat cryptic. If you visited more than once length will be 2</span>
nbText: <span class="hljs-string">"and here is the code for the execution of the program (single step)"</span>
nbCode:
<span class="hljs-keyword">proc</span> op(p: <span class="hljs-type">Program</span>): <span class="hljs-type">OpCode</span> = p.instrs[p.tr].op
<span class="hljs-keyword">proc</span> val(p: <span class="hljs-type">Program</span>): <span class="hljs-built_in">int</span> = p.instrs[p.tr].val
<span class="hljs-keyword">proc</span> endd(p: <span class="hljs-type">Program</span>): <span class="hljs-built_in">bool</span> = (p.tr == p.instrs.len) <span class="hljs-comment">## end is keyword</span>
<span class="hljs-keyword">proc</span> exec(p: <span class="hljs-keyword">var</span> <span class="hljs-type">Program</span>): <span class="hljs-built_in">bool</span> =
<span class="hljs-keyword">let</span>
i = tr[p.tr][^<span class="hljs-number">1</span>]
<span class="hljs-keyword">case</span> p.op:
<span class="hljs-keyword">of</span> opNop:
inc p.tr
<span class="hljs-keyword">of</span> opAcc:
p.acc += p.val
inc p.tr
<span class="hljs-keyword">of</span> opJmp:
inc p.tr, p.val
<span class="hljs-keyword">if</span> p.endd:
<span class="hljs-keyword">return</span> <span class="hljs-literal">false</span>
trace
nbText: <span class="hljs-string">"""this was a template at the beginning
but I forgot strformat has known [limitations with template arguments](https://nim-lang.org/docs/strformat.html#limitations).
Of this I do like the name `echoo` (this is throwaway stuff for debugging purposes).
"""</span>
nbCode:
<span class="hljs-keyword">proc</span> echoo(p: <span class="hljs-type">Program</span>, tr: <span class="hljs-type">Trace</span>) =
<span class="hljs-keyword">echo</span> p.op, <span class="hljs-string">" "</span>, <span class="hljs-string">fmt"{p.val:3}"</span>, <span class="hljs-string">" | "</span>, tr[p.tr]
<span class="hljs-keyword">discard</span>
echoo(exp, tr)
<span class="hljs-keyword">while</span> exec(exp): echoo(exp, tr)
echoo(exp, tr)
<span class="hljs-keyword">echo</span> <span class="hljs-string">"part1: "</span>, exp.acc <span class="hljs-comment">## 5, ok</span>
exp.acc = <span class="hljs-number">0</span> <span class="hljs-comment">## reset for part2</span>
nbText: <span class="hljs-string">"part 2 appears first for the example. It is straightforward test of fixing iteratively all positions"</span>
nbCode:
<span class="hljs-comment"># part 2 fix (example first)</span>
<span class="hljs-keyword">var</span> q: <span class="hljs-type">Program</span>
<span class="hljs-keyword">var</span> k: <span class="hljs-built_in">int</span> <span class="hljs-comment"># last operation to test fixing</span>
<span class="hljs-keyword">template</span> fix(p: <span class="hljs-type">Program</span>) =
k = <span class="hljs-number">0</span>
q = p
<span class="hljs-keyword">while</span> <span class="hljs-keyword">not</span> q.endd:
q = p
<span class="hljs-keyword">if</span> q.acc != <span class="hljs-number">0</span>: <span class="hljs-keyword">echo</span> <span class="hljs-string">"ERROR it should be zero!"</span>
<span class="hljs-keyword">while</span> p.instrs[k].op == opAcc: inc k
<span class="hljs-keyword">if</span> p.instrs[k].op == opJmp: q.instrs[k].op = opNop
<span class="hljs-keyword">elif</span> p.instrs[k].op == opNop: q.instrs[k].op = opJmp
<span class="hljs-keyword">else</span>: <span class="hljs-keyword">echo</span> <span class="hljs-string">"ERROR should not be here"</span>
<span class="hljs-comment">##echo "trying to fix instruction ", k</span>
tr = initTrace(q)
<span class="hljs-comment">##echo "q.acc: ", q.acc</span>
<span class="hljs-keyword">while</span> exec(q): <span class="hljs-keyword">discard</span>
inc k
<span class="hljs-keyword">echo</span> <span class="hljs-string">"part2: "</span>, q.acc
fix exp <span class="hljs-comment">## 8, ok</span>
nbText: <span class="hljs-string">"Finally let's collect the stars!"</span>
nbCode:
<span class="hljs-keyword">let</span> input = <span class="hljs-string">"2020/input08.txt"</span>.readFile
<span class="hljs-keyword">var</span> inp = <span class="hljs-type">Program</span>(instrs: parse input)
tr = initTrace(inp)
<span class="hljs-keyword">while</span> exec(inp): <span class="hljs-keyword">discard</span>
<span class="hljs-keyword">echo</span> <span class="hljs-string">"part1: "</span>, inp.acc <span class="hljs-comment">## 1810</span>
tr = initTrace(inp)
inp.acc = <span class="hljs-number">0</span>
inp.tr = <span class="hljs-number">0</span>
fix inp <span class="hljs-comment">## 969</span>
nbText: <span class="hljs-string">"you know your solution is bad when trying to publish it you incur in all sorts of bugs..."</span>
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>