|
@@ -1,3 +1,37 @@
|
|
|
|
|
+use std::fs::File;
|
|
|
|
|
+use std::io::{BufRead, BufReader};
|
|
|
|
|
+use std::path::Path;
|
|
|
|
|
+
|
|
|
pub fn solve() {
|
|
pub fn solve() {
|
|
|
- println!("NYI");
|
|
|
|
|
|
|
+ let masses = read_masses("./src/day_1/puzzle_1.txt");
|
|
|
|
|
+ let mut sum: f32 = 0.0;
|
|
|
|
|
+
|
|
|
|
|
+ for mass in masses {
|
|
|
|
|
+ sum += calc_fuel_req(mass, 0.0);
|
|
|
|
|
+ }
|
|
|
|
|
+ println!("sum: {}", sum);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+fn calc_fuel_req(mass: f32, total: f32) -> f32 {
|
|
|
|
|
+ let fuel_req = (mass / 3.0).floor() - 2.0;
|
|
|
|
|
+
|
|
|
|
|
+ if fuel_req > 0.0 {
|
|
|
|
|
+ calc_fuel_req(fuel_req, total + fuel_req)
|
|
|
|
|
+ } else {
|
|
|
|
|
+ total
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+fn read_masses(filename: impl AsRef<Path>) -> Vec<f32> {
|
|
|
|
|
+ let file = File::open(filename).expect("no such file");
|
|
|
|
|
+ let buffer = BufReader::new(file);
|
|
|
|
|
+
|
|
|
|
|
+ buffer
|
|
|
|
|
+ .lines()
|
|
|
|
|
+ .map(|l| {
|
|
|
|
|
+ l.expect("could not read line")
|
|
|
|
|
+ .parse::<f32>()
|
|
|
|
|
+ .expect("could not parse line")
|
|
|
|
|
+ })
|
|
|
|
|
+ .collect()
|
|
|
}
|
|
}
|