<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>challenge Archives - Creatronix</title>
	<atom:link href="https://creatronix.de/tag/challenge/feed/" rel="self" type="application/rss+xml" />
	<link>https://creatronix.de/tag/challenge/</link>
	<description>My adventures in code &#38; business</description>
	<lastBuildDate>Fri, 16 Dec 2022 14:08:44 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>2021 &#8211; Advent of code &#8211; Day 1</title>
		<link>https://creatronix.de/2021-advent-of-code-day-1/</link>
		
		<dc:creator><![CDATA[Jörn]]></dc:creator>
		<pubDate>Thu, 02 Dec 2021 09:30:22 +0000</pubDate>
				<category><![CDATA[Data Science & SQL]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[advent of code]]></category>
		<category><![CDATA[challenge]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[jupyter]]></category>
		<category><![CDATA[pandas]]></category>
		<guid isPermaLink="false">https://creatronix.de/?p=4285</guid>

					<description><![CDATA[<p>I&#8217;ve haven&#8217;t participated in the advent of code before. But always been curious. What is advent of code? It&#8217;s an advent Calendar for programmers. You get 25 challenges starting December 1st. Caveat: you have to solve the challenge to be eligible for the next day&#8217;s challenge 🙂 Day 1 Challenge &#8211; Part 1 On the&#8230;</p>
<p>The post <a href="https://creatronix.de/2021-advent-of-code-day-1/">2021 &#8211; Advent of code &#8211; Day 1</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>I&#8217;ve haven&#8217;t participated in the <a href="https://adventofcode.com/2021/day/1">advent of code</a> before. But always been curious.</p>
<h2>What is advent of code?</h2>
<p>It&#8217;s an <span class="Y2IQFc" lang="en">advent Calendar for programmers. You get 25 challenges starting December 1st. Caveat: you have to solve the challenge to be eligible for the next day&#8217;s challenge 🙂<br />
</span><span id="more-4285"></span></p>
<h2>Day 1 Challenge &#8211; Part 1</h2>
<p>On the first day your first task is to count how many times a value is bigger than its predecessor. They give us some sample data</p>
<pre>199 N/A
200 <strong>bigger</strong>
208 <strong>bigger</strong>
210 <strong>bigger</strong>
200 smaller
207 <strong>bigger</strong>
240 <strong>bigger</strong>
269 <strong>bigger</strong>
260 smaller
263 <strong>bigger</strong></pre>
<p>When we count the times a value is bigger we get seven times bigger.</p>
<p>The actual data contains 2000 rows. This isn&#8217;t exactly big data but I&#8217;ve wanted to dust off my Pandas skill, so here we go:</p>
<p>Let&#8217;s look at the data</p>
<pre>import pandas as pd

df = pd.read_csv("./aoc_day_01_data.txt", header=None)
df.describe</pre>
<p>With the read_csv() function we can read in our data file and convert it into a data frame. It&#8217;s important to hand over the header=None. Otherwise pandas assumes the first row is a column header.</p>
<p>df.describe gives us:</p>
<pre class="console_text">&lt;bound method NDFrame.describe of          0
0      159
1      158
2      174
3      196
4      197
...    ...
1995  8538
1996  8543
1997  8545
1998  8557
1999  8568

[2000 rows x 1 columns]&gt;</pre>
<p>Because we want to reference the columns by name we add a column header</p>
<pre>df.columns = ["original"]</pre>
<p>To compare the nth cell with its n+1th cell neighbour be add a new column but shift the values</p>
<pre>df['shifted'] = df['original'].shift(-1)</pre>
<p>The output looks like this:</p>
<table class="dataframe" border="1">
<thead>
<tr>
<th></th>
<th>original</th>
<th>shifted</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>159</td>
<td><strong>158</strong>.0</td>
</tr>
<tr>
<th>1</th>
<td><strong>158</strong></td>
<td>174.0</td>
</tr>
<tr>
<th>2</th>
<td>174</td>
<td>196.0</td>
</tr>
<tr>
<th>3</th>
<td>196</td>
<td>197.0</td>
</tr>
<tr>
<th>4</th>
<td>197</td>
<td>194.0</td>
</tr>
<tr>
<th>&#8230;</th>
<td>&#8230;</td>
<td>&#8230;</td>
</tr>
<tr>
<th>1995</th>
<td>8538</td>
<td>8543.0</td>
</tr>
<tr>
<th>1996</th>
<td>8543</td>
<td>8545.0</td>
</tr>
<tr>
<th>1997</th>
<td>8545</td>
<td>8557.0</td>
</tr>
<tr>
<th>1998</th>
<td>8557</td>
<td>8568.0</td>
</tr>
<tr>
<th>1999</th>
<td>8568</td>
<td>NaN</td>
</tr>
</tbody>
</table>
<p>We add another column where we place the value True when the value from the current row in the shifted column is bigger than in the original column:</p>
<pre>df['increased'] = (df['shifted'] &gt; df['original'])</pre>
<p>Now it starts to look like the sample data from the introduction:</p>
<table class="dataframe" border="1">
<thead>
<tr>
<th></th>
<th>original</th>
<th>shifted</th>
<th>increased</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>159</td>
<td>158.0</td>
<td>False</td>
</tr>
<tr>
<th>1</th>
<td>158</td>
<td>174.0</td>
<td>True</td>
</tr>
<tr>
<th>2</th>
<td>174</td>
<td>196.0</td>
<td>True</td>
</tr>
<tr>
<th>3</th>
<td>196</td>
<td>197.0</td>
<td>True</td>
</tr>
<tr>
<th>4</th>
<td>197</td>
<td>194.0</td>
<td>False</td>
</tr>
<tr>
<th>&#8230;</th>
<td>&#8230;</td>
<td>&#8230;</td>
<td>&#8230;</td>
</tr>
<tr>
<th>1995</th>
<td>8538</td>
<td>8543.0</td>
<td>True</td>
</tr>
<tr>
<th>1996</th>
<td>8543</td>
<td>8545.0</td>
<td>True</td>
</tr>
<tr>
<th>1997</th>
<td>8545</td>
<td>8557.0</td>
<td>True</td>
</tr>
<tr>
<th>1998</th>
<td>8557</td>
<td>8568.0</td>
<td>True</td>
</tr>
<tr>
<th>1999</th>
<td>8568</td>
<td>NaN</td>
<td>False</td>
</tr>
</tbody>
</table>
<p>the last thing we have to do is counting how many times True occurs:</p>
<pre>true_count = df['increased'].sum()</pre>
<p>which gives us &#8220;1583&#8221;</p>
<p>This is a bit of a hack because it assumes that True equals 1 and False == 0</p>
<p>A more elegant solution is to use value_counts:</p>
<pre>df['increased'].value_counts(dropna=False)</pre>
<p>No the output is:</p>
<pre class="console_text">True     1583
False     417
Name: increased, dtype: int64</pre>
<p>And 1583 is the number we are looking for. This earned us our first golden star and unlocked the second part of the challenge:</p>
<h2>Part 2</h2>
<p>The second part is a bit more challenging because we have to sum up three adjacent values and compare them to the next three values.</p>
<pre>199  A       
200  A B     
208  A B C   
210    B C D
200  E   C D
207  E F   D
240  E F G
269    F G H
260      G H
263        H</pre>
<p>I created a new notebook and started like part 1 with reading the data and naming the first column</p>
<pre>import pandas as pd

df = pd.read_csv("./aoc_day_01_data.txt", header=None)
df.columns = ["original"]</pre>
<p>To add the sum of three values to the row of the first value we use the following code</p>
<pre>indexer = pd.api.indexers.FixedForwardWindowIndexer(window_size=3)
df["rolling_sum"] = df.original.rolling(window=indexer).sum()</pre>
<p>This demonstrates the power of Pandas once more: you have integrated sliding window functions!</p>
<p>The rest is equal to part one &#8220;shift, compare and count&#8221;</p>
<pre>df['shifted_rs'] = df['rolling_sum'].shift(-1)
df['increased_rs'] = (df['shifted_rs'] &gt; df['rolling_sum'])
true_count = df['increased_rs'].sum()
true_count</pre>
<p>As a little Fingerübung I did the same with vanilla Python:</p>
<pre>data = []
with open("./aoc_day_01_test_data.txt") as f:
    for line in f:
        data.append(int(line.rstrip()))

triplet_sums = []

for i, v in enumerate(data):
    if i &lt; (len(data) - 2):
        triplet_sum = data[i] + data[i+1] + data[i+2]
        triplet_sums.append(triplet_sum)
print(triplet_sums)

sums_larger_than_previous_sums = 0
for i, v in enumerate(triplet_sums):
    if i &lt; (len(triplet_sums) - 1):
        if triplet_sums[i] &lt; triplet_sums[i+1]:
            sums_larger_than_previous_sums += 1

print(sums_larger_than_previous_sums)</pre>
<p>Which works but is less elegant.</p>
<p>Stay tuned for more!</p>
<p>The post <a href="https://creatronix.de/2021-advent-of-code-day-1/">2021 &#8211; Advent of code &#8211; Day 1</a> appeared first on <a href="https://creatronix.de">Creatronix</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
