day 1 full

This commit is contained in:
Johannes
2019-12-01 09:23:56 +01:00
commit 7dd992b93a
7 changed files with 159 additions and 0 deletions

36
src/tasks/day01.rs Normal file
View File

@@ -0,0 +1,36 @@
use std::fs;
pub fn run() {
let content = fs::read_to_string("input/day01.txt").unwrap();
task1(&content);
task2(&content);
}
fn task1(content: &String) {
let fuel: i32 = content
.lines()
.map(|line| line.parse::<i32>().unwrap())
.map(|mass| mass / 3 - 2)
.sum();
println!("Task 1: {}", fuel);
}
fn task2(content: &String) {
fn while_fun(mass: i32) -> i32 {
let to_add = mass / 3 - 2;
if to_add < 1 {
0
} else {
to_add + while_fun(to_add)
}
};
let fuel: i32 = content
.lines()
.map(|line| line.parse::<i32>().unwrap())
.map(while_fun)
.sum();
println!("Task 2: {}", fuel);
}

1
src/tasks/mod.rs Normal file
View File

@@ -0,0 +1 @@
pub mod day01;