Skip to content

Commit 89b2253

Browse files
committed
day 2
1 parent bd3594c commit 89b2253

File tree

5 files changed

+152
-0
lines changed

5 files changed

+152
-0
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ test_lib = []
2424
# Template dependencies
2525
chrono = { version = "0.4.38", optional = true }
2626
dhat = { version = "0.3.3", optional = true }
27+
heapless = "0.8.0"
2728
itertools = "0.13.0"
2829
pico-args = "0.5.0"
30+
rayon = "1.10.0"
2931
tinyjson = "2.5.1"
3032

3133
# Solution dependencies

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ Solutions for [Advent of Code](https://adventofcode.com/) in [Rust](https://www.
66

77
<!--- advent_readme_stars table --->
88

9+
<!--- benchmarking table --->
10+
## Benchmarks
11+
12+
| Day | Part 1 | Part 2 |
13+
| :---: | :---: | :---: |
14+
| [Day 1](./src/bin/01.rs) | `66.9µs` | `82.5µs` |
15+
| [Day 2](./src/bin/02.rs) | `363.4µs` | `690.8µs` |
16+
17+
**Total: 1.20ms**
918
<!--- benchmarking table --->
1019

1120
---

data/examples/02.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
7 6 4 2 1
2+
1 2 7 8 9
3+
9 7 6 2 1
4+
1 3 2 4 5
5+
8 6 4 4 1
6+
1 3 6 7 9

src/bin/02.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use heapless::Vec as HeaplessVec;
2+
advent_of_code::solution!(2);
3+
4+
const MAX_LEVELS: usize = 20;
5+
6+
fn parse_input<'a>(input: &'a str) -> impl Iterator<Item = HeaplessVec<u32, MAX_LEVELS>> + 'a {
7+
input.lines().map(|line| {
8+
line.split_whitespace()
9+
.map(|s| s.parse::<u32>().unwrap())
10+
.collect::<HeaplessVec<_, MAX_LEVELS>>()
11+
})
12+
}
13+
14+
#[inline]
15+
fn is_safe(level: &[u32]) -> bool {
16+
let increasing = level.windows(2).all(|w| w[0] <= w[1]);
17+
let decreasing = level.windows(2).all(|w| w[0] >= w[1]);
18+
let diff = level
19+
.windows(2)
20+
.all(|w| w[0].abs_diff(w[1]) >= 1 && w[0].abs_diff(w[1]) <= 3);
21+
(increasing || decreasing) && diff
22+
}
23+
24+
pub fn part_one(input: &str) -> Option<u32> {
25+
let c = parse_input(input).filter(|level| is_safe(level)).count();
26+
Some(c as u32)
27+
}
28+
29+
pub fn part_two(input: &str) -> Option<u32> {
30+
let c = parse_input(input)
31+
.filter(|level| {
32+
(0..level.len()).any(|i| {
33+
let mut l = level.clone();
34+
l.remove(i);
35+
is_safe(&l)
36+
})
37+
})
38+
.count();
39+
Some(c as u32)
40+
}
41+
42+
#[cfg(test)]
43+
mod tests {
44+
use super::*;
45+
46+
#[test]
47+
fn test_part_one() {
48+
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
49+
assert_eq!(result, Some(2));
50+
}
51+
52+
#[test]
53+
fn test_part_two() {
54+
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
55+
assert_eq!(result, Some(4));
56+
}
57+
}

0 commit comments

Comments
 (0)