aoc2019/src/day1/main.rs
2020-04-19 01:56:59 +02:00

24 lines
527 B
Rust

use std::io::{self, BufRead};
fn main() -> io::Result<()> {
let mut total: i64 = 0;
io::stdin()
.lock()
.lines()
.filter_map(|l| l.ok())
.filter_map(|l| l.parse::<i64>().ok())
.for_each(|line| {
let mut cost = fuel_cost(line);
while cost > 0 {
total += cost;
cost = fuel_cost(cost);
}
});
println!("total fuel needed: {}", total);
Ok(())
}
fn fuel_cost(mass: i64) -> i64 {
mass / 3 - 2
}