This commit is contained in:
Daniel Flanagan 2022-12-04 01:44:06 -06:00
parent 0ad08975e6
commit 8dbd0cf9af
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4
2 changed files with 23 additions and 36 deletions

View file

@ -15,3 +15,6 @@ pub fn show_answers(answer1: &impl Debug, answer2: &impl Debug) {
println!("Part 1: {:?}", answer1); println!("Part 1: {:?}", answer1);
println!("Part 2: {:?}", answer2); println!("Part 2: {:?}", answer2);
} }
pub fn show_both_answers((a, b): &(impl Debug, impl Debug)) {
show_answers(a, b)
}

View file

@ -1,56 +1,40 @@
mod common; mod common;
fn main() { fn processed_input(input: &str) -> Vec<Vec<i32>> {
let input = common::day_input(4);
println!("Part 1: {}", part1(&input));
println!("Part 2: {}", part2(&input));
}
fn nums(input: &str) -> Vec<i32> {
input input
.split(&['-', ',']) .lines()
.map(|s| s.parse::<i32>().unwrap()) .map(|l| l.split(&['-', ',']).map(|s| s.parse().unwrap()).collect())
.collect() .collect()
} }
fn part1(input: &str) -> i32 { fn both_parts(input: &Vec<Vec<i32>>) -> (usize, usize) {
input input.iter().fold((0, 0), |(a, b), n| {
.lines() (
.map(|l| { (a + ((n[0] <= n[2] && n[1] >= n[3]) || (n[2] <= n[0] && n[3] >= n[1])) as usize),
let n = nums(l); b + ((n[0] <= n[3] && n[2] <= n[1]) as usize),
((n[0] <= n[2] && n[1] >= n[3]) || (n[2] <= n[0] && n[3] >= n[1])) as i32 )
}) })
.sum()
} }
fn part2(input: &str) -> i32 { fn main() {
input let input = processed_input(&common::day_input(4));
.lines() common::show_both_answers(&both_parts(&input))
.map(|l| {
let n = nums(l);
(n[0] <= n[3] && n[2] <= n[1]) as i32
})
.sum()
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
const TEST_INPUT: &str = "2-4,6-8 #[test]
fn test() {
let input = processed_input(
"2-4,6-8
2-3,4-5 2-3,4-5
5-7,7-9 5-7,7-9
2-8,3-7 2-8,3-7
6-6,4-6 6-6,4-6
2-6,4-8"; 2-6,4-8",
);
#[test] assert_eq!(both_parts(&input), (2, 4));
fn test_part1() {
assert_eq!(part1(TEST_INPUT), 2)
}
#[test]
fn test_part2() {
assert_eq!(part2(TEST_INPUT), 4)
} }
} }