Skip to content

Commit 41754a0

Browse files
Solve Day 2 Part 1
1 parent 6bf8003 commit 41754a0

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

data/examples/02.txt

+6
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

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
advent_of_code::solution!(2);
2+
3+
pub fn part_one(input: &str) -> Option<u32> {
4+
let reports: Vec<Vec<i32>> = input
5+
.lines()
6+
.filter_map(|line| {
7+
Some(
8+
line.split_whitespace()
9+
.filter_map(|x| x.parse::<i32>().ok())
10+
.collect::<Vec<i32>>(),
11+
)
12+
})
13+
.collect();
14+
15+
let num_safe: i32 = reports
16+
.iter()
17+
.filter(|report| {
18+
report.windows(2).all(|w| (1..=3).contains(&(w[1] - w[0])))
19+
|| report.windows(2).all(|w| (1..=3).contains(&(w[0] - w[1])))
20+
})
21+
.count() as i32;
22+
23+
Some(num_safe as u32)
24+
}
25+
26+
pub fn part_two(input: &str) -> Option<u32> {
27+
None
28+
}
29+
30+
#[cfg(test)]
31+
mod tests {
32+
use super::*;
33+
34+
#[test]
35+
fn test_part_one() {
36+
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
37+
assert_eq!(result, Some(2));
38+
}
39+
40+
#[test]
41+
fn test_part_two() {
42+
let result = part_two(&advent_of_code::template::read_file("examples", DAY));
43+
assert_eq!(result, None);
44+
}
45+
}

0 commit comments

Comments
 (0)