From f1250271ead104c031eebbd4532ba271e6ab6620 Mon Sep 17 00:00:00 2001 From: Daniel Flanagan Date: Sat, 3 Dec 2022 23:18:55 -0600 Subject: [PATCH] Parts 1 and 2 --- 2022/rust/Cargo.toml | 4 ++++ 2022/rust/src/day4.rs | 50 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/2022/rust/Cargo.toml b/2022/rust/Cargo.toml index 72be5db..13d5820 100644 --- a/2022/rust/Cargo.toml +++ b/2022/rust/Cargo.toml @@ -18,4 +18,8 @@ path = "src/day2-alt1.rs" name = "day3" path = "src/day3.rs" +[[bin]] +name = "day4" +path = "src/day4.rs" + [dependencies] diff --git a/2022/rust/src/day4.rs b/2022/rust/src/day4.rs index 0826245..1300d6d 100644 --- a/2022/rust/src/day4.rs +++ b/2022/rust/src/day4.rs @@ -7,26 +7,66 @@ fn main() { } fn part1(input: &str) -> i32 { - 0 + let mut r = 0; + for l in input.lines() { + let mut c = l.split(","); + let p1 = c.next().unwrap(); + let p2 = c.next().unwrap(); + let mut p1c = p1.split("-"); + let p1n1 = p1c.next().unwrap().parse::().unwrap(); + let p1n2 = p1c.next().unwrap().parse::().unwrap(); + let mut p2c = p2.split("-"); + let p2n1 = p2c.next().unwrap().parse::().unwrap(); + let p2n2 = p2c.next().unwrap().parse::().unwrap(); + + if (p1n1 <= p2n1 && p1n2 >= p2n2) || (p2n1 <= p1n1 && p2n2 >= p1n2) { + r += 1 + } + } + r } fn part2(input: &str) -> i32 { - 0 + let mut r = 0; + for l in input.lines() { + println!("{}", l); + let mut c = l.split(","); + let p1 = c.next().unwrap(); + let p2 = c.next().unwrap(); + let mut p1c = p1.split("-"); + let x1 = p1c.next().unwrap().parse::().unwrap(); + let x2 = p1c.next().unwrap().parse::().unwrap(); + let mut p2c = p2.split("-"); + let y1 = p2c.next().unwrap().parse::().unwrap(); + let y2 = p2c.next().unwrap().parse::().unwrap(); + + println!("{}-{} , {}-{}", x1, x2, y1, y2); + if x1 <= y2 && y1 <= x2 { + r += 1; + println!("overlap"); + } + } + r } #[cfg(test)] mod tests { use super::*; - const TEST_INPUT: &str = ""; + const TEST_INPUT: &str = "2-4,6-8 +2-3,4-5 +5-7,7-9 +2-8,3-7 +6-6,4-6 +2-6,4-8"; #[test] fn test_part1() { - assert_eq!(part1(TEST_INPUT), 0) + assert_eq!(part1(TEST_INPUT), 2) } #[test] fn test_part2() { - assert_eq!(part2(TEST_INPUT), 0) + assert_eq!(part2(TEST_INPUT), 4) } }