-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02.rs
More file actions
97 lines (88 loc) · 2.73 KB
/
02.rs
File metadata and controls
97 lines (88 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use itertools::Itertools;
advent_of_code::solution!(2);
enum Direction {
Ascending,
Descending,
}
pub fn part_one(input: &str) -> Option<u32> {
let result: Vec<bool> = input.lines()
.map(|line| {
let nums: Vec<u32> = line.split_ascii_whitespace()
.map(|num| num.parse::<u32>().unwrap())
.collect();
let dir = if nums[0] > nums[1] { Direction::Descending } else { Direction::Ascending };
let instances = nums.iter().tuple_windows().all(|(a, b)| {
let calc = a.abs_diff(*b);
if calc == 0 {
return false;
}
let ret = match dir {
Direction::Ascending => *a < *b && calc >= 1 && calc <= 3,
Direction::Descending => *a > *b && calc >= 1 && calc <= 3,
};
ret
});
instances
})
.collect();
Some(result.iter().filter(|&&num| num == true).count() as u32)
}
// Time to rethink implementation...
pub fn part_two(input: &str) -> Option<u32> {
let mut counter = 0;
for line in input.lines() {
let nums: Vec<u32> = line.split_ascii_whitespace()
.map(|num| num.parse::<u32>().unwrap())
.collect();
if is_safe(&nums) {
counter += 1;
} else {
for i in 0..nums.len() {
let new_nums = copy_without_index(&nums, &i);
if is_safe(&new_nums) {
counter += 1;
break;
}
}
}
}
Some(counter)
}
fn is_safe(vec: &Vec<u32>) -> bool {
let dir = if vec[0] > vec[1] { Direction::Descending } else { Direction::Ascending };
let instances = vec.iter().tuple_windows().all(|(a, b)| {
let calc = a.abs_diff(*b);
if calc == 0 {
return false;
}
let ret = match dir {
Direction::Ascending => *a < *b && calc >= 1 && calc <= 3,
Direction::Descending => *a > *b && calc >= 1 && calc <= 3,
};
ret
});
instances
}
fn copy_without_index(vec: &Vec<u32>, i: &usize) -> Vec<u32> {
let mut new_vec = Vec::with_capacity(vec.len() - 1);
for (index, value) in vec.iter().enumerate() {
if index != *i {
new_vec.push(*value);
}
}
new_vec
}
#[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));
}
}