Initial commit

This commit is contained in:
2019-12-01 17:58:57 +01:00
parent 57e9a6e7d7
commit 16fdb976d9
4 changed files with 150 additions and 0 deletions
+9
View File
@@ -0,0 +1,9 @@
[package]
name = "day1"
version = "0.1.0"
authors = ["Tobias Eidelpes <tobias@eidelpes.info>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
+30
View File
@@ -0,0 +1,30 @@
use std::fs::File;
use std::env;
use std::io::{Error, BufRead, BufReader};
fn main() -> Result<(), Error> {
let args: Vec<String> = env::args().collect();
let filename = &args[1];
let reader = BufReader::new(File::open(filename).unwrap());
let mut result: i64 = 0;
for line in reader.lines() {
let n = line.unwrap().parse::<i64>().unwrap();
result += calculate_fuel(n);
}
println!("Result: {}", result);
Ok(())
}
fn calculate_fuel(mass: i64) -> i64 {
let fuel = mass / 3 - 2;
if fuel < 0 {
return 0;
} else {
return (fuel) + calculate_fuel(fuel);
}
}