git

My personal website source code
Log | Files | Refs | Submodules | README | LICENSE

commit 8cb020b3b128d5dbeb11ef758af80d2c44a12f9b
parent 63bcc7603d6b31bd55b232f137e0e24c87aa2eeb
Author: Luís Ferreira <contact@lsferreira.net>
Date:   Fri, 17 Dec 2021 23:35:06 +0000

posts: Add 'Solution in D for Advent of Code 2021, Day 7'

Signed-off-by: Luís Ferreira <contact@lsferreira.net>

Diffstat:
Acontent/posts/zet-8-aoc-2021-07.md | 70++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 70 insertions(+), 0 deletions(-)

diff --git a/content/posts/zet-8-aoc-2021-07.md b/content/posts/zet-8-aoc-2021-07.md @@ -0,0 +1,70 @@ +--- +title: 'Zettelkasten #8: Solution in D for Advent of Code 2021, Day 7' +date: '2021-12-17T22:25:00+01:00' +tags: ['zettelkasten', 'zet', 'dlang', 'aoc', 'aoc2021', 'adventofcode'] +description: "This post describes my solution in the D programming language for +the 7th puzzle of the Advent of Code 2021." +--- + +## The challenge + +> A giant whale has decided your submarine is its next meal, and it's much +> faster than you are. There's nowhere to run! +> +> 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 massive underground cave +> system just beyond where they're aiming! +> +> 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? +> +> There's one major catch - crab submarines can only move horizontally. +> +> You quickly make a list of the horizontal position of each crab (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. +> +> [...] +> +> Determine the horizontal position that the crabs can align to using the least +> fuel possible. How much fuel must they spend to align to that position? + +You can read the challenge more in depth, +[here](https://adventofcode.com/2021/day/7). + +## Part 1 + +### Full solution + +```d +auto input = stdin.byLineCopy().front.splitter(",").map!(to!long); // input +input.map!(a => input.map!(b => abs(a-b)).sum).minElement.writeln; // calculate costs +``` + +## Part 2 + +> The crabs don't seem interested in your proposed solution. Perhaps you +> misunderstand crab engineering? +> +> As it turns out, crab submarine engines don't burn fuel at a constant rate. +> Instead, each change of 1 step in horizontal position costs 1 more unit of +> fuel than the last: the first step costs 1, the second step costs 2, the +> third step costs 3, and so on. +> +> [...] +> +> Determine the horizontal position that the crabs can align to using the least +> fuel possible so they can make you an escape route! How much fuel must they +> spend to align to that position? + +### Full solution + +```d +auto input = stdin.byLineCopy().front.splitter(",").map!(to!long).array; // input +iota(input.maxElement).map!(a => input // iter 0 to max element + .map!(b => abs(a-b)*(abs(a-b)+1)/2).sum).minElement.writeln; // calculate costs +```