aoc-2021-rust

Advent of Code 2021 Solutions in Rust
git clone https://git.sinitax.com/sinitax/aoc-2021-rust
Log | Files | Refs | README | sfeed.txt

part1 (2062B)


      1--- Day 7: The Treachery of Whales ---
      2
      3A giant whale has decided your submarine is its next meal, and it's much faster than you are.
      4There's nowhere to run!
      5
      6Suddenly, a swarm of crabs (each in its own tiny submarine - it's too deep for them otherwise) zooms
      7in to rescue you! They seem to be preparing to blast a hole in the ocean floor; sensors indicate a
      8massive underground cave system just beyond where they're aiming!
      9
     10The crab submarines all need to be aligned before they'll have enough power to blast a large enough
     11hole for your submarine to get through. However, it doesn't look like they'll be aligned before the
     12whale catches you! Maybe you can help?
     13
     14There's one major catch - crab submarines can only move horizontally.
     15
     16You quickly make a list of the horizontal position of each crab (your puzzle input). Crab submarines
     17have limited fuel, so you need to find a way to make all of their horizontal positions match while
     18requiring them to spend as little fuel as possible.
     19
     20For example, consider the following horizontal positions:
     21
     2216,1,2,0,4,2,7,1,2,14
     23This means there's a crab with horizontal position 16, a crab with horizontal position 1, and so on.
     24
     25Each change of 1 step in horizontal position of a single crab costs 1 fuel. You could choose any
     26horizontal position to align them all on, but the one that costs the least fuel is horizontal
     27position 2:
     28
     29
     30 - Move from 16 to 2: 14 fuel
     31
     32 - Move from 1 to 2: 1 fuel
     33
     34 - Move from 2 to 2: 0 fuel
     35
     36 - Move from 0 to 2: 2 fuel
     37
     38 - Move from 4 to 2: 2 fuel
     39
     40 - Move from 2 to 2: 0 fuel
     41
     42 - Move from 7 to 2: 5 fuel
     43
     44 - Move from 1 to 2: 1 fuel
     45
     46 - Move from 2 to 2: 0 fuel
     47
     48 - Move from 14 to 2: 12 fuel
     49
     50
     51This costs a total of 37 fuel. This is the cheapest possible outcome; more expensive outcomes
     52include aligning at position 1 (41 fuel), position 3 (39 fuel), or position 10 (71 fuel).
     53
     54Determine the horizontal position that the crabs can align to using the least fuel possible.
     55How much fuel must they spend to align to that position?
     56
     57