init
This commit is contained in:
commit
1df50835be
7 changed files with 251 additions and 0 deletions
24
src/day1/main.rs
Normal file
24
src/day1/main.rs
Normal file
|
@ -0,0 +1,24 @@
|
|||
use std::io::{self, BufRead};
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
let mut total: i64 = 0;
|
||||
io::stdin()
|
||||
.lock()
|
||||
.lines()
|
||||
.filter_map(|l| l.ok())
|
||||
.filter_map(|l| l.parse::<i64>().ok())
|
||||
.for_each(|line| {
|
||||
let mut cost = fuel_cost(line);
|
||||
while cost > 0 {
|
||||
total += cost;
|
||||
cost = fuel_cost(cost);
|
||||
}
|
||||
});
|
||||
|
||||
println!("total fuel needed: {}", total);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn fuel_cost(mass: i64) -> i64 {
|
||||
mass / 3 - 2
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue