Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 0.76 KB | None | 0 0
  1. use std::io::{BufReader};
  2. use std::io::prelude::*;
  3. use std::fs::File;
  4.  
  5. fn fuel_need(weight:i32) -> i32{
  6.     return weight / 3 - 2;
  7. }
  8.  
  9. fn main() {
  10.     let f = BufReader::new(File::open("data/input_1a.sdx").unwrap()); //This will crash if the file can't be found
  11.  
  12.     let mut modules : Vec<i32> = Vec::new();
  13.  
  14.     for line in f.lines() {
  15.         modules.push(line.unwrap().parse::<i32>().unwrap())
  16.     }
  17.  
  18.     let mut total : i32 = 0;
  19.     for m  in modules {
  20.         let module_weight =  fuel_need(m);
  21.         total += module_weight;
  22.  
  23.         let mut fuel = module_weight;
  24.  
  25.         while fuel > 0 {
  26.             fuel = fuel_need(fuel);
  27.  
  28.             if fuel > 0 {
  29.                 total += fuel;
  30.             }
  31.         }
  32.     }
  33.  
  34.     println!("{}", total);
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement