Skip to content

Commit 76e18b3

Browse files
committed
day03 blogpost initial version
1 parent 85bc114 commit 76e18b3

File tree

3 files changed

+481
-74
lines changed

3 files changed

+481
-74
lines changed

2021/day03.html

+352
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,352 @@
1+
<!DOCTYPE html>
2+
<html lang="en-us">
3+
<head>
4+
<title>2021/day03.nim</title>
5+
<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>">
6+
<meta content="text/html; charset=utf-8" http-equiv="content-type">
7+
<meta content="width=device-width, initial-scale=1" name="viewport">
8+
<link rel='stylesheet' href='https://unpkg.com/normalize.css/'>
9+
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/kognise/water.css@latest/dist/dark.min.css">
10+
<link rel='stylesheet' href='https://cdn.jsdelivr.net/gh/pietroppeter/nimib/assets/androidstudio.css'>
11+
<style>
12+
.nb-box {
13+
display: flex;
14+
align-items: center;
15+
justify-content: space-between;
16+
}
17+
.nb-small {
18+
font-size: 0.8rem;
19+
}
20+
button.nb-small {
21+
float: right;
22+
padding: 2px;
23+
padding-right: 5px;
24+
padding-left: 5px;
25+
}
26+
section#source {
27+
display:none
28+
}
29+
</style>
30+
31+
<script async defer data-domain="pietroppeter.github.io/adventofnim" src="https://plausible.io/js/plausible.js"></script>
32+
<style>
33+
a {
34+
text-decoration: none;
35+
color: #009900;
36+
}
37+
38+
a:hover {
39+
color: #99ff99;
40+
}
41+
42+
em {
43+
color: #ffffff;
44+
font-style: normal;
45+
text-shadow: 0 0 5px #ffffff;
46+
}
47+
48+
em.star {
49+
font-style: normal;
50+
color: #ffff66;
51+
text-shadow: 0 0 5px #ffff66;
52+
}
53+
</style>
54+
</head>
55+
<body>
56+
<header>
57+
<div class="nb-box">
58+
<span><a href="..">🏡</a></span>
59+
<span><code>2021/day03.nim</code></span>
60+
<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>
61+
</div>
62+
<hr>
63+
</header><main>
64+
<h2>Day 3: <a href="https://adventofcode.com/2021/day/3">Binary Diagnostic</a></h2>
65+
<h3>Part 1</h3>
66+
<blockquote>
67+
<p>The diagnostic report (your puzzle input) consists of a list of binary numbers which, when decoded properly, can tell you many useful things about the conditions of the submarine. The first parameter to check is the <em>power consumption</em>.</p>
68+
<p>You need to use the binary numbers in the diagnostic report to generate two new binary numbers (called the <em>gamma rate</em> and the <em>epsilon rate</em>). The power consumption can then be found by multiplying the gamma rate by the epsilon rate.</p>
69+
<p>Each bit in the gamma rate can be determined by finding the <em>most common bit in the corresponding position</em> of all numbers in the diagnostic report. For example, given the following diagnostic report:</p>
70+
</blockquote>
71+
<pre><code class="nim hljs"><span class="hljs-keyword">let</span> testInput = <span class="hljs-string">&quot;&quot;&quot;
72+
00100
73+
11110
74+
10110
75+
10111
76+
10101
77+
01111
78+
00111
79+
11100
80+
10000
81+
11001
82+
00010
83+
01010&quot;&quot;&quot;</span></code></pre>
84+
<blockquote>
85+
<p>Considering only the first bit of each number, there are five 0 bits and seven 1 bits. Since the most common bit is 1, the first bit of the gamma rate is 1.</p>
86+
<p>The most common second bit of the numbers in the diagnostic report is 0, so the second bit of the gamma rate is 0.</p>
87+
<p>The most common value of the third, fourth, and fifth bits are 1, 1, and 0, respectively, and so the final three bits of the gamma rate are 110.</p>
88+
<p>So, the gamma rate is the binary number 10110, or <em>22</em> in decimal.</p>
89+
<p>The epsilon rate is calculated in a similar way; rather than use the most common bit, the least common bit from each position is used. So, the epsilon rate is 01001, or <em>9</em> in decimal. Multiplying the gamma rate (22) by the epsilon rate (9) produces the power consumption, <em>198</em>.</p>
90+
<p>Use the binary numbers in your diagnostic report to calculate the gamma rate and epsilon rate, then multiply them together. <em>What is the power consumption of the submarine?</em></p>
91+
</blockquote>
92+
<p>I had to catch a plane this morning and the puzzle unlocked
93+
while getting onto the plane. Luckily I was able to solve part1 in the few
94+
minutes from the moment I sat on the plane to the moment they asked us
95+
to turn off the devices, so I could get also the text for part2 and had
96+
plenty of time during flight to solve it.</p>
97+
<pre><code class="nim hljs"><span class="hljs-keyword">let</span>
98+
testNums = testInput.splitLines.toSeq
99+
puzzleNums = <span class="hljs-string">&quot;2021/input03.txt&quot;</span>.lines.toSeq</code></pre>
100+
<pre><code class="nim hljs"><span class="hljs-keyword">func</span> getGamma(nums: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">string</span>]): <span class="hljs-built_in">int</span> =
101+
<span class="hljs-keyword">var</span>
102+
gamma = <span class="hljs-string">&quot;&quot;</span> <span class="hljs-comment"># I can reuse identifier</span>
103+
countOnes: <span class="hljs-built_in">int</span>
104+
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">0</span> .. nums[<span class="hljs-number">0</span>].<span class="hljs-keyword">high</span>:
105+
countOnes = <span class="hljs-number">0</span>
106+
<span class="hljs-keyword">for</span> num <span class="hljs-keyword">in</span> nums:
107+
<span class="hljs-keyword">if</span> num[i] == <span class="hljs-string">'1'</span>:
108+
inc countOnes
109+
<span class="hljs-keyword">if</span> countOnes &gt;= nums.len <span class="hljs-keyword">div</span> <span class="hljs-number">2</span>:
110+
gamma.add <span class="hljs-string">'1'</span>
111+
<span class="hljs-keyword">else</span>:
112+
gamma.add <span class="hljs-string">'0'</span>
113+
<span class="hljs-literal">result</span> = parseBinInt gamma
114+
115+
dump testNums.getGamma
116+
dump puzzleNums.getGamma</code></pre>
117+
<pre><samp>testNums.getGamma = 22
118+
puzzleNums.getGamma = 2502
119+
</samp></pre>
120+
<blockquote>
121+
<p>That's the right answer! You are <em class="star">one gold star</em> closer to saving your vacation.</p>
122+
</blockquote>
123+
<pre><code class="nim hljs"><span class="hljs-keyword">func</span> part1(nums: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">string</span>]): <span class="hljs-built_in">int</span> =
124+
<span class="hljs-keyword">let</span>
125+
gamma = getGamma(nums)
126+
<span class="hljs-comment"># 2^5(=32) - 22 - 1 -&gt; 9 </span>
127+
epsilon = <span class="hljs-number">2</span>^len(nums[<span class="hljs-number">0</span>]) - gamma
128+
<span class="hljs-keyword">return</span> gamma*epsilon
129+
130+
dump part1 testNums
131+
dump part1 puzzleNums</code></pre>
132+
<pre><samp>part1 testNums = 220
133+
part1 puzzleNums = 3988188
134+
</samp></pre>
135+
<pre><code class="nim hljs"><span class="hljs-keyword">func</span> lifeSupport(nums: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">string</span>], oxy=<span class="hljs-literal">true</span>): <span class="hljs-built_in">int</span> =
136+
<span class="hljs-keyword">var</span>
137+
nums = nums <span class="hljs-comment"># idiomatic</span>
138+
i = <span class="hljs-number">0</span>
139+
zeros, ones: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">int</span>]
140+
<span class="hljs-keyword">while</span> nums.len &gt; <span class="hljs-number">1</span>:
141+
zeros = @[] <span class="hljs-comment"># zeros, ones = @[] does not work</span>
142+
ones = @[]
143+
<span class="hljs-keyword">for</span> j, num <span class="hljs-keyword">in</span> nums:
144+
<span class="hljs-keyword">if</span> num[i] == <span class="hljs-string">'1'</span>:
145+
ones.add j
146+
<span class="hljs-keyword">else</span>:
147+
zeros.add j
148+
<span class="hljs-comment">#dump ones</span>
149+
<span class="hljs-comment">#dump zeros</span>
150+
<span class="hljs-keyword">if</span> oxy:
151+
<span class="hljs-keyword">if</span> ones.len &gt;= zeros.len:
152+
nums = collect:
153+
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> ones:
154+
nums[j]
155+
<span class="hljs-keyword">else</span>:
156+
nums = collect:
157+
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> zeros:
158+
nums[j]
159+
<span class="hljs-keyword">else</span>:
160+
<span class="hljs-keyword">if</span> zeros.len &lt;= ones.len:
161+
nums = collect:
162+
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> zeros:
163+
nums[j]
164+
<span class="hljs-keyword">else</span>:
165+
nums = collect:
166+
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> ones:
167+
nums[j]
168+
inc i
169+
<span class="hljs-comment">#debugEcho nums</span>
170+
<span class="hljs-keyword">assert</span> nums.len == <span class="hljs-number">1</span>
171+
<span class="hljs-keyword">return</span> parseBinInt(nums[<span class="hljs-number">0</span>])
172+
173+
<span class="hljs-keyword">func</span> part2(nums: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">string</span>]): <span class="hljs-built_in">int</span> =
174+
lifeSupport(nums)*lifeSupport(nums, <span class="hljs-literal">false</span>)
175+
dump part2 testNums
176+
dump part2 puzzleNums</code></pre>
177+
<pre><samp>part2 testNums = 230
178+
part2 puzzleNums = 2555739
179+
</samp></pre>
180+
<blockquote>
181+
<p>That's the right answer! You are <em class="star">one gold star</em> closer to saving your vacation.</p>
182+
</blockquote>
183+
<p>Later I will add more comments, highlights and I have
184+
in mind something special for the &quot;visualization&quot; part (if I get to it).</p>
185+
<p>Stay tuned and enjoy Advent of Nim! 🎄👑</p>
186+
187+
</main>
188+
<footer>
189+
<hr>
190+
<div class="nb-box">
191+
<span><span class="nb-small">made with <a href="https://pietroppeter.github.io/nimib/">nimib 🐳</a></span></span>
192+
<span></span>
193+
<span><button class="nb-small" id="show" onclick="toggleSourceDisplay()">Show Source</button></span>
194+
</div>
195+
</footer>
196+
<section id="source">
197+
<pre><code class="nim hljs"><span class="hljs-keyword">import</span> nimib, animu
198+
199+
nbInit(theme=useAdventOfNim)
200+
201+
nbText: <span class="hljs-string">&quot;&quot;&quot;## Day 3: [Binary Diagnostic](https://adventofcode.com/2021/day/3)
202+
203+
### Part 1
204+
205+
&gt; The diagnostic report (your puzzle input) consists of a list of binary numbers which, when decoded properly, can tell you many useful things about the conditions of the submarine. The first parameter to check is the _power consumption_.
206+
&gt;
207+
&gt; You need to use the binary numbers in the diagnostic report to generate two new binary numbers (called the _gamma rate_ and the _epsilon rate_). The power consumption can then be found by multiplying the gamma rate by the epsilon rate.
208+
&gt;
209+
&gt; Each bit in the gamma rate can be determined by finding the _most common bit in the corresponding position_ of all numbers in the diagnostic report. For example, given the following diagnostic report:
210+
&quot;&quot;&quot;</span>
211+
212+
nbCode:
213+
<span class="hljs-keyword">let</span> testInput = <span class="hljs-string">&quot;&quot;&quot;
214+
00100
215+
11110
216+
10110
217+
10111
218+
10101
219+
01111
220+
00111
221+
11100
222+
10000
223+
11001
224+
00010
225+
01010&quot;&quot;&quot;</span>
226+
227+
nbText: <span class="hljs-string">&quot;&quot;&quot;
228+
&gt; Considering only the first bit of each number, there are five 0 bits and seven 1 bits. Since the most common bit is 1, the first bit of the gamma rate is 1.
229+
&gt;
230+
&gt; The most common second bit of the numbers in the diagnostic report is 0, so the second bit of the gamma rate is 0.
231+
&gt;
232+
&gt; The most common value of the third, fourth, and fifth bits are 1, 1, and 0, respectively, and so the final three bits of the gamma rate are 110.
233+
&gt;
234+
&gt; So, the gamma rate is the binary number 10110, or _22_ in decimal.
235+
&gt;
236+
&gt; The epsilon rate is calculated in a similar way; rather than use the most common bit, the least common bit from each position is used. So, the epsilon rate is 01001, or _9_ in decimal. Multiplying the gamma rate (22) by the epsilon rate (9) produces the power consumption, _198_.
237+
&gt;
238+
&gt; Use the binary numbers in your diagnostic report to calculate the gamma rate and epsilon rate, then multiply them together. _What is the power consumption of the submarine?_
239+
240+
I had to catch a plane this morning and the puzzle unlocked
241+
while getting onto the plane. Luckily I was able to solve part1 in the few
242+
minutes from the moment I sat on the plane to the moment they asked us
243+
to turn off the devices, so I could get also the text for part2 and had
244+
plenty of time during flight to solve it.
245+
&quot;&quot;&quot;</span>
246+
nbCode:
247+
<span class="hljs-keyword">let</span>
248+
testNums = testInput.splitLines.toSeq
249+
puzzleNums = <span class="hljs-string">&quot;2021/input03.txt&quot;</span>.lines.toSeq
250+
251+
nbCode:
252+
<span class="hljs-keyword">func</span> getGamma(nums: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">string</span>]): <span class="hljs-built_in">int</span> =
253+
<span class="hljs-keyword">var</span>
254+
gamma = <span class="hljs-string">&quot;&quot;</span> <span class="hljs-comment"># I can reuse identifier</span>
255+
countOnes: <span class="hljs-built_in">int</span>
256+
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> <span class="hljs-number">0</span> .. nums[<span class="hljs-number">0</span>].<span class="hljs-keyword">high</span>:
257+
countOnes = <span class="hljs-number">0</span>
258+
<span class="hljs-keyword">for</span> num <span class="hljs-keyword">in</span> nums:
259+
<span class="hljs-keyword">if</span> num[i] == <span class="hljs-string">'1'</span>:
260+
inc countOnes
261+
<span class="hljs-keyword">if</span> countOnes &gt;= nums.len <span class="hljs-keyword">div</span> <span class="hljs-number">2</span>:
262+
gamma.add <span class="hljs-string">'1'</span>
263+
<span class="hljs-keyword">else</span>:
264+
gamma.add <span class="hljs-string">'0'</span>
265+
<span class="hljs-literal">result</span> = parseBinInt gamma
266+
267+
dump testNums.getGamma
268+
dump puzzleNums.getGamma
269+
270+
gotTheStar
271+
272+
nbCode:
273+
<span class="hljs-keyword">func</span> part1(nums: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">string</span>]): <span class="hljs-built_in">int</span> =
274+
<span class="hljs-keyword">let</span>
275+
gamma = getGamma(nums)
276+
<span class="hljs-comment"># 2^5(=32) - 22 - 1 -&gt; 9 </span>
277+
epsilon = <span class="hljs-number">2</span>^len(nums[<span class="hljs-number">0</span>]) - gamma
278+
<span class="hljs-keyword">return</span> gamma*epsilon
279+
280+
dump part1 testNums
281+
dump part1 puzzleNums
282+
283+
nbCode:
284+
<span class="hljs-keyword">func</span> lifeSupport(nums: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">string</span>], oxy=<span class="hljs-literal">true</span>): <span class="hljs-built_in">int</span> =
285+
<span class="hljs-keyword">var</span>
286+
nums = nums <span class="hljs-comment"># idiomatic</span>
287+
i = <span class="hljs-number">0</span>
288+
zeros, ones: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">int</span>]
289+
<span class="hljs-keyword">while</span> nums.len &gt; <span class="hljs-number">1</span>:
290+
zeros = @[] <span class="hljs-comment"># zeros, ones = @[] does not work</span>
291+
ones = @[]
292+
<span class="hljs-keyword">for</span> j, num <span class="hljs-keyword">in</span> nums:
293+
<span class="hljs-keyword">if</span> num[i] == <span class="hljs-string">'1'</span>:
294+
ones.add j
295+
<span class="hljs-keyword">else</span>:
296+
zeros.add j
297+
<span class="hljs-comment">#dump ones</span>
298+
<span class="hljs-comment">#dump zeros</span>
299+
<span class="hljs-keyword">if</span> oxy:
300+
<span class="hljs-keyword">if</span> ones.len &gt;= zeros.len:
301+
nums = collect:
302+
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> ones:
303+
nums[j]
304+
<span class="hljs-keyword">else</span>:
305+
nums = collect:
306+
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> zeros:
307+
nums[j]
308+
<span class="hljs-keyword">else</span>:
309+
<span class="hljs-keyword">if</span> zeros.len &lt;= ones.len:
310+
nums = collect:
311+
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> zeros:
312+
nums[j]
313+
<span class="hljs-keyword">else</span>:
314+
nums = collect:
315+
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> ones:
316+
nums[j]
317+
inc i
318+
<span class="hljs-comment">#debugEcho nums</span>
319+
<span class="hljs-keyword">assert</span> nums.len == <span class="hljs-number">1</span>
320+
<span class="hljs-keyword">return</span> parseBinInt(nums[<span class="hljs-number">0</span>])
321+
322+
<span class="hljs-keyword">func</span> part2(nums: <span class="hljs-built_in">seq</span>[<span class="hljs-built_in">string</span>]): <span class="hljs-built_in">int</span> =
323+
lifeSupport(nums)*lifeSupport(nums, <span class="hljs-literal">false</span>)
324+
dump part2 testNums
325+
dump part2 puzzleNums
326+
327+
gotTheStar
328+
329+
nbText: <span class="hljs-string">&quot;&quot;&quot;
330+
Later I will add more comments, highlights and I have
331+
in mind something special for the &quot;visualization&quot; part (if I get to it).
332+
333+
Stay tuned and enjoy Advent of Nim! 🎄👑
334+
&quot;&quot;&quot;</span>
335+
336+
nbSave
337+
338+
</code></pre>
339+
</section><script>
340+
function toggleSourceDisplay() {
341+
var btn = document.getElementById("show")
342+
var source = document.getElementById("source");
343+
if (btn.innerHTML=="Show Source") {
344+
btn.innerHTML = "Hide Source";
345+
source.style.display = "block";
346+
} else {
347+
btn.innerHTML = "Show Source";
348+
source.style.display = "none";
349+
}
350+
}
351+
</script></body>
352+
</html>

0 commit comments

Comments
 (0)