Day 19
authorNeil Smith <neil.git@njae.me.uk>
Mon, 19 Dec 2016 10:13:43 +0000 (10:13 +0000)
committerNeil Smith <neil.git@njae.me.uk>
Mon, 19 Dec 2016 10:13:43 +0000 (10:13 +0000)
advent19.hs [new file with mode: 0644]
day19.html [new file with mode: 0644]

diff --git a/advent19.hs b/advent19.hs
new file mode 100644 (file)
index 0000000..9b9406c
--- /dev/null
@@ -0,0 +1,37 @@
+{-# LANGUAGE BangPatterns #-}
+
+import Prelude hiding (length, take, drop)
+import Data.Sequence
+
+-- input = 5 
+input = 3012210 
+
+main :: IO ()
+main = do 
+    part1 
+    part2
+
+part1 :: IO ()
+part1 = print $ 2 * (input - 2 ^ (toInteger (floor $ logBase 2 (fromIntegral input)))) + 1
+
+part2 :: IO ()
+part2 = print $ index (presentSteps initial) 0
+
+presentSteps :: Seq Int -> Seq Int
+presentSteps elves 
+    | isFinished elves = elves
+    | otherwise = presentSteps $ next elves
+
+initial :: Seq Int
+initial = fromList [1..input] 
+
+isFinished :: Seq Int -> Bool
+isFinished elves = length elves == 1
+
+next :: Seq Int -> Seq Int
+next elves = prefix >< (midfix |> suffix)
+    where 
+        target = length elves `quot` 2
+        prefix = drop 1 $ take target elves
+        midfix = drop (target+1) elves
+        suffix = index elves 0
diff --git a/day19.html b/day19.html
new file mode 100644 (file)
index 0000000..441174e
--- /dev/null
@@ -0,0 +1,179 @@
+<!DOCTYPE html>
+<html lang="en-us">
+<head>
+<meta charset="utf-8"/>
+<title>Day 19 - Advent of Code 2016</title>
+<!--[if lt IE 9]><script src="/static/html5.js"></script><![endif]-->
+<link href='//fonts.googleapis.com/css?family=Source+Code+Pro:300&subset=latin,latin-ext' rel='stylesheet' type='text/css'>
+<link rel="stylesheet" type="text/css" href="/static/style.css?9"/>
+<link rel="shortcut icon" href="/favicon.ico?2"/>
+</head><!--
+
+
+
+
+Oh, hello!  Funny seeing you here.
+
+I appreciate your enthusiasm, but you aren't going to find much down here.
+There certainly aren't clues to any of the puzzles.  The best surprises don't
+even appear in the source until you unlock them for real.
+
+Please be careful with automated requests; I'm not Google, and I can only take
+so much traffic.  Please be considerate so that everyone gets to play.
+
+If you're curious about how Advent of Code works, it's running on some custom
+Perl code. Other than a few integrations (auth, analytics, ads, social media),
+I built the whole thing myself, including the design, animations, prose, and
+all of the puzzles.
+
+The puzzles probably took the longest; the easiest ones were around 45 minutes
+each, but the harder ones took 2-3 hours, and a few even longer than that. A
+lot of effort went into building this thing - I hope you're enjoying playing it
+as much as I enjoyed making it for you!
+
+If you'd like to hang out, I'm @ericwastl on Twitter.
+
+- Eric Wastl
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+-->
+<body>
+<header><div><h1 class="title-global"><a href="/">Advent of Code</a></h1><nav><ul><li><a href="/2016/about">[About]</a></li><li><a href="/2016/support">[AoC++]</a></li><li><a href="/2016/events">[Events]</a></li><li><a href="/2016/settings">[Settings]</a></li><li><a href="/2016/auth/logout">[Log Out]</a></li></ul></nav><div class="user">Neil Smith <span class="supporter">(AoC++)</span> <span class="star-count">38*</span></div></div><div><h1 class="title-event">&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">sub y{</span><a href="/2016">2016</a><span class="title-event-wrap">}</span></h1><nav><ul><li><a href="/2016">[Calendar]</a></li><li><a href="/2016/leaderboard">[Leaderboard]</a></li><li><a href="/2016/stats">[Stats]</a></li><li><a href="/2016/sponsors">[Sponsors]</a></li></ul></nav></div></header>
+
+<div id="sidebar">
+<div id="sponsor"><div class="quiet">Our <a href="/2016/sponsors">sponsors</a> help make AoC possible:</div><p><a href="https://www.detroitlabs.com/careers" target="_blank" onclick="if(ga)ga('send','event','sponsor','click',this.href);">Detroit Labs</a> - Build beautiful mobile apps.</p></div>
+<div id="ad">
+<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
+<!-- Advent of Code Wide Skyscraper -->
+<ins class="adsbygoogle"
+     style="display:inline-block;width:160px;height:600px"
+     data-ad-client="ca-pub-9420604735624631"
+     data-ad-slot="8014013294"></ins>
+<script>
+(adsbygoogle = window.adsbygoogle || []).push({});
+</script>
+</div><!--/ad-->
+</div><!--/sidebar-->
+
+<main>
+<article class="day-desc"><h2>--- Day 19: An Elephant Named Joseph ---</h2><p>The Elves contact you over a highly secure emergency channel. Back at the North Pole, the Elves are busy <span title="Eggnoggedly misunderstanding them, actually.">misunderstanding</span> <a href="https://en.wikipedia.org/wiki/White_elephant_gift_exchange">White Elephant parties</a>.</p>
+<p>Each Elf brings a present. They all sit in a circle, numbered starting with position <code>1</code>. Then, starting with the first Elf, they take turns stealing all the presents from the Elf to their left.  An Elf with no presents is removed from the circle and does not take turns.</p>
+<p>For example, with five Elves (numbered <code>1</code> to <code>5</code>):</p>
+<pre><code>  1
+5   2
+ 4 3
+</code></pre>
+<ul>
+<li>Elf <code>1</code> takes Elf <code>2</code>'s present.</li>
+<li>Elf <code>2</code> has no presents and is skipped.</li>
+<li>Elf <code>3</code> takes Elf <code>4</code>'s present.</li>
+<li>Elf <code>4</code> has no presents and is also skipped.</li>
+<li>Elf <code>5</code> takes Elf <code>1</code>'s two presents.</li>
+<li>Neither Elf <code>1</code> nor Elf <code>2</code> have any presents, so both are skipped.</li>
+<li>Elf <code>3</code> takes Elf <code>5</code>'s three presents.</li>
+</ul>
+<p>So, with <em>five</em> Elves, the Elf that sits starting in position <code>3</code> gets all the presents.</p>
+<p>With the number of Elves given in your puzzle input, <em>which Elf gets all the presents?</em></p>
+</article>
+<p>Your puzzle answer was <code>1830117</code>.</p><article class="day-desc"><h2>--- Part Two ---</h2><p>Realizing the folly of their present-exchange rules, the Elves agree to instead steal presents from the Elf <em>directly across the circle</em>. If two Elves are across the circle, the one on the left (from the perspective of the stealer) is stolen from.  The other rules remain unchanged: Elves with no presents are removed from the circle entirely, and the other elves move in slightly to keep the circle evenly spaced.</p>
+<p>For example, with five Elves (again numbered <code>1</code> to <code>5</code>):</p>
+<ul>
+<li>The Elves sit in a circle; Elf <code>1</code> goes first:
+<pre><code>  <em>1</em>
+5   2
+ 4 3
+</code></pre></li>
+<li>Elves <code>3</code> and <code>4</code> are across the circle; Elf <code>3</code>'s present is stolen, being the one to the left. Elf <code>3</code> leaves the circle, and the rest of the Elves move in:
+<pre><code>  <em>1</em>           1
+5   2  --&gt;  5   2
+ 4 -          4
+</code></pre></li>
+<li>Elf <code>2</code> steals from the Elf directly across the circle, Elf <code>5</code>:
+<pre><code>  1         1 
+-   <em>2</em>  --&gt;     2
+  4         4 
+</code></pre></li>
+<li>Next is Elf <code>4</code> who, choosing between Elves <code>1</code> and <code>2</code>, steals from Elf <code>1</code>:
+<pre><code> -          2  
+    2  --&gt;
+ <em>4</em>          4
+</code></pre></li>
+<li>Finally, Elf <code>2</code> steals from Elf <code>4</code>:
+<pre><code> <em>2</em>
+    --&gt;  2  
+ -
+</code></pre></li>
+</ul>
+<p>So, with <em>five</em> Elves, the Elf that sits starting in position <code>2</code> gets all the presents.</p>
+<p>With the number of Elves given in your puzzle input, <em>which Elf now gets all the presents?</em></p>
+</article>
+<p>Your puzzle answer was <code>1417887</code>.</p><p class="day-success">Both parts of this puzzle are complete! They provide two gold stars: **</p>
+<p>At this point, you should <a href="/2016">return to your advent calendar</a> and try another puzzle.</p>
+<p>Your puzzle input was <code class="puzzle-input">3012210</code>.</p>
+<p>You can also <span class="share">[Share<span class="share-content">on
+  <a href="https://twitter.com/intent/tweet?text=I%27ve+completed+%22An+Elephant+Named+Joseph%22+%2D+Day+19+%2D+Advent+of+Code+2016&amp;url=http%3A%2F%2Fadventofcode%2Ecom%2F2016%2Fday%2F19&amp;related=ericwastl&amp;hashtags=AdventOfCode" target="_blank">Twitter</a>
+  <a href="https://plus.google.com/share?url=http%3A%2F%2Fadventofcode%2Ecom%2F2016%2Fday%2F19" target="_blank">Google+</a>
+  <a href="http://www.reddit.com/submit?url=http%3A%2F%2Fadventofcode%2Ecom%2F2016%2Fday%2F19&amp;title=I%27ve+completed+%22An+Elephant+Named+Joseph%22+%2D+Day+19+%2D+Advent+of+Code+2016" target="_blank">Reddit</a
+></span>]</span> this puzzle.</p>
+</main>
+
+<!-- ga -->
+<script>
+(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
+(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
+m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
+})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
+ga('create', 'UA-69522494-1', 'auto');
+ga('send', 'pageview');
+</script>
+<!-- /ga -->
+</body>
+</html>
\ No newline at end of file