mirror of
https://codeberg.org/JasterV/aoc_2021.git
synced 2026-04-26 18:40:05 +00:00
finish day 7
This commit is contained in:
parent
dd6b780f7f
commit
50cb5ba268
1 changed files with 6 additions and 1 deletions
|
|
@ -26,7 +26,12 @@ fn optimal_alignment_position(positions: &[i32]) -> (i32, i32) {
|
||||||
fn calculate_alignment_fuel_cost(align_to: i32, positions: &[i32]) -> i32 {
|
fn calculate_alignment_fuel_cost(align_to: i32, positions: &[i32]) -> i32 {
|
||||||
positions
|
positions
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|pos| i32::abs(align_to - pos))
|
// Get the distance between the 2 positions
|
||||||
|
.map(|pos| align_to - pos)
|
||||||
|
// Transform into absolute distance
|
||||||
|
.map(i32::abs)
|
||||||
|
// Calculate sum from 0 to n
|
||||||
|
.map(|n| n * (n + 1) / 2)
|
||||||
.sum()
|
.sum()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue