Day 7 part 1

This commit is contained in:
2021-12-07 11:30:25 -08:00
parent 186fc8e581
commit 091f53b1fe
3 changed files with 100 additions and 0 deletions

98
2021/src/day7.rs Normal file
View File

@@ -0,0 +1,98 @@
//! --- Day 7: The Treachery of Whales ---
//! 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.
//!
//! For example, consider the following horizontal positions:
//!
//! 16,1,2,0,4,2,7,1,2,14
//! This means there's a crab with horizontal position 16, a crab with horizontal position 1, and so on.
//!
//! 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 2:
//!
//! Move from 16 to 2: 14 fuel
//! Move from 1 to 2: 1 fuel
//! Move from 2 to 2: 0 fuel
//! Move from 0 to 2: 2 fuel
//! Move from 4 to 2: 2 fuel
//! Move from 2 to 2: 0 fuel
//! Move from 7 to 2: 5 fuel
//! Move from 1 to 2: 1 fuel
//! Move from 2 to 2: 0 fuel
//! Move from 14 to 2: 12 fuel
//! This costs a total of 37 fuel. This is the cheapest possible outcome; more expensive outcomes include aligning at position 1 (41 fuel), position 3 (39 fuel), or position 10 (71 fuel).
//!
//! 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?
//!
//!
use std::num::ParseIntError;
use anyhow::Result;
use aoc_runner_derive::{aoc, aoc_generator};
#[aoc_generator(day7)]
fn parse(input: &str) -> Result<Vec<u64>, ParseIntError> {
input
.split(',')
.map(|s| s.parse())
.collect::<Result<Vec<u64>, ParseIntError>>()
}
fn score(nums: &[u64], mid: u64) -> u64 {
nums.iter()
.map(|n| ((*n as i64) - (mid as i64)).abs())
.sum::<i64>() as u64
}
#[aoc(day7, part1)]
fn part1(input: &[u64]) -> Result<u64> {
let mut input: Vec<_> = input.to_vec();
input.sort_unstable();
Ok(score(&input, input[input.len() / 2]))
}
/*
#[aoc(day7, part2)]
fn part2(depths: &[u64]) -> Result<u64> {
todo!("part2")
Ok(())
}
*/
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_score() -> Result<()> {
let nums: Vec<u64> = parse("16,1,2,0,4,2,7,1,2,14")?;
assert_eq!(score(&nums, 1), 41);
assert_eq!(score(&nums, 2), 37);
assert_eq!(score(&nums, 3), 39);
assert_eq!(score(&nums, 10), 71);
Ok(())
}
#[test]
fn test_part1() -> Result<()> {
let input = r#"16,1,2,0,4,2,7,1,2,14"#.trim();
assert_eq!(part1(&parse(input)?)?, 37);
Ok(())
}
/*
#[test]
fn test_part2()->Result<()> {
let input = r#"
"#
.trim();
assert_eq!(part2(&parse(input)?)?, u64::MAX);
Ok(())
}
*/
}

View File

@@ -4,6 +4,7 @@ pub mod day3;
pub mod day4;
pub mod day5;
pub mod day6;
pub mod day7;
use aoc_runner_derive::aoc_lib;