day 1 full
This commit is contained in:
5
src/main.rs
Normal file
5
src/main.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
mod tasks;
|
||||
|
||||
fn main() {
|
||||
tasks::day01::run();
|
||||
}
|
||||
36
src/tasks/day01.rs
Normal file
36
src/tasks/day01.rs
Normal 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
1
src/tasks/mod.rs
Normal file
@@ -0,0 +1 @@
|
||||
pub mod day01;
|
||||
Reference in New Issue
Block a user