Skip to content

Instantly share code, notes, and snippets.

@neofight78
Created December 7, 2021 07:29
Show Gist options
  • Save neofight78/80737969e9d01f6ad08e2a30d4e42c1f to your computer and use it in GitHub Desktop.
Save neofight78/80737969e9d01f6ad08e2a30d4e42c1f to your computer and use it in GitHub Desktop.
Advent of Code 2021 - Day 7: The Treachery of Whales
fn align(positions: &[i64]) -> i64 {
(*positions.iter().min().unwrap()..=*positions.iter().max().unwrap())
.map(|target| positions.iter().map(|&p| (p - target).abs()).sum())
.min()
.unwrap()
}
fn align2(positions: &[i64]) -> i64 {
(*positions.iter().min().unwrap()..=*positions.iter().max().unwrap())
.map(|target| {
positions
.iter()
.map(|&p| {
let diff = (p - target).abs();
(diff.pow(2) + diff) / 2
})
.sum()
})
.min()
.unwrap()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment