day_1.rs 655 B

123456789101112131415161718192021222324252627
  1. fn total_fuel(mass: f32) -> i32 {
  2. let mut req_fuel = get_fuel_cost(mass);
  3. let mut fuel = req_fuel;
  4. while get_fuel_cost(req_fuel as f32) > 0{
  5. req_fuel = get_fuel_cost(req_fuel as f32);
  6. fuel += req_fuel;
  7. }
  8. fuel
  9. }
  10. fn get_fuel_cost(mass: f32) -> i32{
  11. ((mass / 3.0).floor() - 2.0) as i32
  12. }
  13. pub fn main() {
  14. let contents = std::fs::read_to_string("input_day_1.txt").expect("Scream Internally");
  15. let lines = contents.split("\r\n");
  16. let mut all_f = 0;
  17. for l in lines {
  18. let x: f32 = l.parse().unwrap();
  19. all_f += total_fuel(x);
  20. }
  21. println!("{}", all_f);
  22. }