Skip to content

Commit b6c1c53

Browse files
committed
2019 day 1
1 parent 3fcbc0d commit b6c1c53

File tree

7 files changed

+84
-2
lines changed

7 files changed

+84
-2
lines changed

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/aoc/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ year2015 = { path = "../year2015", optional = true }
1414
year2016 = { path = "../year2016", optional = true }
1515
year2017 = { path = "../year2017", optional = true }
1616
year2018 = { path = "../year2018", optional = true }
17+
year2019 = { path = "../year2019", optional = true }
1718
year2024 = { path = "../year2024", optional = true }
1819

1920
[features]
@@ -22,8 +23,8 @@ const_lut = ["year2024?/const_lut"]
2223
all-simd = ["utils/all-simd"]
2324
test-runner = []
2425
# xtask update features
25-
all-years = ["year2015", "year2016", "year2017", "year2018", "year2024"]
26-
unsafe = ["year2015?/unsafe", "year2016?/unsafe", "year2017?/unsafe", "year2018?/unsafe", "year2024?/unsafe", "utils/unsafe"]
26+
all-years = ["year2015", "year2016", "year2017", "year2018", "year2019", "year2024"]
27+
unsafe = ["year2015?/unsafe", "year2016?/unsafe", "year2017?/unsafe", "year2018?/unsafe", "year2019?/unsafe", "year2024?/unsafe", "utils/unsafe"]
2728

2829
[lints]
2930
workspace = true

crates/aoc/src/years.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ pub mod year2017 {
2727
pub mod year2018 {
2828
pub use ::utils::puzzles_noop as puzzles;
2929
}
30+
#[cfg(not(feature = "year2019"))]
31+
pub mod year2019 {
32+
pub use ::utils::puzzles_noop as puzzles;
33+
}
3034
#[cfg(not(feature = "year2024"))]
3135
pub mod year2024 {
3236
pub use ::utils::puzzles_noop as puzzles;
@@ -39,6 +43,8 @@ pub use ::year2016;
3943
pub use ::year2017;
4044
#[cfg(feature = "year2018")]
4145
pub use ::year2018;
46+
#[cfg(feature = "year2019")]
47+
pub use ::year2019;
4248
#[cfg(feature = "year2024")]
4349
pub use ::year2024;
4450

@@ -111,6 +117,7 @@ macro_rules! all_puzzles {
111117
$crate::year2016::puzzles,
112118
$crate::year2017::puzzles,
113119
$crate::year2018::puzzles,
120+
$crate::year2019::puzzles,
114121
$crate::year2024::puzzles,
115122

116123
$callback

crates/year2019/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "year2019"
3+
authors = { workspace = true }
4+
edition = { workspace = true }
5+
license = { workspace = true }
6+
publish = { workspace = true }
7+
repository = { workspace = true }
8+
rust-version = { workspace = true }
9+
10+
[dependencies]
11+
utils = { path = "../utils", default-features = false }
12+
13+
[features]
14+
unsafe = ["utils/unsafe"]

crates/year2019/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Solutions for [Advent of Code 2019](https://adventofcode.com/2019)

crates/year2019/src/day01.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use utils::prelude::*;
2+
3+
/// Summing values from a recursive formula.
4+
#[derive(Clone, Debug)]
5+
pub struct Day01 {
6+
modules: Vec<u32>,
7+
}
8+
9+
impl Day01 {
10+
pub fn new(input: &str, _: InputType) -> Result<Self, InputError> {
11+
Ok(Self {
12+
modules: parser::u32().parse_lines(input)?,
13+
})
14+
}
15+
16+
#[must_use]
17+
pub fn part1(&self) -> u32 {
18+
self.modules
19+
.iter()
20+
.map(|mass| (mass / 3).saturating_sub(2))
21+
.sum()
22+
}
23+
24+
#[must_use]
25+
pub fn part2(&self) -> u32 {
26+
self.modules
27+
.iter()
28+
.map(|&(mut mass)| {
29+
let mut fuel = 0;
30+
while mass > 0 {
31+
mass = (mass / 3).saturating_sub(2);
32+
fuel += mass;
33+
}
34+
fuel
35+
})
36+
.sum()
37+
}
38+
}
39+
40+
examples!(Day01 -> (u32, u32) [
41+
{input: "12", part1: 2, part2: 2},
42+
{input: "14", part1: 2, part2: 2},
43+
{input: "1969", part1: 654, part2: 966},
44+
{input: "100756", part1: 33583, part2: 50346},
45+
]);

crates/year2019/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#![doc = include_str!("../README.md")]
2+
#![cfg_attr(not(feature = "unsafe"), forbid(unsafe_code))]
3+
4+
utils::year!(2019 => year2019, ${
5+
1 => day01::Day01,
6+
});

0 commit comments

Comments
 (0)