Add part one of day 2
Some checks failed
Continuous Integration / Continuous Integration (push) Has been cancelled
Some checks failed
Continuous Integration / Continuous Integration (push) Has been cancelled
This commit is contained in:
parent
ae09ba2cc7
commit
6a3a324ea1
2 changed files with 24 additions and 2 deletions
|
@ -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
|
|
@ -1,7 +1,23 @@
|
||||||
advent_of_code::solution!(2);
|
advent_of_code::solution!(2);
|
||||||
|
|
||||||
pub fn part_one(input: &str) -> Option<u32> {
|
pub fn part_one(input: &str) -> Option<u32> {
|
||||||
None
|
let mut num_safe: u32 = 0;
|
||||||
|
let lines = input.lines();
|
||||||
|
for line in lines {
|
||||||
|
let levels: Vec<u32> = line
|
||||||
|
.split_whitespace()
|
||||||
|
.filter_map(|i| i.parse::<u32>().ok())
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
if levels
|
||||||
|
.windows(2)
|
||||||
|
.all(|w| (w[0] > w[1] && w[0] - w[1] <= 3) || (w[1] > w[0] && w[1] - w[0] <= 3))
|
||||||
|
&& (levels.windows(2).all(|w| w[0] < w[1]) || levels.windows(2).all(|w| w[0] > w[1]))
|
||||||
|
{
|
||||||
|
num_safe += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(num_safe)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn part_two(input: &str) -> Option<u32> {
|
pub fn part_two(input: &str) -> Option<u32> {
|
||||||
|
@ -15,7 +31,7 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_part_one() {
|
fn test_part_one() {
|
||||||
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
|
let result = part_one(&advent_of_code::template::read_file("examples", DAY));
|
||||||
assert_eq!(result, None);
|
assert_eq!(result, Some(2));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Add table
Reference in a new issue