diff --git a/day1/.gitignore b/day1/.gitignore new file mode 100644 index 0000000..53eaa21 --- /dev/null +++ b/day1/.gitignore @@ -0,0 +1,2 @@ +/target +**/*.rs.bk diff --git a/day1/Cargo.lock b/day1/Cargo.lock new file mode 100644 index 0000000..da01ae3 --- /dev/null +++ b/day1/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "day1" +version = "0.1.0" + diff --git a/day1/Cargo.toml b/day1/Cargo.toml new file mode 100644 index 0000000..18c2a28 --- /dev/null +++ b/day1/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "day1" +version = "0.1.0" +authors = ["Tyler Hallada "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/day1/input/input.txt b/day1/input/input.txt new file mode 100644 index 0000000..d883837 --- /dev/null +++ b/day1/input/input.txt @@ -0,0 +1,100 @@ +109506 +140405 +139135 +110950 +84296 +123991 +59438 +85647 +81214 +100517 +100910 +57704 +83368 +50777 +85523 +95788 +127699 +138908 +95502 +81703 +67317 +108468 +58394 +72202 +121580 +86908 +72705 +86578 +83714 +114900 +142915 +51332 +69054 +97039 +143539 +61143 +113534 +98335 +58533 +83893 +127138 +50844 +88397 +133591 +83563 +52435 +96342 +109491 +81148 +127397 +86200 +92418 +144842 +120142 +97531 +54449 +91004 +129115 +142487 +68513 +140405 +80111 +139359 +57486 +116973 +135102 +59737 +144040 +95483 +134470 +60473 +113142 +78189 +53845 +124139 +78055 +63791 +99879 +58630 +111233 +80544 +76932 +79644 +116247 +54646 +85217 +110795 +142095 +74492 +93318 +122300 +82755 +147407 +98697 +98105 +132055 +67856 +109731 +75747 +135700 diff --git a/day1/input/test.txt b/day1/input/test.txt new file mode 100644 index 0000000..f620e23 --- /dev/null +++ b/day1/input/test.txt @@ -0,0 +1,4 @@ +12 +14 +1969 +100756 diff --git a/day1/src/main.rs b/day1/src/main.rs new file mode 100644 index 0000000..83f99f5 --- /dev/null +++ b/day1/src/main.rs @@ -0,0 +1,88 @@ +use std::fs::File; +use std::io::{self, prelude::*, BufReader}; + +const INPUT: &str = "input/input.txt"; + +fn read_masses(filename: &str) -> io::Result> { + let file = File::open(filename)?; + let reader = BufReader::new(file); + + Ok(reader + .lines() + .map(|mass| mass.unwrap().parse().unwrap()) + .collect()) +} + +fn calculate_fuel_requirement(mass: u32) -> u32 { + mass / 3 - 2 +} + +fn calculate_fuel_requirement_including_fuel_mass(mass: u32) -> u32 { + let mut fuel: i32 = mass as i32 / 3 - 2; + let mut total_requirement = 0; + + while fuel > 0 { + total_requirement += fuel as u32; + fuel = fuel / 3 - 2; + } + + total_requirement +} + +fn calculate_fuel_sum(masses: Vec) -> u32 { + let fuel_requirements: Vec = masses + .iter() + .map(|mass| calculate_fuel_requirement(*mass)) + .collect(); + fuel_requirements.iter().sum() +} + +fn calculate_fuel_sum_including_fuel_mass(masses: Vec) -> u32 { + let fuel_requirements: Vec = masses + .iter() + .map(|mass| calculate_fuel_requirement_including_fuel_mass(*mass)) + .collect(); + fuel_requirements.iter().sum() +} + +fn solve_part1() -> io::Result { + Ok(calculate_fuel_sum(read_masses(INPUT)?)) +} + +fn solve_part2() -> io::Result { + Ok(calculate_fuel_sum_including_fuel_mass(read_masses(INPUT)?)) +} + +fn main() -> io::Result<()> { + println!("Part 1: {}", solve_part1()?); + println!("Part 2: {}", solve_part2()?); + + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + const TEST_INPUT: &str = "input/test.txt"; + + #[test] + fn reads_masses() { + assert_eq!(read_masses(TEST_INPUT).unwrap(), vec![12, 14, 1969, 100756]); + } + + #[test] + fn calculates_correct_fuel_requirements() { + assert_eq!(calculate_fuel_requirement(12), 2); + assert_eq!(calculate_fuel_requirement(14), 2); + assert_eq!(calculate_fuel_requirement(1969), 654); + assert_eq!(calculate_fuel_requirement(100756), 33583); + } + + #[test] + fn calculates_correct_fuel_requirements_including_fuel_mass() { + assert_eq!(calculate_fuel_requirement_including_fuel_mass(14), 2); + assert_eq!(calculate_fuel_requirement_including_fuel_mass(1969), 966); + assert_eq!(calculate_fuel_requirement_including_fuel_mass(100756), 50346); + } +}