Tidy
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/all-checks-complete Pipeline was successful
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/all-checks-complete Pipeline was successful

This commit is contained in:
Smaug123
2023-12-13 12:54:46 +00:00
parent 9207ecc582
commit 4f2d2acd52
4 changed files with 47 additions and 35 deletions

View File

@@ -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]

View File

@@ -44,7 +44,7 @@ module Benchmarks =
[<GlobalSetup>]
member _.Setup () = Run.shouldWrite <- false
[<Params(11, 12)>]
[<Params(11, 12, 13)>]
member val Day = 0 with get, set
[<Params(false, true)>]

View File

@@ -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
|]

View File

@@ -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".
[<TailCall>]
let rec findRow (banAnswer : uint32) (rows : ResizeArray<uint32>) (currentLine : int) =
let rec findRow (banAnswer : uint32) (rows : ResizeArray<uint32>) (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<char>) : ReadOnlySpan<char> =
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<char>.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<char>.Empty
else
s <- s.Slice (index + 2)
s <- s.Slice (group.Length + 1)
answer