summaryrefslogtreecommitdiffstats
path: root/day04b/src
diff options
context:
space:
mode:
authorShivesh Mandalia <mail@shivesh.org>2022-12-20 23:33:51 +0000
committerShivesh Mandalia <mail@shivesh.org>2022-12-20 23:33:51 +0000
commita667c19882e1feb542a122781b4efe5545dea0dd (patch)
treed7f8a0aa645ad5181995e93381e5c0cf9122cd42 /day04b/src
parent5941c6c20ab337194514a5d2bf2b9b9c8b0b3d32 (diff)
downloadadvent_of_code_2022-a667c19882e1feb542a122781b4efe5545dea0dd.tar.gz
advent_of_code_2022-a667c19882e1feb542a122781b4efe5545dea0dd.zip
complete day 4
Diffstat (limited to 'day04b/src')
-rw-r--r--day04b/src/main.rs57
1 files changed, 57 insertions, 0 deletions
diff --git a/day04b/src/main.rs b/day04b/src/main.rs
new file mode 100644
index 0000000..31fc46f
--- /dev/null
+++ b/day04b/src/main.rs
@@ -0,0 +1,57 @@
+/// --- Part Two ---
+///
+/// It seems like there is still quite a bit of duplicate work planned. Instead, the Elves would
+/// like to know the number of pairs that overlap at all.
+///
+/// In the above example, the first two pairs (2-4,6-8 and 2-3,4-5) don't overlap, while the
+/// remaining four pairs (5-7,7-9, 2-8,3-7, 6-6,4-6, and 2-6,4-8) do overlap:
+///
+/// 5-7,7-9 overlaps in a single section, 7.
+/// 2-8,3-7 overlaps all of the sections 3 through 7.
+/// 6-6,4-6 overlaps in a single section, 6.
+/// 2-6,4-8 overlaps in sections 4, 5, and 6.
+///
+/// So, in this example, the number of overlapping assignment pairs is 4.
+///
+/// In how many assignment pairs do the ranges overlap?
+use clap::Parser;
+use itertools::Itertools;
+
+use std::fs::File;
+use std::io::prelude::*;
+use std::io::BufReader;
+use std::path::PathBuf;
+
+const FILEPATH: &'static str = "examples/input.txt";
+
+#[derive(Parser, Debug)]
+#[clap(author, version, about, long_about = None)]
+struct Cli {
+ #[clap(short, long, default_value = FILEPATH)]
+ file: PathBuf,
+}
+
+fn main() {
+ let args = Cli::parse();
+
+ let file = File::open(&args.file).unwrap();
+ let reader = BufReader::new(file);
+ let res = reader
+ .lines()
+ .map(|l| {
+ l.unwrap()
+ .split(',')
+ .map(|s| {
+ s.split('-')
+ .map(|v| v.parse::<usize>().unwrap())
+ .collect_tuple::<(usize, usize)>()
+ .unwrap()
+ })
+ .collect_tuple::<((usize, usize), (usize, usize))>()
+ .unwrap()
+ })
+ .filter(|((l1, l2), (r1, r2))| !(l1 > r2 || r1 > l2))
+ .count();
+
+ println!("{res}");
+}