--- Writeup at https://work.njae.me.uk/2021/12/09/advent-of-code-2021-day-9/
+-- Writeup at https://work.njae.me.uk/2021/12/12/advent-of-code-2021-day-11/
import Data.Array.IArray
import Data.Char
c = (length $ head rows) - 1
mkOct e = Octopus (digitToInt e) False
+part1 :: Grid -> Int
part1 grid = snd $ (simulate grid) !! 100
+part2 :: Grid -> Int
part2 grid = length $ takeWhile notSyncronised $ simulate grid
where notSyncronised (g, _) = not $ simultaneous g
numFlashers = length flashers
grid3 = resetFlashers grid2 flashers
-
+simultaneous :: Grid -> Bool
simultaneous grid = all zeroOct $ elems grid
where zeroOct (Octopus 0 _) = True
zeroOct _ = False
increment :: Grid -> Grid
increment = amap incrementOne
+incrementSome :: Grid -> [Coord] -> Grid
incrementSome grid locations = grid // (zip locations incrementedOcts)
where incrementedOcts = map (incrementOne . (grid !)) locations
+incrementOne :: Octopus -> Octopus
incrementOne (Octopus energy flashed) = Octopus (energy + 1) flashed
-
findFlashers :: Grid -> [Coord]
findFlashers = map fst . filter (overpowered . snd) . assocs
where overpowered (Octopus energy _) = energy > 9
-
+flash :: Grid -> [Coord] -> Grid
flash grid [] = grid
flash grid (here:agenda)
+ -- already flashed, so ignore
| flashed == True = flash grid agenda
+ -- not enough energy to flash, so ignore
| energy <= 9 = flash grid agenda
| otherwise = flash grid'' agenda'
- -- set this as flashed
- -- increment neighbours
- -- add negighbours to agenda
where Octopus energy flashed = grid ! here
nbrs = neighbours grid here
- octopus' = Octopus (energy + 1) True
- agenda' = nbrs ++ agenda
+ -- set this as flashed
+ octopus' = Octopus energy True
grid' = grid // [(here, octopus')]
+ -- add negighbours to agenda
+ agenda' = nbrs ++ agenda
+ -- increment neighbours
grid'' = incrementSome grid' nbrs
resetFlashers :: Grid -> [Coord] -> Grid
resetFlashers grid locations = grid // (zip locations resetOcts)
where resetOcts = repeat (Octopus 0 False)
-
neighbours :: Grid -> Coord -> [Coord]
neighbours grid here = filter (inRange (bounds grid))
[ here ^+^ (V2 r c)
--- /dev/null
+<!DOCTYPE html>
+<html lang="en-us">
+<head>
+<meta charset="utf-8"/>
+<title>Day 11 - 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">22*</span></div></div><div><h1 class="title-event"> <span class="title-event-wrap">{'year':</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://2021-aoc-templates.util.repl.co/" target="_blank" onclick="if(ga)ga('send','event','sponsor','sidebar',this.href);" rel="noopener">Replit</a> - Code and host in your browser with no setup in Python, React, Kaboom.js, Java, C, Nix, you name it, even Solidity. Happy coding!</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 11: Dumbo Octopus ---</h2><p>You enter a large cavern full of rare bioluminescent <a href="https://www.youtube.com/watch?v=eih-VSaS2g0" target="_blank">dumbo octopuses</a>! They seem to not like the Christmas lights on your submarine, so you turn them off for now.</p>
+<p>There are 100 <span title="I know it's weird; I grew up saying 'octopi' too.">octopuses</span> arranged neatly in a 10 by 10 grid. Each octopus slowly gains <em>energy</em> over time and <em>flashes</em> brightly for a moment when its energy is full. Although your lights are off, maybe you could navigate through the cave without disturbing the octopuses if you could predict when the flashes of light will happen.</p>
+<p>Each octopus has an <em>energy level</em> - your submarine can remotely measure the energy level of each octopus (your puzzle input). For example:</p>
+<pre><code>5483143223
+2745854711
+5264556173
+6141336146
+6357385478
+4167524645
+2176841721
+6882881134
+4846848554
+5283751526
+</code></pre>
+<p>The energy level of each octopus is a value between <code>0</code> and <code>9</code>. Here, the top-left octopus has an energy level of <code>5</code>, the bottom-right one has an energy level of <code>6</code>, and so on.</p>
+<p>You can model the energy levels and flashes of light in <em>steps</em>. During a single step, the following occurs:</p>
+<ul>
+<li>First, the energy level of each octopus increases by <code>1</code>.</li>
+<li>Then, any octopus with an energy level greater than <code>9</code> <em>flashes</em>. This increases the energy level of all adjacent octopuses by <code>1</code>, including octopuses that are diagonally adjacent. If this causes an octopus to have an energy level greater than <code>9</code>, it <em>also flashes</em>. This process continues as long as new octopuses keep having their energy level increased beyond <code>9</code>. (An octopus can only flash <em>at most once per step</em>.)</li>
+<li>Finally, any octopus that flashed during this step has its energy level set to <code>0</code>, as it used all of its energy to flash.</li>
+</ul>
+<p>Adjacent flashes can cause an octopus to flash on a step even if it begins that step with very little energy. Consider the middle octopus with <code>1</code> energy in this situation:</p>
+<pre><code>Before any steps:
+11111
+19991
+19191
+19991
+11111
+
+After step 1:
+34543
+4<em>000</em>4
+5<em>000</em>5
+4<em>000</em>4
+34543
+
+After step 2:
+45654
+51115
+61116
+51115
+45654
+</code></pre>
+<p>An octopus is <em>highlighted</em> when it flashed during the given step.</p>
+<p>Here is how the larger example above progresses:</p>
+<pre><code>Before any steps:
+5483143223
+2745854711
+5264556173
+6141336146
+6357385478
+4167524645
+2176841721
+6882881134
+4846848554
+5283751526
+
+After step 1:
+6594254334
+3856965822
+6375667284
+7252447257
+7468496589
+5278635756
+3287952832
+7993992245
+5957959665
+6394862637
+
+After step 2:
+88<em>0</em>7476555
+5<em>0</em>89<em>0</em>87<em>0</em>54
+85978896<em>0</em>8
+84857696<em>00</em>
+87<em>00</em>9<em>0</em>88<em>00</em>
+66<em>000</em>88989
+68<em>0000</em>5943
+<em>000000</em>7456
+9<em>000000</em>876
+87<em>0000</em>6848
+
+After step 3:
+<em>00</em>5<em>0</em>9<em>00</em>866
+85<em>00</em>8<em>00</em>575
+99<em>000000</em>39
+97<em>000000</em>41
+9935<em>0</em>8<em>00</em>63
+77123<em>00000</em>
+791125<em>000</em>9
+221113<em>0000</em>
+<em>0</em>421125<em>000</em>
+<em>00</em>21119<em>000</em>
+
+After step 4:
+2263<em>0</em>31977
+<em>0</em>923<em>0</em>31697
+<em>00</em>3222115<em>0</em>
+<em>00</em>41111163
+<em>00</em>76191174
+<em>00</em>53411122
+<em>00</em>4236112<em>0</em>
+5532241122
+1532247211
+113223<em>0</em>211
+
+After step 5:
+4484144<em>000</em>
+2<em>0</em>44144<em>000</em>
+2253333493
+1152333274
+11873<em>0</em>3285
+1164633233
+1153472231
+6643352233
+2643358322
+2243341322
+
+After step 6:
+5595255111
+3155255222
+33644446<em>0</em>5
+2263444496
+2298414396
+2275744344
+2264583342
+7754463344
+3754469433
+3354452433
+
+After step 7:
+67<em>0</em>7366222
+4377366333
+4475555827
+34966557<em>0</em>9
+35<em>00</em>6256<em>0</em>9
+35<em>0</em>9955566
+3486694453
+8865585555
+486558<em>0</em>644
+4465574644
+
+After step 8:
+7818477333
+5488477444
+5697666949
+46<em>0</em>876683<em>0</em>
+473494673<em>0</em>
+474<em>00</em>97688
+69<em>0000</em>7564
+<em>000000</em>9666
+8<em>00000</em>4755
+68<em>0000</em>7755
+
+After step 9:
+9<em>0</em>6<em>0000</em>644
+78<em>00000</em>976
+69<em>000000</em>8<em>0</em>
+584<em>00000</em>82
+5858<em>0000</em>93
+69624<em>00000</em>
+8<em>0</em>2125<em>000</em>9
+222113<em>000</em>9
+9111128<em>0</em>97
+7911119976
+
+After step 10:
+<em>0</em>481112976
+<em>00</em>31112<em>00</em>9
+<em>00</em>411125<em>0</em>4
+<em>00</em>811114<em>0</em>6
+<em>00</em>991113<em>0</em>6
+<em>00</em>93511233
+<em>0</em>44236113<em>0</em>
+553225235<em>0</em>
+<em>0</em>53225<em>0</em>6<em>00</em>
+<em>00</em>3224<em>0000</em>
+</code></pre>
+
+<p>After step 10, there have been a total of <code>204</code> flashes. Fast forwarding, here is the same configuration every 10 steps:</p>
+
+<pre><code>After step 20:
+3936556452
+56865568<em>0</em>6
+449655569<em>0</em>
+444865558<em>0</em>
+445686557<em>0</em>
+568<em>00</em>86577
+7<em>00000</em>9896
+<em>0000000</em>344
+6<em>000000</em>364
+46<em>0000</em>9543
+
+After step 30:
+<em>0</em>643334118
+4253334611
+3374333458
+2225333337
+2229333338
+2276733333
+2754574565
+5544458511
+9444447111
+7944446119
+
+After step 40:
+6211111981
+<em>0</em>421111119
+<em>00</em>42111115
+<em>000</em>3111115
+<em>000</em>3111116
+<em>00</em>65611111
+<em>0</em>532351111
+3322234597
+2222222976
+2222222762
+
+After step 50:
+9655556447
+48655568<em>0</em>5
+448655569<em>0</em>
+445865558<em>0</em>
+457486557<em>0</em>
+57<em>000</em>86566
+6<em>00000</em>9887
+8<em>000000</em>533
+68<em>00000</em>633
+568<em>0000</em>538
+
+After step 60:
+25333342<em>00</em>
+274333464<em>0</em>
+2264333458
+2225333337
+2225333338
+2287833333
+3854573455
+1854458611
+1175447111
+1115446111
+
+After step 70:
+8211111164
+<em>0</em>421111166
+<em>00</em>42111114
+<em>000</em>4211115
+<em>0000</em>211116
+<em>00</em>65611111
+<em>0</em>532351111
+7322235117
+5722223475
+4572222754
+
+After step 80:
+1755555697
+59655556<em>0</em>9
+448655568<em>0</em>
+445865558<em>0</em>
+457<em>0</em>86557<em>0</em>
+57<em>000</em>86566
+7<em>00000</em>8666
+<em>0000000</em>99<em>0</em>
+<em>0000000</em>8<em>00</em>
+<em>0000000000</em>
+
+After step 90:
+7433333522
+2643333522
+2264333458
+2226433337
+2222433338
+2287833333
+2854573333
+4854458333
+3387779333
+3333333333
+
+After step 100:
+<em>0</em>397666866
+<em>0</em>749766918
+<em>00</em>53976933
+<em>000</em>4297822
+<em>000</em>4229892
+<em>00</em>53222877
+<em>0</em>532222966
+9322228966
+7922286866
+6789998766
+</code></pre>
+<p>After 100 steps, there have been a total of <code><em>1656</em></code> flashes.</p>
+<p>Given the starting energy levels of the dumbo octopuses in your cavern, simulate 100 steps. <em>How many total flashes are there after 100 steps?</em></p>
+</article>
+<p>Your puzzle answer was <code>1627</code>.</p><article class="day-desc"><h2 id="part2">--- Part Two ---</h2><p>It seems like the individual flashes aren't bright enough to navigate. However, you might have a better option: the flashes seem to be <em>synchronizing</em>!</p>
+<p>In the example above, the first time all octopuses flash simultaneously is step <code><em>195</em></code>:</p>
+<pre><code>After step 193:
+5877777777
+8877777777
+7777777777
+7777777777
+7777777777
+7777777777
+7777777777
+7777777777
+7777777777
+7777777777
+
+After step 194:
+6988888888
+9988888888
+8888888888
+8888888888
+8888888888
+8888888888
+8888888888
+8888888888
+8888888888
+8888888888
+
+After step 195:
+<em>0000000000</em>
+<em>0000000000</em>
+<em>0000000000</em>
+<em>0000000000</em>
+<em>0000000000</em>
+<em>0000000000</em>
+<em>0000000000</em>
+<em>0000000000</em>
+<em>0000000000</em>
+<em>0000000000</em>
+</code></pre>
+<p>If you can calculate the exact moments when the octopuses will all flash simultaneously, you should be able to navigate through the cavern. <em>What is the first step during which all octopuses flash?</em></p>
+</article>
+<p>Your puzzle answer was <code>329</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="11/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+%22Dumbo+Octopus%22+%2D+Day+11+%2D+Advent+of+Code+2021&url=https%3A%2F%2Fadventofcode%2Ecom%2F2021%2Fday%2F11&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+%22Dumbo+Octopus%22+%2D+Day+11+%2D+Advent+of+Code+2021+%23AdventOfCode+https%3A%2F%2Fadventofcode%2Ecom%2F2021%2Fday%2F11'}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