add day 1 and 2 solutions

This commit is contained in:
george
2024-06-18 15:06:38 +01:00
commit a1b380717c
11 changed files with 197 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
use std::io::{self, BufRead};
fn main() {
let nums: Vec<u64> = io::stdin()
.lock()
.lines()
.map(|line| line.unwrap().parse::<u64>().unwrap())
.collect();
let p1: u64 = nums.iter().map(|&num| mass_to_fuel(num)).sum();
let p2: u64 = nums.iter().map(|&num| fuel_for_fuel(num)).sum();
println!("P1: {}, P2: {}", p1, p2);
}
fn mass_to_fuel(mass: u64) -> u64 {
(mass / 3).saturating_sub(2)
}
fn fuel_for_fuel(fuel: u64) -> u64 {
let mut tot = 0;
let mut fuel = fuel;
while fuel > 0 {
fuel = mass_to_fuel(fuel);
tot += fuel;
}
tot
}