Skip to content

Commit 5d5c709

Browse files
committed
solve: day1
1 parent c3ce51c commit 5d5c709

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
resolver = "2"
33
members = [
44
"aoc_lib/aoc_derive",
5-
"aoc_lib/utils",
5+
"aoc_lib/utils", "day1",
66
]
77

88
[workspace.dependencies]

day1/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "day1"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]
7+
aoc_derive.path = '../aoc_lib/aoc_derive'
8+
utils.path = '../aoc_lib/utils'
9+
derive_more.workspace = true
10+
itertools.workspace = true
11+
lazy-regex.workspace = true
12+
parse-display.workspace = true
13+
rayon.workspace = true
14+
regex.workspace = true
15+
num.workspace = true
16+
17+
[dev-dependencies]
18+
pretty_assertions.workspace = true

day1/src/main.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use aoc_derive::aoc_main;
2+
use utils::*;
3+
4+
#[aoc_main]
5+
fn solve(input: Input) -> impl Into<Solution> {
6+
let (_, password, password2) =
7+
input.lines().fold((50, 0, 0), |(dial, password, password2), line| {
8+
let inc = if line.starts_with('L') { -1 } else { 1 };
9+
let steps: i64 = line[1..].parse().unwrap();
10+
11+
let (dial, password2) =
12+
(0..steps).fold((dial, password2), |(dial, password2), _| match dial + inc {
13+
0 => (0, password2 + 1),
14+
100 => (0, password2 + 1),
15+
-1 => (99, password2),
16+
next => (next, password2),
17+
});
18+
let password = if dial == 0 { password + 1 } else { password };
19+
20+
(dial, password, password2)
21+
});
22+
(password, password2)
23+
}
24+
25+
#[cfg(test)]
26+
mod tests {
27+
use super::*;
28+
#[test]
29+
fn test_examples() {
30+
use utils::assert_example;
31+
assert_part2!("R1000", 10);
32+
assert_part2!("L1000", 10);
33+
assert_part2!("R50", 1);
34+
assert_part2!("L50", 1);
35+
36+
assert_example!(
37+
r#"R49
38+
R1"#,
39+
1
40+
);
41+
42+
assert_example!(
43+
r#"L68
44+
L30
45+
R48
46+
L5
47+
R60
48+
L55
49+
L1
50+
L99
51+
R14
52+
L82"#,
53+
3,
54+
6
55+
);
56+
}
57+
}

0 commit comments

Comments
 (0)