# AOC Day 7 Part 2 with Triangle Number Proof

unknown
rust
2 years ago
1.2 kB
11
Indexable
Never
```use std::cmp::{max, min};
use std::fs::File;

fn main() {
let file = File::open("./input.txt").unwrap();

let mut str_lines: Vec<String> = Vec::new();

for line in input_lines {
if let Ok(input) = line {
str_lines.push(input);
}
}

let mut numbers: Vec<u64> = Vec::new();
let mut max_num: u64 = 0;
let mut min_num: u64 = u64::MAX;

for line in str_lines {
for token in line.split(",") {
let num: u64 = token.parse().unwrap();
numbers.push(num);
max_num = max(max_num, num);
min_num = min(min_num, num);
}
}

let mut min_fuel: u64 = u64::MAX;
for i in min_num..max_num {
let mut fuel_spent: u64 = 0;
for num in &numbers {
let num_steps = if num > &i { num - i } else { i - num };
fuel_spent += calc_fuel_from_steps(num_steps);
}
min_fuel = min(min_fuel, fuel_spent);
}

println!("Min fuel {}", min_fuel);
}

fn calc_fuel_from_steps(steps: u64) -> u64 {
let fuel = (steps * (steps + 1)) / 2;
fuel
}```