diff options
| author | Yigit Sever | 2022-12-06 14:47:43 +0300 |
|---|---|---|
| committer | Yigit Sever | 2022-12-06 14:47:43 +0300 |
| commit | 244d3dfa233371db0adcff329283bb1ce9bba9f5 (patch) | |
| tree | d6ec2d6682a45c823bf0ee9f921f861f3cefe118 /2022/day4/part1/src | |
| parent | ef7e8df2825480f34c1034015a8221c09f6ebdf6 (diff) | |
| download | aoc-244d3dfa233371db0adcff329283bb1ce9bba9f5.tar.gz aoc-244d3dfa233371db0adcff329283bb1ce9bba9f5.tar.bz2 aoc-244d3dfa233371db0adcff329283bb1ce9bba9f5.zip | |
2022, day4: done
Diffstat (limited to '2022/day4/part1/src')
| -rw-r--r-- | 2022/day4/part1/src/main.rs | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/2022/day4/part1/src/main.rs b/2022/day4/part1/src/main.rs new file mode 100644 index 0000000..a4b7e38 --- /dev/null +++ b/2022/day4/part1/src/main.rs | |||
| @@ -0,0 +1,50 @@ | |||
| 1 | use itertools::Itertools; | ||
| 2 | |||
| 3 | #[derive(Debug)] | ||
| 4 | struct Elf { | ||
| 5 | start: usize, | ||
| 6 | end: usize, | ||
| 7 | } | ||
| 8 | |||
| 9 | #[derive(Debug)] | ||
| 10 | struct Pair(Elf, Elf); | ||
| 11 | |||
| 12 | impl Elf { | ||
| 13 | pub fn new(range: &str) -> Self { | ||
| 14 | if let Some((start, end)) = range.to_string().split("-").collect_tuple() { | ||
| 15 | let start: usize = start.parse().unwrap(); | ||
| 16 | let end: usize = end.parse().unwrap(); | ||
| 17 | Self { start, end } | ||
| 18 | } else { | ||
| 19 | panic!("Expected two elements"); | ||
| 20 | } | ||
| 21 | } | ||
| 22 | } | ||
| 23 | |||
| 24 | impl Pair { | ||
| 25 | pub fn new(line: &str) -> Self { | ||
| 26 | if let Some((first, second)) = line.to_string().split(",").collect_tuple() { | ||
| 27 | Pair(Elf::new(first), Elf::new(second)) | ||
| 28 | } else { | ||
| 29 | panic!("Expected two elements"); | ||
| 30 | } | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | impl Pair { | ||
| 35 | fn contains(&self) -> bool { | ||
| 36 | return (self.0.start <= self.1.start && self.0.end >= self.1.end) | ||
| 37 | || (self.0.start >= self.1.start && self.0.end <= self.1.end); | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | fn main() { | ||
| 42 | let sum: usize = include_str!("../../example") | ||
| 43 | .lines() | ||
| 44 | .map(|l| Pair::new(l)) | ||
| 45 | .map(|p| p.contains()) | ||
| 46 | .filter(|r| *r) | ||
| 47 | .count(); | ||
| 48 | |||
| 49 | println!("{}", sum); | ||
| 50 | } | ||
