|
4 | 4 | //! |
5 | 5 | //! https://adventofcode.com/2022/day/4 |
6 | 6 |
|
| 7 | +use { |
| 8 | + crate::helpers::{read_lines, Answer}, |
| 9 | + std::time::SystemTime, |
| 10 | +}; |
| 11 | + |
| 12 | +/// solves the part 1 of day 04 and return its result and elapsed time |
| 13 | +pub fn pt1(filename: &str) -> Answer { |
| 14 | + let time = SystemTime::now(); |
| 15 | + |
| 16 | + let answer = read_lines(filename) |
| 17 | + .map(|line| parse_line(line).fully_contains()) |
| 18 | + .filter(|p| *p) |
| 19 | + .count(); |
| 20 | + |
| 21 | + Answer::new(answer as u32, time.elapsed().unwrap().as_millis() as u32) |
| 22 | +} |
| 23 | + |
| 24 | +/// solves the part 2 of day 04 and return its result and elapsed time |
| 25 | +pub fn pt2(filename: &str) -> Answer { |
| 26 | + let time = SystemTime::now(); |
| 27 | + |
| 28 | + let answer = read_lines(filename) |
| 29 | + .map(|line| parse_line(line).contains()) |
| 30 | + .filter(|p| *p) |
| 31 | + .count(); |
| 32 | + |
| 33 | + Answer::new(answer as u32, time.elapsed().unwrap().as_millis() as u32) |
| 34 | +} |
| 35 | + |
| 36 | +// parses a line into a SectionPair |
| 37 | +fn parse_line(line: String) -> SectionPair { |
| 38 | + let splits: Vec<u32> = line |
| 39 | + .replace(',', "-") |
| 40 | + .split('-') |
| 41 | + .map(|x| x.parse().unwrap()) |
| 42 | + .collect(); |
| 43 | + |
| 44 | + assert!(splits.len() == 4, "failed to parse line"); |
| 45 | + |
| 46 | + let first_section = Section::new(splits[0], splits[1]); |
| 47 | + let second_section = Section::new(splits[2], splits[3]); |
| 48 | + |
| 49 | + SectionPair::new(first_section, second_section) |
| 50 | +} |
| 51 | + |
| 52 | +/// represents a pair of `Section`s |
| 53 | +struct SectionPair { |
| 54 | + first: Section, |
| 55 | + second: Section, |
| 56 | +} |
| 57 | + |
| 58 | +impl SectionPair { |
| 59 | + // determines if one section fully contains the other |
| 60 | + fn fully_contains(&self) -> bool { |
| 61 | + self.first.start <= self.second.start && self.first.end >= self.second.end |
| 62 | + || self.first.start >= self.second.start && self.first.end <= self.second.end |
| 63 | + } |
| 64 | + |
| 65 | + // determines if one section contains the other |
| 66 | + fn contains(&self) -> bool { |
| 67 | + self.first.start <= self.second.end && self.first.end >= self.second.start |
| 68 | + || self.second.start <= self.first.end && self.second.end >= self.first.start |
| 69 | + } |
| 70 | + |
| 71 | + // constructor |
| 72 | + fn new(first: Section, second: Section) -> Self { |
| 73 | + SectionPair { first, second } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/// represents the start and the end of the section the elf needs to clean |
| 78 | +struct Section { |
| 79 | + start: u32, |
| 80 | + end: u32, |
| 81 | +} |
| 82 | + |
| 83 | +impl Section { |
| 84 | + // contructor |
| 85 | + fn new(start: u32, end: u32) -> Self { |
| 86 | + Section { start, end } |
| 87 | + } |
| 88 | +} |
| 89 | + |
7 | 90 | #[cfg(test)] |
8 | 91 | mod tests { |
9 | | - use super::*; |
| 92 | + use super::{parse_line, pt1, pt2}; |
| 93 | + |
| 94 | + const FILENAME: &str = "./data/examples/example04.txt"; |
| 95 | + |
| 96 | + #[test] |
| 97 | + fn test_pt1() { |
| 98 | + let answer = pt1(FILENAME); |
| 99 | + assert_eq!(2, answer.answer()); |
| 100 | + } |
| 101 | + |
| 102 | + #[test] |
| 103 | + fn test_pt2() { |
| 104 | + let answer = pt2(FILENAME); |
| 105 | + assert_eq!(4, answer.answer()); |
| 106 | + } |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn test_parse_line() { |
| 110 | + let line = String::from("2-4,6-8"); |
| 111 | + let pair = parse_line(line); |
| 112 | + |
| 113 | + assert_eq!(2, pair.first.start); |
| 114 | + assert_eq!(4, pair.first.end); |
| 115 | + assert_eq!(6, pair.second.start); |
| 116 | + assert_eq!(8, pair.second.end); |
| 117 | + } |
10 | 118 |
|
11 | | - // #[test] |
12 | | - // fn () { |
| 119 | + #[test] |
| 120 | + fn test_fully_contains() { |
| 121 | + let pair1 = parse_line(String::from("2-4,6-8")); |
| 122 | + let pair2 = parse_line(String::from("2-8,3-7")); |
13 | 123 |
|
14 | | - // } |
| 124 | + assert!(!pair1.fully_contains()); |
| 125 | + assert!(pair2.fully_contains()); |
| 126 | + } |
15 | 127 | } |
0 commit comments