aboutsummaryrefslogtreecommitdiff
path: root/2022
diff options
context:
space:
mode:
authorj-james2022-12-05 08:01:10 +0000
committerj-james2022-12-05 08:01:10 +0000
commit82be8473d21c15bc22c46cc4ba5463e3d2213c7d (patch)
treec09b49f5658b8363139ccc88825f1b2a5f882874 /2022
parentc278475cbbb71cbe182adcb1bc5776fa1f492655 (diff)
Day Four
Diffstat (limited to '2022')
-rw-r--r--2022/nim/day04/src/main.nim23
-rw-r--r--2022/rust/day04/Cargo.lock7
-rw-r--r--2022/rust/day04/Cargo.toml8
-rw-r--r--2022/rust/day04/src/main.rs43
4 files changed, 81 insertions, 0 deletions
diff --git a/2022/nim/day04/src/main.nim b/2022/nim/day04/src/main.nim
new file mode 100644
index 0000000..bb024fb
--- /dev/null
+++ b/2022/nim/day04/src/main.nim
@@ -0,0 +1,23 @@
+# Day 4: Camp Cleanup
+import std/[os, strutils, sequtils, sugar]
+
+let input = paramStr(1).readFile().strip().split("\n")
+
+let assign = input.mapIt(it.split(","))
+ .mapIt(it.mapIt(it.split("-").map(parseInt)))
+
+func contains(a, b: seq[int]): bool =
+ return a[0] in b[0] .. b[1] and
+ a[1] in b[0] .. b[1] or
+ b[0] in a[0] .. a[1] and
+ b[1] in a[0] .. a[1]
+
+echo assign.filterIt(contains(it[0], it[1])).len
+
+func overlaps(a, b: seq[int]): bool =
+ return a[0] in b[0] .. b[1] or
+ a[1] in b[0] .. b[1] or
+ b[0] in a[0] .. a[1] or
+ b[1] in a[0] .. a[1]
+
+echo assign.filterIt(overlaps(it[0], it[1])).len
diff --git a/2022/rust/day04/Cargo.lock b/2022/rust/day04/Cargo.lock
new file mode 100644
index 0000000..b8f9a18
--- /dev/null
+++ b/2022/rust/day04/Cargo.lock
@@ -0,0 +1,7 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "day04"
+version = "0.1.0"
diff --git a/2022/rust/day04/Cargo.toml b/2022/rust/day04/Cargo.toml
new file mode 100644
index 0000000..aa462dc
--- /dev/null
+++ b/2022/rust/day04/Cargo.toml
@@ -0,0 +1,8 @@
+[package]
+name = "day04"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
diff --git a/2022/rust/day04/src/main.rs b/2022/rust/day04/src/main.rs
new file mode 100644
index 0000000..3f8b9cd
--- /dev/null
+++ b/2022/rust/day04/src/main.rs
@@ -0,0 +1,43 @@
+use std::env;
+use std::fs;
+use std::ops::RangeInclusive;
+
+fn main() {
+ let args = env::args().nth(1).expect("missing input");
+ let input = fs::read_to_string(args).unwrap();
+ let assignments = input.trim().split('\n').map(|x| {
+ let mut vec = x.split(['-',',']).map(|x| x.parse::<i32>().unwrap());
+ return [vec.next().unwrap(), vec.next().unwrap(),
+ vec.next().unwrap(), vec.next().unwrap()];
+ }).collect::<Vec<[i32; 4]>>();
+
+ println!("{}", assignments.iter()
+ .filter(|x| contains(x[0] ..= x[1], x[2] ..= x[3])).count());
+
+ println!("{}", assignments.iter()
+ .filter(|x| overlaps(x[0] ..= x[1], x[2] ..= x[3])).count());
+}
+
+fn contains(a: RangeInclusive::<i32>, b: RangeInclusive::<i32>) -> bool {
+ return b.contains(a.start()) && b.contains(a.end()) ||
+ a.contains(b.start()) && a.contains(b.end());
+}
+
+fn overlaps(a: RangeInclusive::<i32>, b: RangeInclusive::<i32>) -> bool {
+ return b.contains(a.start()) || b.contains(a.end()) ||
+ a.contains(b.start()) || a.contains(b.end());
+}
+
+/*
+fn main() {
+ let args = env::args().nth(1).expect("missing input");
+ let input = fs::read_to_string(args).unwrap();
+ let assignments = input.trim().split('\n')
+ .map(|x| {x.split(['-',',']).collect()})
+ .collect::<Vec<Vec<&str>>>();
+
+ println!("{:?}", assignments);
+
+}
+
+*/