generated from fspoettel/advent-of-code-rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2497bb8
commit fe2c3d3
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
7 6 4 2 1 | ||
1 2 7 8 9 | ||
9 7 6 2 1 | ||
1 3 2 4 5 | ||
8 6 4 4 1 | ||
1 3 6 7 9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
advent_of_code::solution!(2); | ||
|
||
fn is_safe(levels: &[i32]) -> bool { | ||
let is_increasing = levels.windows(2).all(|pair| pair[1] > pair[0]); | ||
let is_decreasing = levels.windows(2).all(|pair| pair[1] < pair[0]); | ||
|
||
if !is_increasing && !is_decreasing { | ||
return false; | ||
} | ||
|
||
levels.windows(2).all(|pair| { | ||
let diff = (pair[1] - pair[0]).abs(); | ||
(1..=3).contains(&diff) | ||
}) | ||
} | ||
|
||
pub fn part_one(input: &str) -> Option<u32> { | ||
let count = input | ||
.lines() | ||
.filter(|report| { | ||
let levels: Vec<i32> = report | ||
.split_whitespace() | ||
.map(|n| n.parse::<i32>().expect("Invalid number")) | ||
.collect(); | ||
|
||
is_safe(&levels) | ||
}) | ||
.count(); | ||
|
||
Some(count as u32) | ||
} | ||
|
||
pub fn part_two(input: &str) -> Option<u32> { | ||
let count = input | ||
.lines() | ||
.filter(|report| { | ||
let levels: Vec<i32> = report | ||
.split_whitespace() | ||
.map(|n| n.parse::<i32>().expect("Invalid number")) | ||
.collect(); | ||
|
||
if is_safe(&levels) { | ||
return true; | ||
} | ||
|
||
for i in 0..levels.len() { | ||
let mut modified = levels.clone(); | ||
modified.remove(i); | ||
|
||
if is_safe(&modified) { | ||
return true; | ||
} | ||
} | ||
false | ||
}) | ||
.count(); | ||
|
||
Some(count as u32) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_part_one() { | ||
let result = part_one(&advent_of_code::template::read_file("examples", DAY)); | ||
assert_eq!(result, Some(2)); | ||
} | ||
|
||
#[test] | ||
fn test_part_two() { | ||
let result = part_two(&advent_of_code::template::read_file("examples", DAY)); | ||
assert_eq!(result, Some(4)); | ||
} | ||
} |