Completed day 1

This commit is contained in:
Tyler Hallada 2019-12-03 21:53:39 -05:00
parent 1ef9d8b86a
commit f15f4abed9
6 changed files with 209 additions and 0 deletions

2
day1/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
**/*.rs.bk

6
day1/Cargo.lock generated Normal file
View File

@ -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"

9
day1/Cargo.toml Normal file
View File

@ -0,0 +1,9 @@
[package]
name = "day1"
version = "0.1.0"
authors = ["Tyler Hallada <tyler@hallada.net>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]

100
day1/input/input.txt Normal file
View File

@ -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

4
day1/input/test.txt Normal file
View File

@ -0,0 +1,4 @@
12
14
1969
100756

88
day1/src/main.rs Normal file
View File

@ -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<Vec<u32>> {
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>) -> u32 {
let fuel_requirements: Vec<u32> = masses
.iter()
.map(|mass| calculate_fuel_requirement(*mass))
.collect();
fuel_requirements.iter().sum()
}
fn calculate_fuel_sum_including_fuel_mass(masses: Vec<u32>) -> u32 {
let fuel_requirements: Vec<u32> = masses
.iter()
.map(|mass| calculate_fuel_requirement_including_fuel_mass(*mass))
.collect();
fuel_requirements.iter().sum()
}
fn solve_part1() -> io::Result<u32> {
Ok(calculate_fuel_sum(read_masses(INPUT)?))
}
fn solve_part2() -> io::Result<u32> {
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);
}
}