summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMartin Ashby <martin@ashbysoft.com>2022-12-04 08:52:58 +0000
committerMartin Ashby <martin@ashbysoft.com>2022-12-04 08:52:58 +0000
commit11a3e07268362bc1ee61b9d3f5fa6db6e12ac707 (patch)
treeaf3b5e36f7ae8c13a3968700c28d863480ac84d3 /src
parente973a7925aa06241bc03ab30f4d337c77bfe988f (diff)
downloadaoc2022-11a3e07268362bc1ee61b9d3f5fa6db6e12ac707.tar.gz
aoc2022-11a3e07268362bc1ee61b9d3f5fa6db6e12ac707.tar.bz2
aoc2022-11a3e07268362bc1ee61b9d3f5fa6db6e12ac707.tar.xz
aoc2022-11a3e07268362bc1ee61b9d3f5fa6db6e12ac707.zip
day4
Diffstat (limited to 'src')
-rw-r--r--src/day4.rs27
-rw-r--r--src/main.rs3
2 files changed, 30 insertions, 0 deletions
diff --git a/src/day4.rs b/src/day4.rs
new file mode 100644
index 0000000..46b9077
--- /dev/null
+++ b/src/day4.rs
@@ -0,0 +1,27 @@
+pub fn run(input: String) {
+ let count_overlaps = input.lines().map(|line| {
+ let (r1s, r2s) = line.split_once(",").expect("couldn't split on comma");
+ let (r1starts,r1ends) = r1s.split_once("-").expect("r1s no delimiter -");
+ let (r2starts,r2ends) = r2s.split_once("-").expect("r2s no delimiter -");
+ let r1start: u32 = r1starts.parse().expect("r1starts not a u32");
+ let r1end: u32 = r1ends.parse().expect("r1ends not a u32");
+ let r2start: u32 = r2starts.parse().expect("r2starts not a u32");
+ let r2end: u32 = r2ends.parse().expect("r2ends not a u32");
+ // fully contained (i.e. r1 is fully inside r2 or vice versa)
+ //let b1in2 = r1start >= r2start && r1end <= r2end;
+ //let b2in1 = r2start >= r1start && r2end <= r1end;
+ //return b1in2 || b2in1;
+
+ // partially contained (i.e. at least one end of r1 is contained in r2, or vice versa)
+ let e1 = r1start >= r2start && r1start <= r2end;
+ let e2 = r1end >= r2start && r1end <= r2end;
+ let e3 = r2start >= r1start && r2start <= r1end;
+ let e4 = r2end >= r1start && r2end <= r1end;
+ let res = e1 || e2 || e3 || e4;
+ //println!("{}: {}", line, res);
+ res
+ })
+ .filter(|x| {*x})
+ .count();
+ println!("Day 4: {}", count_overlaps);
+} \ No newline at end of file
diff --git a/src/main.rs b/src/main.rs
index 79c5970..c257999 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -4,8 +4,11 @@ mod day1;
mod day2;
mod day3;
+mod day4;
+
fn main() {
day1::run(fs::read_to_string("input/day1.txt").expect("Failed to read input file!"));
day2::run(fs::read_to_string("input/day2.txt").expect("Failed to read input file!"));
day3::run(fs::read_to_string("input/day3.txt").expect("Failed to read input file!"));
+ day4::run(fs::read_to_string("input/day4.txt").expect("Failed to read input file!"));
}