summary refs log tree commit diff stats
path: root/solutions/day4.fs
diff options
context:
space:
mode:
authorBrian Chu <brianmchu42@gmail.com>2022-12-03 21:44:55 -0800
committerBrian Chu <brianmchu42@gmail.com>2022-12-03 21:44:55 -0800
commitd47a200ff9bef16b5d0a6258dc0e963b44db366d (patch)
treef68b5c1b10d0bce418ba25d4dbb2f2065639d770 /solutions/day4.fs
parent7ba655e5c2519885907d542aae1c0daa73a6f1b8 (diff)
downloadAdventOfCode2022-d47a200ff9bef16b5d0a6258dc0e963b44db366d.tar.gz
solution for day 4
Diffstat (limited to 'solutions/day4.fs')
-rw-r--r--solutions/day4.fs35
1 files changed, 35 insertions, 0 deletions
diff --git a/solutions/day4.fs b/solutions/day4.fs
new file mode 100644
index 0000000..beb0515
--- /dev/null
+++ b/solutions/day4.fs
@@ -0,0 +1,35 @@
+namespace Solutions
+
+module Day4 =
+    open System.IO
+    open System.Text.RegularExpressions
+
+    type range = {
+        startID: int;
+        endID: int
+    }
+
+    let getRanges input =
+        let matches = Regex.Matches(input, "(\d+)-(\d+),(\d+)-(\d+)") 
+                        |> Seq.head 
+                        |> fun x -> x.Groups 
+                        |> Seq.map (fun x -> x.Value) 
+                        |> List.ofSeq 
+                        |> List.tail
+        assert (Seq.length matches = 4)
+        {startID = int matches[0]; endID = int matches[1]}, {startID = int matches[2]; endID = int matches[3]}
+        
+    let ranges = File.ReadLines("inputs/day4.txt") |> Seq.map getRanges
+
+    let contains (rangeA, rangeB) =
+        (rangeA.startID <= rangeB.startID && rangeA.endID >= rangeB.endID) || (rangeA.startID >= rangeB.startID && rangeA.endID <= rangeB.endID)
+
+    let part1 () = ranges |> Seq.filter contains |> Seq.length
+
+    let overlaps (rangeA, rangeB) =
+        (rangeA.startID >= rangeB.startID && rangeA.startID <= rangeB.endID) ||
+        (rangeA.endID >= rangeB.startID && rangeA.endID <= rangeB.endID)     ||
+        (rangeB.startID >= rangeA.startID && rangeB.startID <= rangeA.endID) ||
+        (rangeB.endID >= rangeA.startID && rangeB.endID <= rangeA.endID)
+    
+    let part2 () = ranges |> Seq.filter overlaps |> Seq.length
\ No newline at end of file