From 4f2d2acd52ccd896f2bf22e8093b3d6832cafc0a Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Wed, 13 Dec 2023 12:54:46 +0000 Subject: [PATCH] Tidy --- .../AdventOfCode2023.FSharp.Bench/Inputs.fs | 2 +- .../AdventOfCode2023.FSharp.Bench/Program.fs | 2 +- .../AdventOfCode2023.FSharp.Bench/Run.fs | 13 ++++ .../AdventOfCode2023.FSharp.Lib/Day13.fs | 65 +++++++++---------- 4 files changed, 47 insertions(+), 35 deletions(-) diff --git a/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Bench/Inputs.fs b/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Bench/Inputs.fs index 5646ffa..11956a7 100644 --- a/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Bench/Inputs.fs +++ b/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Bench/Inputs.fs @@ -14,6 +14,6 @@ module Inputs = if isNull dir then failwith "reached root of filesystem without finding inputs dir" - Array.init 12 (fun day -> Path.Combine (dir.FullName, "inputs", $"day%i{day + 1}.txt") |> File.ReadAllText) + Array.init 13 (fun day -> Path.Combine (dir.FullName, "inputs", $"day%i{day + 1}.txt") |> File.ReadAllText) let inline day (i : int) = days.[i - 1] diff --git a/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Bench/Program.fs b/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Bench/Program.fs index 8a2d560..7395103 100644 --- a/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Bench/Program.fs +++ b/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Bench/Program.fs @@ -44,7 +44,7 @@ module Benchmarks = [] member _.Setup () = Run.shouldWrite <- false - [] + [] member val Day = 0 with get, set [] diff --git a/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Bench/Run.fs b/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Bench/Run.fs index d3ffbe0..4746311 100644 --- a/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Bench/Run.fs +++ b/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Bench/Run.fs @@ -173,6 +173,18 @@ module Run = if shouldWrite then Console.WriteLine output + let day13 (partTwo : bool) (input : string) = + if not partTwo then + let output = Day13.part1 input + + if shouldWrite then + Console.WriteLine output + else + let output = Day13.part2 input + + if shouldWrite then + Console.WriteLine output + let allRuns = [| day1 @@ -187,4 +199,5 @@ module Run = day10 day11 day12 + day13 |] diff --git a/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day13.fs b/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day13.fs index c597297..dad9fd8 100644 --- a/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day13.fs +++ b/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day13.fs @@ -33,7 +33,7 @@ module Day13 = answer - let verifyHorizontalReflection (group : ResizeArray<'a>) (smaller : int) (bigger : int) : bool = + let verifyReflection (group : ResizeArray<'a>) (smaller : int) (bigger : int) : bool = let midPoint = (smaller + bigger) / 2 let rec isOkWithin (curr : int) = @@ -50,11 +50,12 @@ module Day13 = smaller = 0 || bigger = group.Count - 1 - /// Find reflection among rows + /// Find reflection among rows. + /// Returns 0 to indicate "no answer". [] - let rec findRow (banAnswer : uint32) (rows : ResizeArray) (currentLine : int) = + let rec findRow (banAnswer : uint32) (rows : ResizeArray) (currentLine : int) : uint32 = if currentLine = rows.Count - 1 then - None + 0ul else let mutable answer = UInt32.MaxValue let mutable i = currentLine @@ -64,7 +65,7 @@ module Day13 = if currentLine % 2 <> i % 2 then if rows.[i] = rows.[currentLine] then - if verifyHorizontalReflection rows currentLine i then + if verifyReflection rows currentLine i then let desiredAnswer = uint32 (((currentLine + i) / 2) + 1) if desiredAnswer <> banAnswer then @@ -72,7 +73,7 @@ module Day13 = i <- Int32.MaxValue if answer < UInt32.MaxValue then - Some answer + answer else findRow banAnswer rows (currentLine + 1) @@ -88,7 +89,8 @@ module Day13 = if not row.IsEmpty then rowBuf.Add (rowToInt row) - let solve (banAnswer : uint32) (rowBuf : ResizeArray<_>) (colBuf : ResizeArray<_>) : uint32 option = + /// Returns 0 to indicate "no solution". + let solve (banAnswer : uint32) (rowBuf : ResizeArray<_>) (colBuf : ResizeArray<_>) : uint32 = match findRow (if banAnswer >= 100ul then @@ -98,8 +100,18 @@ module Day13 = rowBuf 0 with - | Some rowIndex -> Some (100ul * rowIndex) - | None -> findRow banAnswer colBuf 0 + | rowIndex when rowIndex > 0ul -> 100ul * rowIndex + | _ -> findRow banAnswer colBuf 0 + + /// Returns also the group with this gro + let peelGroup (s : ReadOnlySpan) : ReadOnlySpan = + let index = s.IndexOf "\n\n" + + if index < 0 then + // last group + s + else + s.Slice (0, index + 1) let part1 (s : string) = let mutable s = s.AsSpan () @@ -108,26 +120,20 @@ module Day13 = let mutable answer = 0ul while not s.IsEmpty do - let index = s.IndexOf "\n\n" - - let group = - if index < 0 then - // last group - s - else - s.Slice (0, index + 1) + let group = peelGroup s render rows cols group - answer <- answer + Option.get (solve UInt32.MaxValue rows cols) + // There's an obvious perf optimisation where we don't compute cols + // until we know there's no row answer. Life's too short. + answer <- answer + solve UInt32.MaxValue rows cols - if index < 0 then + if group.Length >= s.Length then s <- ReadOnlySpan.Empty else - s <- s.Slice (index + 2) + s <- s.Slice (group.Length + 1) answer - // 358 90 385 385 90 102 346 let flipAt (rows : ResizeArray<_>) (cols : ResizeArray<_>) (rowNum : int) (colNum : int) : unit = rows.[rowNum] <- let index = 1ul <<< (cols.Count - colNum - 1) @@ -152,18 +158,11 @@ module Day13 = let mutable answer = 0ul while not s.IsEmpty do - let index = s.IndexOf "\n\n" - - let group = - if index < 0 then - // last group - s - else - s.Slice (0, index + 1) + let group = peelGroup s render rows cols group - let bannedAnswer = solve UInt32.MaxValue rows cols |> Option.get + let bannedAnswer = solve UInt32.MaxValue rows cols let mutable isDone = false let mutable rowToChange = 0 @@ -175,7 +174,7 @@ module Day13 = flipAt rows cols rowToChange colToChange match solve bannedAnswer rows cols with - | Some solved when solved > 0ul -> + | solved when solved > 0ul -> isDone <- true answer <- answer + solved | _ -> @@ -184,9 +183,9 @@ module Day13 = rowToChange <- rowToChange + 1 - if index < 0 then + if group.Length >= s.Length then s <- ReadOnlySpan.Empty else - s <- s.Slice (index + 2) + s <- s.Slice (group.Length + 1) answer