Last active
December 1, 2019 17:43
-
-
Save itsHobbes/b7550da31e371911279e3e5476f2be0d to your computer and use it in GitHub Desktop.
AOC Day 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use crate::util::util; | |
use std::vec::Vec; | |
pub fn execute() { | |
let filename = String::from("day1.txt"); | |
let masses: Vec<u32> = util::read_file(filename); | |
println!("Part one answer: {}", part_one(&masses)); | |
println!("Part two answer: {}", part_two(&masses)); | |
} | |
fn part_one(masses: &Vec<u32>) -> u32 { | |
return masses.iter().map(|mass| calculate_fuel(*mass)).sum(); | |
} | |
fn calculate_fuel(mass: u32) -> u32 { | |
return (mass / 3).saturating_sub(2); | |
} | |
fn part_two(masses: &Vec<u32>) -> u32 { | |
let mut sum: u32 = 0; | |
for mass in masses { | |
let mut result: u32 = *mass; | |
while result > 0 { | |
result = calculate_fuel(result); | |
sum += result; | |
} | |
} | |
return sum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment