From cf21b8024fdea08072210b7d0ab0230789538928 Mon Sep 17 00:00:00 2001 From: Neil Smith <neil.git@njae.me.uk> Date: Mon, 6 Dec 2021 11:17:05 +0000 Subject: [PATCH] Day 6 done --- advent-of-code21.cabal | 5 ++ advent06/Main.hs | 48 ++++++++++++ data/advent06.txt | 1 + problems/day06.html | 164 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 218 insertions(+) create mode 100644 advent06/Main.hs create mode 100644 data/advent06.txt create mode 100644 problems/day06.html diff --git a/advent-of-code21.cabal b/advent-of-code21.cabal index 69ff677..c19d0d4 100644 --- a/advent-of-code21.cabal +++ b/advent-of-code21.cabal @@ -104,3 +104,8 @@ executable advent05 import: common-extensions, build-directives main-is: advent05/Main.hs build-depends: text, attoparsec, linear, containers + +executable advent06 + import: common-extensions, build-directives + main-is: advent06/Main.hs + build-depends: split, containers diff --git a/advent06/Main.hs b/advent06/Main.hs new file mode 100644 index 0000000..6b37771 --- /dev/null +++ b/advent06/Main.hs @@ -0,0 +1,48 @@ +-- Writeup at https://work.njae.me.uk/2021/12/06/advent-of-code-2021-day-6/ + +import Data.List +import Data.List.Split +import qualified Data.IntMap.Strict as M + +type Shoal = M.IntMap Integer + +main :: IO () +main = + do text <- readFile "data/advent06.txt" + let fish = map (read @Int) $ splitOn "," text + let shoal = mkShoal fish + let generations = iterate generation shoal + print $ part1 generations + print $ part2 generations + +part1 :: [Shoal] -> Integer +part1 = countInGeneration 80 + +part2 :: [Shoal] -> Integer +part2 = countInGeneration 256 + +countInGeneration :: Int -> [Shoal] -> Integer +countInGeneration n generations = sum $ M.elems (generations!!n) + +generation :: Shoal -> Shoal +generation shoal = M.union (age shoal) (birth shoal) + +age :: Shoal -> Shoal +age shoal = M.insert 6 (was0 + was7) shoal' + where shoal' = M.mapKeys ageFish shoal + was0 = M.findWithDefault 0 0 shoal + was7 = M.findWithDefault 0 7 shoal + +ageFish :: Int -> Int +ageFish 0 = 6 +ageFish n = n - 1 + +birth :: Shoal -> Shoal +birth shoal = M.singleton 8 parents + where parents = M.findWithDefault 0 0 shoal + +mkShoal :: [Int] -> Shoal +mkShoal = M.fromList + . map (\g -> (head g, fromIntegral $ length g)) + . group + . sort diff --git a/data/advent06.txt b/data/advent06.txt new file mode 100644 index 0000000..70db26a --- /dev/null +++ b/data/advent06.txt @@ -0,0 +1 @@ +4,1,1,1,5,1,3,1,5,3,4,3,3,1,3,3,1,5,3,2,4,4,3,4,1,4,2,2,1,3,5,1,1,3,2,5,1,1,4,2,5,4,3,2,5,3,3,4,5,4,3,5,4,2,5,5,2,2,2,3,5,5,4,2,1,1,5,1,4,3,2,2,1,2,1,5,3,3,3,5,1,5,4,2,2,2,1,4,2,5,2,3,3,2,3,4,4,1,4,4,3,1,1,1,1,1,4,4,5,4,2,5,1,5,4,4,5,2,3,5,4,1,4,5,2,1,1,2,5,4,5,5,1,1,1,1,1,4,5,3,1,3,4,3,3,1,5,4,2,1,4,4,4,1,1,3,1,3,5,3,1,4,5,3,5,1,1,2,2,4,4,1,4,1,3,1,1,3,1,3,3,5,4,2,1,1,2,1,2,3,3,5,4,1,1,2,1,2,5,3,1,5,4,3,1,5,2,3,4,4,3,1,1,1,2,1,1,2,1,5,4,2,2,1,4,3,1,1,1,1,3,1,5,2,4,1,3,2,3,4,3,4,2,1,2,1,2,4,2,1,5,2,2,5,5,1,1,2,3,1,1,1,3,5,1,3,5,1,3,3,2,4,5,5,3,1,4,1,5,2,4,5,5,5,2,4,2,2,5,2,4,1,3,2,1,1,4,4,1,5 diff --git a/problems/day06.html b/problems/day06.html new file mode 100644 index 0000000..e74d894 --- /dev/null +++ b/problems/day06.html @@ -0,0 +1,164 @@ +<!DOCTYPE html> +<html lang="en-us"> +<head> +<meta charset="utf-8"/> +<title>Day 6 - Advent of Code 2021</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?26"/> +<link rel="stylesheet alternate" type="text/css" href="/static/highcontrast.css?0" title="High Contrast"/> +<link rel="shortcut icon" href="/favicon.png"/> +</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 a massive company, 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, social media), I +built the whole thing myself, including the design, animations, prose, and all +of the puzzles. + +The puzzles are most of the work; preparing a new calendar and a new set of +puzzles each year takes all of my free time for 4-5 months. 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="/2021/about">[About]</a></li><li><a href="/2021/events">[Events]</a></li><li><a href="https://teespring.com/stores/advent-of-code" target="_blank">[Shop]</a></li><li><a href="/2021/settings">[Settings]</a></li><li><a href="/2021/auth/logout">[Log Out]</a></li></ul></nav><div class="user">Neil Smith <a href="/2021/support" class="supporter-badge" title="Advent of Code Supporter">(AoC++)</a> <span class="star-count">12*</span></div></div><div><h1 class="title-event"> <span class="title-event-wrap">sub y{</span><a href="/2021">2021</a><span class="title-event-wrap">}</span></h1><nav><ul><li><a href="/2021">[Calendar]</a></li><li><a href="/2021/support">[AoC++]</a></li><li><a href="/2021/sponsors">[Sponsors]</a></li><li><a href="/2021/leaderboard">[Leaderboard]</a></li><li><a href="/2021/stats">[Stats]</a></li></ul></nav></div></header> + +<div id="sidebar"> +<div id="sponsor"><div class="quiet">Our <a href="/2021/sponsors">sponsors</a> help make Advent of Code possible:</div><div class="sponsor"><a href="https://retool.com/?utm_source=sponsor&utm_medium=event&utm_campaign=adventofcode" target="_blank" onclick="if(ga)ga('send','event','sponsor','sidebar',this.href);" rel="noopener">Retool</a> - Build internal apps remarkably fast. Drag and drop a form together, and have it POST back to your API in minutes. Write JavaScript anywhere to customize. Deploy instantly with access controls and audit logs.</div></div> +</div><!--/sidebar--> + +<main> +<script>window.addEventListener('click', function(e,s,r){if(e.target.nodeName==='CODE'&&e.detail===3){s=window.getSelection();s.removeAllRanges();r=document.createRange();r.selectNodeContents(e.target);s.addRange(r);}});</script> +<article class="day-desc"><h2>--- Day 6: Lanternfish ---</h2><p>The sea floor is getting steeper. Maybe the sleigh keys got carried this way?</p> +<p>A massive school of glowing <a href="https://en.wikipedia.org/wiki/Lanternfish" target="_blank">lanternfish</a> swims past. They must spawn quickly to reach such large numbers - maybe <em>exponentially</em> quickly? You should model their growth rate to be sure.</p> +<p>Although you know nothing about this specific species of lanternfish, you make some guesses about their attributes. Surely, <span title="I heard you like lanternfish.">each lanternfish creates a new lanternfish</span> once every <em>7</em> days.</p> +<p>However, this process isn't necessarily synchronized between every lanternfish - one lanternfish might have 2 days left until it creates another lanternfish, while another might have 4. So, you can model each fish as a single number that represents <em>the number of days until it creates a new lanternfish</em>.</p> +<p>Furthermore, you reason, a <em>new</em> lanternfish would surely need slightly longer before it's capable of producing more lanternfish: two more days for its first cycle.</p> +<p>So, suppose you have a lanternfish with an internal timer value of <code>3</code>:</p> +<ul> +<li>After one day, its internal timer would become <code>2</code>.</li> +<li>After another day, its internal timer would become <code>1</code>.</li> +<li>After another day, its internal timer would become <code>0</code>.</li> +<li>After another day, its internal timer would reset to <code>6</code>, and it would create a <em>new</em> lanternfish with an internal timer of <code>8</code>.</li> +<li>After another day, the first lanternfish would have an internal timer of <code>5</code>, and the second lanternfish would have an internal timer of <code>7</code>.</li> +</ul> +<p>A lanternfish that creates a new fish resets its timer to <code>6</code>, <em>not <code>7</code></em> (because <code>0</code> is included as a valid timer value). The new lanternfish starts with an internal timer of <code>8</code> and does not start counting down until the next day.</p> +<p>Realizing what you're trying to do, the submarine automatically produces a list of the ages of several hundred nearby lanternfish (your puzzle input). For example, suppose you were given the following list:</p> +<pre><code>3,4,3,1,2</code></pre> +<p>This list means that the first fish has an internal timer of <code>3</code>, the second fish has an internal timer of <code>4</code>, and so on until the fifth fish, which has an internal timer of <code>2</code>. Simulating these fish over several days would proceed as follows:</p> +<pre><code>Initial state: 3,4,3,1,2 +After 1 day: 2,3,2,0,1 +After 2 days: 1,2,1,6,0,8 +After 3 days: 0,1,0,5,6,7,8 +After 4 days: 6,0,6,4,5,6,7,8,8 +After 5 days: 5,6,5,3,4,5,6,7,7,8 +After 6 days: 4,5,4,2,3,4,5,6,6,7 +After 7 days: 3,4,3,1,2,3,4,5,5,6 +After 8 days: 2,3,2,0,1,2,3,4,4,5 +After 9 days: 1,2,1,6,0,1,2,3,3,4,8 +After 10 days: 0,1,0,5,6,0,1,2,2,3,7,8 +After 11 days: 6,0,6,4,5,6,0,1,1,2,6,7,8,8,8 +After 12 days: 5,6,5,3,4,5,6,0,0,1,5,6,7,7,7,8,8 +After 13 days: 4,5,4,2,3,4,5,6,6,0,4,5,6,6,6,7,7,8,8 +After 14 days: 3,4,3,1,2,3,4,5,5,6,3,4,5,5,5,6,6,7,7,8 +After 15 days: 2,3,2,0,1,2,3,4,4,5,2,3,4,4,4,5,5,6,6,7 +After 16 days: 1,2,1,6,0,1,2,3,3,4,1,2,3,3,3,4,4,5,5,6,8 +After 17 days: 0,1,0,5,6,0,1,2,2,3,0,1,2,2,2,3,3,4,4,5,7,8 +After 18 days: 6,0,6,4,5,6,0,1,1,2,6,0,1,1,1,2,2,3,3,4,6,7,8,8,8,8 +</code></pre> +<p>Each day, a <code>0</code> becomes a <code>6</code> and adds a new <code>8</code> to the end of the list, while each other number decreases by 1 if it was present at the start of the day.</p> +<p>In this example, after 18 days, there are a total of <code>26</code> fish. After 80 days, there would be a total of <code><em>5934</em></code>.</p> +<p>Find a way to simulate lanternfish. <em>How many lanternfish would there be after 80 days?</em></p> +</article> +<p>Your puzzle answer was <code>352195</code>.</p><article class="day-desc"><h2 id="part2">--- Part Two ---</h2><p>Suppose the lanternfish live forever and have unlimited food and space. Would they take over the entire ocean?</p> +<p>After 256 days in the example above, there would be a total of <code><em>26984457539</em></code> lanternfish!</p> +<p><em>How many lanternfish would there be after 256 days?</em></p> +</article> +<p>Your puzzle answer was <code>1600306001288</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="/2021">return to your Advent calendar</a> and try another puzzle.</p> +<p>If you still want to see it, you can <a href="6/input" target="_blank">get your puzzle input</a>.</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+%22Lanternfish%22+%2D+Day+6+%2D+Advent+of+Code+2021&url=https%3A%2F%2Fadventofcode%2Ecom%2F2021%2Fday%2F6&related=ericwastl&hashtags=AdventOfCode" target="_blank">Twitter</a> + <a href="javascript:void(0);" onclick="var mastodon_instance=prompt('Mastodon Instance / Server Name?'); if(typeof mastodon_instance==='string' && mastodon_instance.length){this.href='https://'+mastodon_instance+'/share?text=I%27ve+completed+%22Lanternfish%22+%2D+Day+6+%2D+Advent+of+Code+2021+%23AdventOfCode+https%3A%2F%2Fadventofcode%2Ecom%2F2021%2Fday%2F6'}else{return false;}" target="_blank">Mastodon</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('set', 'anonymizeIp', true); +ga('send', 'pageview'); +</script> +<!-- /ga --> +</body> +</html> \ No newline at end of file -- 2.43.0