This commit is contained in:
Adam Jeniski 2022-12-04 16:10:27 -05:00
parent 493b28e3d9
commit b90734ce25
4 changed files with 1046 additions and 0 deletions

1000
2022/input/day_4.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,6 @@
2-4,6-8
2-3,4-5
5-7,7-9
2-8,3-7
6-6,4-6
2-6,4-8

39
2022/src/day_4.rs Normal file
View File

@ -0,0 +1,39 @@
advent_of_code_macro::solve_problem!(
day 4,
Input Vec<Vec<i32>>,
parse |input: &str| {
input.lines().map(|line| {
let (a, rest) = line.split_once('-').unwrap();
let (b, rest) = rest.split_once(',').unwrap();
let (c, d) = rest.split_once('-').unwrap();
[a, b, c, d].into_iter()
.map(str::parse)
.map(Result::unwrap)
.collect()
}).collect()
},
part one |data: Input| {
data.into_iter().fold(0, |acc, curr|
if (curr[0] <= curr[2] && curr[1] >= curr[3])
|| (curr[2] <= curr[0] && curr[3] >= curr[1]) {
acc + 1
} else {
acc
}
)
},
part two |data: Input| {
data.into_iter().fold(0, |acc, curr|
if (curr[0] >= curr[2] && curr[0] <= curr[3])
|| (curr[1] >= curr[2] && curr[1] <= curr[3])
|| (curr[2] >= curr[0] && curr[2] <= curr[1])
|| (curr[3] >= curr[0] && curr[3] <= curr[1]) {
acc + 1
} else {
acc
}
)
},
sample tests [2, 4],
star tests [450, 837]
);

View File

@ -2,3 +2,4 @@
mod day_1; mod day_1;
mod day_2; mod day_2;
mod day_3; mod day_3;
mod day_4;