Skip to content

Commit

Permalink
Year 2019 Day 1
Browse files Browse the repository at this point in the history
  • Loading branch information
maneatingape committed Aug 29, 2023
1 parent 76c7dcc commit f177178
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 1 deletion.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pie
* [2022](#2022) (19 ms)
* [2021](#2021) (11 ms)
* [2020](#2020) (286 ms)
* [2015](#2015) (in progress)
* [Earlier](#2019) (in progress)

## 2022

Expand Down Expand Up @@ -189,6 +189,12 @@ pie
| 24 | [Lobby Layout](https://adventofcode.com/2020/day/24) | [Source](src/year2020/day24.rs) | 4346 |
| 25 | [Combo Breaker](https://adventofcode.com/2020/day/25) | [Source](src/year2020/day25.rs) | 24 |

## 2019

| Day | Problem | Solution | Benchmark (μs) |
| --- | --- | --- | --: |
| 1 | [The Tyranny of the Rocket Equation](https://adventofcode.com/2019/day/1) | [Source](src/year2019/day01.rs) | 1 |

## 2015

| Day | Problem | Solution | Benchmark (μs) |
Expand Down
4 changes: 4 additions & 0 deletions benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ mod year2020 {
benchmark!(year2020, day25);
}

mod year2019 {
benchmark!(year2019, day01);
}

mod year2015 {
benchmark!(year2015, day01);
benchmark!(year2015, day02);
Expand Down
100 changes: 100 additions & 0 deletions input/year2019/day01.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
73617
104372
131825
85022
105514
78478
87420
118553
97680
89479
146989
79746
108085
117895
143811
102509
102382
92975
72978
94208
130521
83042
138605
107566
63374
71176
129487
118408
115425
63967
98282
121829
92834
61084
70122
87221
132773
141347
133225
81199
94994
60881
110074
63499
143107
76618
86818
135394
106908
96085
99801
112903
51751
56002
70924
62180
133025
68025
122660
64898
77339
62109
133891
134460
84224
54836
59748
125540
67796
71845
92899
130103
74612
136820
96212
132002
97405
82629
63717
62805
112693
147810
139827
116220
69711
50236
137833
103743
147456
112098
84867
75615
132738
81072
89444
58443
94465
112494
82127
132533
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ pub mod year2020 {
pub mod day25;
}

/// # Rescue Santa from deep space with a solar system adventure.
pub mod year2019 {
pub mod day01;
}

/// # Help Santa by solving puzzles to fix the weather machine's snow function.
pub mod year2015 {
pub mod day01;
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ fn all_solutions() -> Vec<Solution> {
solution!(year2020, day23),
solution!(year2020, day24),
solution!(year2020, day25),
// 2019
solution!(year2019, day01),
// 2015
solution!(year2015, day01),
solution!(year2015, day02),
Expand Down
34 changes: 34 additions & 0 deletions src/year2019/day01.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//! # The Tyranny of the Rocket Equation
//!
//! The title of the problem is a reference to the
//! [real life equation](https://en.wikipedia.org/wiki/Tsiolkovsky_rocket_equation).
use crate::util::parse::*;

/// The [`iter_unsigned`] utility method extracts and parses numbers from surrounding text.
///
/// [`iter_unsigned`]: crate::util::parse
pub fn parse(input: &str) -> Vec<u32> {
input.iter_unsigned().collect()
}

/// Calculate fuel requirements following the formula.
pub fn part1(input: &[u32]) -> u32 {
input.iter().map(|mass| mass / 3 - 2).sum()
}

/// Calculate the fuel requirements taking into account that fuel needs more fuel to lift it.
/// Mass of 8 or below results in zero or negative fuel so we can stop.
pub fn part2(input: &[u32]) -> u32 {
input
.iter()
.copied()
.map(|mut mass| {
let mut fuel = 0;
while mass > 8 {
mass = mass / 3 - 2;
fuel += mass;
}
fuel
})
.sum()
}
4 changes: 4 additions & 0 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ mod year2020 {
mod day25_test;
}

mod year2019 {
mod day01_test;
}

mod year2015 {
mod day01_test;
mod day02_test;
Expand Down
15 changes: 15 additions & 0 deletions tests/year2019/day01_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use aoc::year2019::day01::*;

const EXAMPLE: &str = "100756";

#[test]
fn part1_test() {
let input = parse(EXAMPLE);
assert_eq!(part1(&input), 33583);
}

#[test]
fn part2_test() {
let input = parse(EXAMPLE);
assert_eq!(part2(&input), 50346);
}

0 comments on commit f177178

Please sign in to comment.