tangled
alpha
login
or
join now
nove.dev
/
aoc-2024
0
fork
atom
retroactive, to derust my rust
0
fork
atom
overview
issues
pulls
pipelines
day2 part2
nove.dev
3 months ago
bb45902f
0e4ac584
+38
-4
1 changed file
expand all
collapse all
unified
split
src
lib.rs
+38
-4
src/lib.rs
···
40
40
}
41
41
42
42
pub mod day2 {
43
43
-
pub fn day2_part1(input: &str) -> String {
44
44
-
let reports: Vec<Vec<u32>> = input
43
43
+
fn day2_preprocess(input: &str) -> Vec<Vec<u32>> {
44
44
+
input
45
45
.lines()
46
46
.map(|report| {
47
47
report
···
49
49
.map(|level| level.parse().unwrap())
50
50
.collect()
51
51
})
52
52
-
.collect();
53
53
-
52
52
+
.collect()
53
53
+
}
54
54
+
pub fn day2_part1(input: &str) -> String {
55
55
+
let reports = day2_preprocess(input);
54
56
reports
55
57
.into_iter()
56
58
.filter(gradual)
···
58
60
.count()
59
61
.to_string()
60
62
}
63
63
+
pub fn day2_part2(input: &str) -> String {
64
64
+
let reports = day2_preprocess(input);
65
65
+
reports
66
66
+
.into_iter()
67
67
+
.filter(should_filter_out_part2)
68
68
+
.count()
69
69
+
.to_string()
70
70
+
}
71
71
+
fn should_filter_out_part2(report: &Vec<u32>) -> bool {
72
72
+
if gradual(report) && monotonic(report) {
73
73
+
return true;
74
74
+
}
75
75
+
for index_to_skip in 0..report.len() {
76
76
+
let skipped_report: Vec<u32> = report
77
77
+
.iter()
78
78
+
.enumerate()
79
79
+
.filter(|(i, _)| *i != index_to_skip)
80
80
+
.map(|(_, elem)| *elem)
81
81
+
.collect();
82
82
+
if gradual(&skipped_report) && monotonic(&skipped_report) {
83
83
+
return true;
84
84
+
}
85
85
+
}
86
86
+
false
87
87
+
}
61
88
62
89
fn gradual(report: &Vec<u32>) -> bool {
63
90
for i in 1..report.len() {
···
95
122
assert_eq!(test_result, "2");
96
123
let result = day2::day2_part1(include_str!("../input/day2.txt"));
97
124
assert_eq!(result, "213");
125
125
+
}
126
126
+
#[test]
127
127
+
fn day2_part2_test() {
128
128
+
let test_result = day2::day2_part2(include_str!("../input/day2.test.txt"));
129
129
+
assert_eq!(test_result, "4");
130
130
+
let result = day2::day2_part2(include_str!("../input/day2.txt"));
131
131
+
assert_eq!(result, "285");
98
132
}
99
133
100
134
#[test]