mirror of
https://github.com/Ajetski/advent-of-code.git
synced 2025-09-30 13:03:19 -09:00
do day 4
This commit is contained in:
parent
493b28e3d9
commit
b90734ce25
1000
2022/input/day_4.txt
Normal file
1000
2022/input/day_4.txt
Normal file
File diff suppressed because it is too large
Load Diff
6
2022/input/day_4_sample.txt
Normal file
6
2022/input/day_4_sample.txt
Normal 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
39
2022/src/day_4.rs
Normal 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]
|
||||||
|
);
|
@ -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;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user