Done day 7
authorNeil Smith <neil.git@njae.me.uk>
Tue, 7 Dec 2021 10:35:25 +0000 (10:35 +0000)
committerNeil Smith <neil.git@njae.me.uk>
Tue, 7 Dec 2021 10:35:25 +0000 (10:35 +0000)
advent-of-code21.cabal
advent07/Main.hs [new file with mode: 0644]
data/advent07.txt [new file with mode: 0644]
problems/day07.html [new file with mode: 0644]

index c19d0d4dd2e462746b0d996d83840561f4c0d0f5..11a6fbd18705b9832702c058efab83c1ae2432fa 100644 (file)
@@ -109,3 +109,8 @@ executable advent06
   import: common-extensions, build-directives
   main-is: advent06/Main.hs
   build-depends: split, containers
+
+executable advent07
+  import: common-extensions, build-directives
+  main-is: advent07/Main.hs
+  build-depends: split
diff --git a/advent07/Main.hs b/advent07/Main.hs
new file mode 100644 (file)
index 0000000..50cd076
--- /dev/null
@@ -0,0 +1,36 @@
+-- Writeup at https://work.njae.me.uk/2021/12/07/advent-of-code-2021-day-7/
+
+import Data.List.Split
+
+main :: IO ()
+main = 
+  do  text <- readFile "data/advent07.txt"
+      let subs = map (read @Int) $ splitOn "," text
+      print $ part1 subs
+      print $ part2 subs
+
+part1 = bestFuel loss1
+part2 = bestFuel loss2
+
+bestFuel :: ([Int] -> Int -> Int) -> [Int] -> Int
+bestFuel loss subs = loss subs best
+  where meanSub = (sum subs) `div` (length subs)
+        best = gradientDescend loss subs meanSub
+
+loss1 :: [Int] -> Int -> Int
+loss1 subs target = sum $ map diff subs
+  where diff s = abs (target - s)
+
+loss2 :: [Int] -> Int -> Int
+loss2 subs target = sum $ map triangleDiff subs
+  where diff s = abs (target - s)
+        triangleDiff s = ((diff s) * ((diff s) + 1)) `div` 2
+
+gradientDescend :: ([Int] -> Int -> Int) -> [Int] -> Int -> Int
+gradientDescend loss subs guess = 
+  if | lossLower  < lossHere -> gradientDescend loss subs (guess - 1)
+     | lossHigher < lossHere -> gradientDescend loss subs (guess + 1)
+     | otherwise -> guess
+  where lossHere   = loss subs guess
+        lossLower  = loss subs (guess - 1)
+        lossHigher = loss subs (guess + 1)
diff --git a/data/advent07.txt b/data/advent07.txt
new file mode 100644 (file)
index 0000000..58b0702
--- /dev/null
@@ -0,0 +1 @@
+1101,1,29,67,1102,0,1,65,1008,65,35,66,1005,66,28,1,67,65,20,4,0,1001,65,1,65,1106,0,8,99,35,67,101,99,105,32,110,39,101,115,116,32,112,97,115,32,117,110,101,32,105,110,116,99,111,100,101,32,112,114,111,103,114,97,109,10,616,0,1633,1048,833,967,161,22,823,601,603,538,340,798,1053,400,54,41,54,296,1336,1013,9,763,650,313,15,177,1289,307,741,314,289,63,183,503,764,187,225,596,273,387,1,1165,61,19,78,514,355,605,103,483,291,1781,1137,398,593,38,444,204,274,528,147,131,1021,812,430,710,257,1408,1587,517,773,218,99,357,301,543,1668,11,311,350,373,145,507,325,1006,696,607,281,433,302,148,519,846,1528,766,158,51,850,216,1320,690,338,298,631,560,306,5,888,242,1230,1694,1330,570,184,946,97,96,272,537,312,1246,847,138,325,28,253,785,483,906,412,28,178,485,828,823,1035,1001,108,1068,90,308,223,18,191,1269,39,238,307,7,643,1546,203,254,371,402,207,666,786,793,361,441,105,15,421,1748,255,152,1376,626,296,707,4,627,885,49,316,34,379,1591,39,1087,135,1515,69,725,419,924,414,78,1169,8,1331,2,771,1295,570,323,9,406,75,42,1003,180,188,174,145,128,625,1312,85,427,56,15,87,449,831,906,34,186,609,1597,531,104,1034,615,608,1338,192,280,982,334,853,1155,194,124,205,1384,135,906,239,761,1357,16,328,623,3,1432,634,1698,31,981,347,75,222,896,77,1204,1272,711,106,772,1366,279,162,98,487,1281,188,71,307,398,470,40,12,459,449,984,1271,260,1132,493,1117,129,36,1040,947,570,89,853,373,102,771,107,266,106,59,485,61,87,353,164,278,1489,542,442,4,62,788,63,130,723,919,1169,327,459,431,1107,992,1162,1287,901,838,638,261,307,761,533,119,336,4,422,173,172,64,222,531,998,1250,1007,20,1231,69,289,531,757,185,519,184,1139,369,2,1102,857,339,1267,1357,217,774,1352,23,136,2,1389,253,87,883,28,247,292,15,332,69,170,20,544,75,850,310,1137,301,155,265,100,842,189,7,584,40,168,22,548,7,30,1027,744,1294,329,100,1255,424,515,460,163,375,26,618,275,1012,935,160,181,84,186,990,1208,152,753,508,590,578,81,625,600,430,306,311,156,5,56,187,25,249,1090,316,224,173,199,71,221,1219,335,87,260,607,121,25,1326,473,224,92,87,734,179,64,325,320,117,302,1247,879,716,984,284,239,738,30,90,61,844,997,823,387,956,842,580,540,648,1947,32,63,380,873,1086,142,512,206,742,584,157,858,1300,992,311,139,906,693,1,36,1320,236,48,58,32,147,34,229,497,1,657,616,309,494,1419,264,595,729,1374,984,74,446,436,77,1516,156,915,565,159,269,263,442,775,12,6,337,115,971,598,87,1283,533,991,204,1382,1204,277,27,801,260,198,426,89,72,458,1164,571,1329,501,1547,125,376,865,642,268,626,167,429,901,623,103,100,1064,125,450,695,28,1470,469,187,119,1363,44,485,1243,1163,507,139,147,72,100,160,624,506,1360,66,444,581,729,531,701,1091,178,476,22,926,354,88,1076,946,213,38,43,125,291,714,113,54,1214,1067,641,374,411,64,1364,415,133,752,372,212,19,1941,780,902,512,852,157,8,175,90,913,125,771,764,381,947,572,391,313,249,201,106,1500,487,107,868,464,984,1471,550,642,196,571,18,306,293,659,1274,290,352,0,528,754,564,316,685,57,293,75,584,251,1107,217,11,21,329,493,175,600,259,380,30,148,556,136,180,12,26,507,199,3,0,143,87,1452,359,989,170,64,269,17,1018,105,317,289,127,275,269,359,511,690,205,423,356,19,177,260,789,51,119,210,1151,707,869,194,773,159,216,759,16,161,47,1254,293,54,504,432,1230,213,26,253,424,98,1515,162,346,326,12,122,79,210,912,55,705,597,369,1381,284,1163,316,34,384,36,1254,1455,994,60,1395,476,100,38,726,198,605,103,489,361,9,24,158,1056,264,1103,175,1423,266,45,93,271,331,673,788,48,12,580,697,593,480,268,559,302,87,281,6,401,170,90,939,543,223,137,809,139,182,571,68,1112,20,1004,1090,249,1435,267,10,375,504,906,946,1503,1362,184,233,112,1058,16,235,548,563,162,102,746,439,105,259,27,19,817,1444,119,175,341,130,202,31,432,480,710,1127,682,454,134,823,168,276,113,914,1112,118,10,1041,902,141,1428,282,485,353,589,906,987,488,144,154,25,930,368,261,176,168,85,814,1915,248,49,1012,3,143,951,30,411,336,46,1383,26,857,1650,192,1477,194,73,154,91,287,229,144,675,989,135,360,74,60,223,219,625,182,793
\ No newline at end of file
diff --git a/problems/day07.html b/problems/day07.html
new file mode 100644 (file)
index 0000000..77f0664
--- /dev/null
@@ -0,0 +1,161 @@
+<!DOCTYPE html>
+<html lang="en-us">
+<head>
+<meta charset="utf-8"/>
+<title>Day 7 - 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">14*</span></div></div><div><h1 class="title-event">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="title-event-wrap">/^</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://getsturdy.com/aoc" target="_blank" onclick="if(ga)ga('send','event','sponsor','sidebar',this.href);" rel="noopener">Sturdy</a> - Real-time version control for code. Watch us live!</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 7: The Treachery of Whales ---</h2><p>A giant <a href="https://en.wikipedia.org/wiki/Sperm_whale" target="_blank">whale</a> has decided your submarine is its next meal, and it's much faster than you are. There's nowhere to run!</p>
+<p>Suddenly, a swarm of crabs (each in its own tiny submarine - it's too deep for them otherwise) zooms in to rescue you! They seem to be preparing to blast a hole in the ocean floor; sensors indicate a <em>massive underground cave system</em> just beyond where they're aiming!</p>
+<p>The crab submarines all need to be aligned before they'll have enough power to blast a large enough hole for your submarine to get through. However, it doesn't look like they'll be aligned before the whale catches you! Maybe you can help?</p>
+<p>There's one major catch - crab submarines can only move horizontally.</p>
+<p>You quickly make a list of <em>the horizontal position of each crab</em> (your puzzle input). Crab submarines have limited fuel, so you need to find a way to make all of their horizontal positions match while requiring them to spend as little fuel as possible.</p>
+<p>For example, consider the following horizontal positions:</p>
+<pre><code>16,1,2,0,4,2,7,1,2,14</code></pre>
+<p>This means there's a crab with horizontal position <code>16</code>, a crab with horizontal position <code>1</code>, and so on.</p>
+<p>Each change of 1 step in horizontal position of a single crab costs 1 fuel. You could choose any horizontal position to align them all on, but the one that costs the least fuel is horizontal position <code>2</code>:</p>
+<ul>
+<li>Move from <code>16</code> to <code>2</code>: <code>14</code> fuel</li>
+<li>Move from <code>1</code> to <code>2</code>: <code>1</code> fuel</li>
+<li>Move from <code>2</code> to <code>2</code>: <code>0</code> fuel</li>
+<li>Move from <code>0</code> to <code>2</code>: <code>2</code> fuel</li>
+<li>Move from <code>4</code> to <code>2</code>: <code>2</code> fuel</li>
+<li>Move from <code>2</code> to <code>2</code>: <code>0</code> fuel</li>
+<li>Move from <code>7</code> to <code>2</code>: <code>5</code> fuel</li>
+<li>Move from <code>1</code> to <code>2</code>: <code>1</code> fuel</li>
+<li>Move from <code>2</code> to <code>2</code>: <code>0</code> fuel</li>
+<li>Move from <code>14</code> to <code>2</code>: <code>12</code> fuel</li>
+</ul>
+<p>This costs a total of <code><em>37</em></code> fuel. This is the cheapest possible outcome; more expensive outcomes include aligning at position <code>1</code> (<code>41</code> fuel), position <code>3</code> (<code>39</code> fuel), or position <code>10</code> (<code>71</code> fuel).</p>
+<p>Determine the horizontal position that the crabs can align to using the least fuel possible. <em>How much fuel must they spend to align to that position?</em></p>
+</article>
+<p>Your puzzle answer was <code>336721</code>.</p><article class="day-desc"><h2 id="part2">--- Part Two ---</h2><p>The crabs don't seem interested in your proposed solution. Perhaps you misunderstand crab engineering?</p>
+<p>As it turns out, crab submarine engines <span title="This appears to be due to the modial interaction of magneto-reluctance and capacitive duractance.">don't burn fuel at a constant rate</span>. Instead, each change of 1 step in horizontal position costs 1 more unit of fuel than the last: the first step costs <code>1</code>, the second step costs <code>2</code>, the third step costs <code>3</code>, and so on.</p>
+<p>As each crab moves, moving further becomes more expensive. This changes the best horizontal position to align them all on; in the example above, this becomes <code>5</code>:</p>
+<ul>
+<li>Move from <code>16</code> to <code>5</code>: <code>66</code> fuel</li>
+<li>Move from <code>1</code> to <code>5</code>: <code>10</code> fuel</li>
+<li>Move from <code>2</code> to <code>5</code>: <code>6</code> fuel</li>
+<li>Move from <code>0</code> to <code>5</code>: <code>15</code> fuel</li>
+<li>Move from <code>4</code> to <code>5</code>: <code>1</code> fuel</li>
+<li>Move from <code>2</code> to <code>5</code>: <code>6</code> fuel</li>
+<li>Move from <code>7</code> to <code>5</code>: <code>3</code> fuel</li>
+<li>Move from <code>1</code> to <code>5</code>: <code>10</code> fuel</li>
+<li>Move from <code>2</code> to <code>5</code>: <code>6</code> fuel</li>
+<li>Move from <code>14</code> to <code>5</code>: <code>45</code> fuel</li>
+</ul>
+<p>This costs a total of <code><em>168</em></code> fuel. This is the new cheapest possible outcome; the old alignment position (<code>2</code>) now costs <code>206</code> fuel instead.</p>
+<p>Determine the horizontal position that the crabs can align to using the least fuel possible so they can make you an escape route! <em>How much fuel must they spend to align to that position?</em></p>
+</article>
+<p>Your puzzle answer was <code>91638945</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="7/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+%22The+Treachery+of+Whales%22+%2D+Day+7+%2D+Advent+of+Code+2021&amp;url=https%3A%2F%2Fadventofcode%2Ecom%2F2021%2Fday%2F7&amp;related=ericwastl&amp;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+%22The+Treachery+of+Whales%22+%2D+Day+7+%2D+Advent+of+Code+2021+%23AdventOfCode+https%3A%2F%2Fadventofcode%2Ecom%2F2021%2Fday%2F7'}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