From f177178ef96529dbc064985aaefd776c3760ab71 Mon Sep 17 00:00:00 2001 From: maneatingape <44142177+maneatingape@users.noreply.github.com> Date: Thu, 17 Aug 2023 03:14:14 +0200 Subject: [PATCH] Year 2019 Day 1 --- README.md | 8 ++- benches/benchmark.rs | 4 ++ input/year2019/day01.txt | 100 +++++++++++++++++++++++++++++++++++ src/lib.rs | 5 ++ src/main.rs | 2 + src/year2019/day01.rs | 34 ++++++++++++ tests/test.rs | 4 ++ tests/year2019/day01_test.rs | 15 ++++++ 8 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 input/year2019/day01.txt create mode 100644 src/year2019/day01.rs create mode 100644 tests/year2019/day01_test.rs diff --git a/README.md b/README.md index b6fd3e1..960e348 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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) | diff --git a/benches/benchmark.rs b/benches/benchmark.rs index c7ecc0f..82399cc 100644 --- a/benches/benchmark.rs +++ b/benches/benchmark.rs @@ -119,6 +119,10 @@ mod year2020 { benchmark!(year2020, day25); } +mod year2019 { + benchmark!(year2019, day01); +} + mod year2015 { benchmark!(year2015, day01); benchmark!(year2015, day02); diff --git a/input/year2019/day01.txt b/input/year2019/day01.txt new file mode 100644 index 0000000..5e2ab33 --- /dev/null +++ b/input/year2019/day01.txt @@ -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 diff --git a/src/lib.rs b/src/lib.rs index 0be7792..413ae49 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/main.rs b/src/main.rs index d77bfa9..697f981 100644 --- a/src/main.rs +++ b/src/main.rs @@ -159,6 +159,8 @@ fn all_solutions() -> Vec { solution!(year2020, day23), solution!(year2020, day24), solution!(year2020, day25), + // 2019 + solution!(year2019, day01), // 2015 solution!(year2015, day01), solution!(year2015, day02), diff --git a/src/year2019/day01.rs b/src/year2019/day01.rs new file mode 100644 index 0000000..038d5c0 --- /dev/null +++ b/src/year2019/day01.rs @@ -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 { + 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() +} diff --git a/tests/test.rs b/tests/test.rs index f2d503e..a12debd 100644 --- a/tests/test.rs +++ b/tests/test.rs @@ -82,6 +82,10 @@ mod year2020 { mod day25_test; } +mod year2019 { + mod day01_test; +} + mod year2015 { mod day01_test; mod day02_test; diff --git a/tests/year2019/day01_test.rs b/tests/year2019/day01_test.rs new file mode 100644 index 0000000..e199107 --- /dev/null +++ b/tests/year2019/day01_test.rs @@ -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); +}