Skip to content

Commit c243e3e

Browse files
committed
2017 day 1
1 parent 4a9b2f7 commit c243e3e

File tree

7 files changed

+88
-2
lines changed

7 files changed

+88
-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
@@ -12,12 +12,13 @@ utils = { path = "../utils" }
1212
# xtask update dependencies
1313
year2015 = { path = "../year2015", optional = true }
1414
year2016 = { path = "../year2016", optional = true }
15+
year2017 = { path = "../year2017", optional = true }
1516

1617
[features]
1718
default = ["all-years", "unsafe"]
1819
# xtask update features
19-
all-years = ["year2015", "year2016"]
20-
unsafe = ["year2015?/unsafe", "year2016?/unsafe", "utils/unsafe"]
20+
all-years = ["year2015", "year2016", "year2017"]
21+
unsafe = ["year2015?/unsafe", "year2016?/unsafe", "year2017?/unsafe", "utils/unsafe"]
2122

2223
[lints]
2324
workspace = true

crates/aoc/src/years.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,16 @@ pub mod year2015 {
1919
pub mod year2016 {
2020
pub use ::utils::puzzles_noop as puzzles;
2121
}
22+
#[cfg(not(feature = "year2017"))]
23+
pub mod year2017 {
24+
pub use ::utils::puzzles_noop as puzzles;
25+
}
2226
#[cfg(feature = "year2015")]
2327
pub use ::year2015;
2428
#[cfg(feature = "year2016")]
2529
pub use ::year2016;
30+
#[cfg(feature = "year2017")]
31+
pub use ::year2017;
2632

2733
/// Macro which invokes a callback macro with a list of all implemented puzzle solutions.
2834
///
@@ -91,6 +97,7 @@ macro_rules! all_puzzles {
9197
// xtask update all_puzzles
9298
$crate::year2015::puzzles,
9399
$crate::year2016::puzzles,
100+
$crate::year2017::puzzles,
94101

95102
$callback
96103
]

crates/year2017/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "year2017"
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" }
12+
13+
[features]
14+
unsafe = ["utils/unsafe"]

crates/year2017/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 2017](https://adventofcode.com/2017)

crates/year2017/src/day01.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use utils::prelude::*;
2+
3+
/// Adding matching digits.
4+
#[derive(Clone, Debug)]
5+
pub struct Day01<'a> {
6+
input: &'a [u8],
7+
}
8+
9+
impl<'a> Day01<'a> {
10+
pub fn new(input: &'a str, _: InputType) -> Result<Self, InputError> {
11+
if let Some(b) = input.bytes().find(|b| !b.is_ascii_digit()) {
12+
Err(InputError::new(input, b as char, "expected digit"))
13+
} else {
14+
Ok(Self {
15+
input: input.as_bytes(),
16+
})
17+
}
18+
}
19+
20+
#[must_use]
21+
pub fn part1(&self) -> u32 {
22+
self.input
23+
.iter()
24+
.zip(self.input.iter().cycle().skip(1))
25+
.map(|(&a, &b)| if a == b { (a - b'0') as u32 } else { 0 })
26+
.sum()
27+
}
28+
29+
#[must_use]
30+
pub fn part2(&self) -> u32 {
31+
self.input
32+
.iter()
33+
.zip(self.input.iter().cycle().skip(self.input.len() / 2))
34+
.map(|(&a, &b)| if a == b { (a - b'0') as u32 } else { 0 })
35+
.sum()
36+
}
37+
}
38+
39+
examples!(Day01<'_> -> (u32, u32) [
40+
{input: "1122", part1: 3},
41+
{input: "1111", part1: 4},
42+
{input: "1234", part1: 0},
43+
{input: "91212129", part1: 9},
44+
{input: "1212", part2: 6},
45+
{input: "1221", part2: 0},
46+
{input: "123425", part2: 4},
47+
{input: "123123", part2: 12},
48+
{input: "12131415", part2: 4},
49+
]);

crates/year2017/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!(2017 => year2017, ${
5+
1 => day01::Day01<'_>,
6+
});

0 commit comments

Comments
 (0)