Camp Cleanup
Description: https://adventofcode.com/2022/day/4
open System.IO
open System
type Input() =
let parseRanges (line: string) : (int * int) * (int * int) =
let ranges =
line.Split ','
|> Seq.map (fun x -> x.Split '-' |> (fun y -> (y[0] |> int, y[1] |> int)))
|> Seq.sortBy (fun (x, _) -> x)
|> Array.ofSeq
ranges[0], ranges[1]
let includes (r1: int * int, r2: int * int) : int =
if fst r1 < fst r2 then
if snd r1 >= snd r2 then 1 else 0
else if fst r1 = fst r2 then
1
else
0
let overlap (r1: int * int, r2: int * int) : int =
if fst r1 < fst r2 then
if snd r1 >= fst r2 then 1 else 0
else if fst r1 = fst r2 then
1
else
0
let computePart1 (line: string) : int = line |> parseRanges |> includes
let computePart2 (line: string) : int = line |> parseRanges |> overlap
member _.Compute(part: int) =
use stream = new StreamReader "./input.txt"
let mutable sum = 0
let mutable endOfFile = false
while (not endOfFile) do
let line = stream.ReadLine()
if (line = null) then endOfFile <- true
else if (part = 1) then sum <- sum + computePart1 line
else sum <- sum + computePart2 line
sum
let input = Input()
let part = (Environment.GetCommandLineArgs()[1]) |> int
printfn "%d" (input.Compute(part))