This commit is contained in:
Patrick Stevens
2022-12-04 09:04:19 +00:00
committed by GitHub
parent 3b88eaf39a
commit 659c67a3e5
5 changed files with 1091 additions and 0 deletions

36
AdventOfCode2022/Day4.fs Normal file
View File

@@ -0,0 +1,36 @@
namespace AdventOfCode2022
open System
[<RequireQualifiedAccess>]
module Day4 =
/// This only checks if the first arg fully contains the second arg.
let inline fullyContains (a, b) (c, d) : bool = a <= c && b >= d
let inline overlaps (a, b) (c, d) : bool = b >= c && a <= d
let parse (s : ReadOnlySpan<char>) : (int * int) * (int * int) =
let commaIndex = s.IndexOf ','
let firstElf = s.Slice (0, commaIndex)
let secondElf = s.Slice (commaIndex + 1)
let firstDashIndex = firstElf.IndexOf '-'
let firstElf1 = firstElf.Slice (0, firstDashIndex)
let firstElf2 = firstElf.Slice (firstDashIndex + 1)
let secondDashIndex = secondElf.IndexOf '-'
let secondElf1 = secondElf.Slice (0, secondDashIndex)
let secondElf2 = secondElf.Slice (secondDashIndex + 1)
(Int32.Parse firstElf1, Int32.Parse firstElf2), (Int32.Parse secondElf1, Int32.Parse secondElf2)
let part1 (lines : string seq) : int =
lines
|> Seq.map (fun s -> parse (s.AsSpan ()))
|> Seq.filter (fun (firstElf, secondElf) -> fullyContains firstElf secondElf || fullyContains secondElf firstElf
)
|> Seq.length
let part2 (lines : string seq) : int =
lines
|> Seq.map (fun s -> parse (s.AsSpan ()))
|> Seq.filter (fun (firstElf, secondElf) -> overlaps firstElf secondElf)
|> Seq.length