Part 2
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/all-checks-complete Pipeline was successful

This commit is contained in:
Smaug123
2023-12-13 12:46:36 +00:00
parent 074124d924
commit 9207ecc582
2 changed files with 98 additions and 29 deletions

View File

@@ -5,6 +5,10 @@ open System
[<RequireQualifiedAccess>]
module Day13 =
let inline isPowerOf2 (i : uint32) =
// https://stackoverflow.com/a/600306/126995
(i &&& (i - 1ul)) = 0ul
let rowToInt (row : ReadOnlySpan<char>) : uint32 =
let mutable mult = 1ul
let mutable answer = 0ul
@@ -48,7 +52,7 @@ module Day13 =
/// Find reflection among rows
[<TailCall>]
let rec findRow (rows : ResizeArray<'a>) (currentLine : int) =
let rec findRow (banAnswer : uint32) (rows : ResizeArray<uint32>) (currentLine : int) =
if currentLine = rows.Count - 1 then
None
else
@@ -61,24 +65,49 @@ module Day13 =
if currentLine % 2 <> i % 2 then
if rows.[i] = rows.[currentLine] then
if verifyHorizontalReflection rows currentLine i then
answer <- uint32 (((currentLine + i) / 2) + 1)
i <- Int32.MaxValue
let desiredAnswer = uint32 (((currentLine + i) / 2) + 1)
if desiredAnswer <> banAnswer then
answer <- uint32 desiredAnswer
i <- Int32.MaxValue
if answer < UInt32.MaxValue then
Some answer
else
findRow rows (currentLine + 1)
findRow banAnswer rows (currentLine + 1)
let render (rowBuf : ResizeArray<_>) (colBuf : ResizeArray<_>) (group : ReadOnlySpan<char>) =
rowBuf.Clear ()
colBuf.Clear ()
let lineLength = group.IndexOf '\n'
for col = 0 to lineLength - 1 do
colBuf.Add (colToInt group lineLength col)
for row in StringSplitEnumerator.make' '\n' group do
if not row.IsEmpty then
rowBuf.Add (rowToInt row)
let solve (banAnswer : uint32) (rowBuf : ResizeArray<_>) (colBuf : ResizeArray<_>) : uint32 option =
match
findRow
(if banAnswer >= 100ul then
banAnswer / 100ul
else
UInt32.MaxValue)
rowBuf
0
with
| Some rowIndex -> Some (100ul * rowIndex)
| None -> findRow banAnswer colBuf 0
let part1 (s : string) =
let mutable s = s.AsSpan ()
use lines = StringSplitEnumerator.make' '\n' s
let rows = ResizeArray ()
let cols = ResizeArray ()
let mutable answer = 0ul
while not s.IsEmpty do
rows.Clear ()
cols.Clear ()
let index = s.IndexOf "\n\n"
let group =
@@ -88,21 +117,8 @@ module Day13 =
else
s.Slice (0, index + 1)
let lineLength = s.IndexOf '\n'
cols.EnsureCapacity lineLength |> ignore
for col = 0 to lineLength - 1 do
cols.Add (colToInt group lineLength col)
for row in StringSplitEnumerator.make' '\n' group do
if not row.IsEmpty then
rows.Add (rowToInt row)
match findRow rows 0 with
| Some rowIndex -> answer <- answer + 100ul * rowIndex
| None ->
let colIndex = Option.get (findRow cols 0)
answer <- answer + colIndex
render rows cols group
answer <- answer + Option.get (solve UInt32.MaxValue rows cols)
if index < 0 then
s <- ReadOnlySpan<char>.Empty
@@ -111,13 +127,66 @@ module Day13 =
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)
if rows.[rowNum] &&& index > 0ul then
rows.[rowNum] - index
else
rows.[rowNum] + index
cols.[colNum] <-
let index = 1ul <<< (rows.Count - rowNum - 1)
if cols.[colNum] &&& index > 0ul then
cols.[colNum] - index
else
cols.[colNum] + index
let part2 (s : string) =
use mutable lines = StringSplitEnumerator.make '\n' s
let mutable s = s.AsSpan ()
let rows = ResizeArray ()
let cols = ResizeArray ()
let mutable answer = 0ul
let mutable answer = 0uL
while not s.IsEmpty do
let index = s.IndexOf "\n\n"
for line in lines do
if not line.IsEmpty then
()
let group =
if index < 0 then
// last group
s
else
s.Slice (0, index + 1)
render rows cols group
let bannedAnswer = solve UInt32.MaxValue rows cols |> Option.get
let mutable isDone = false
let mutable rowToChange = 0
while not isDone && rowToChange < rows.Count do
let mutable colToChange = 0
while not isDone && colToChange < cols.Count do
flipAt rows cols rowToChange colToChange
match solve bannedAnswer rows cols with
| Some solved when solved > 0ul ->
isDone <- true
answer <- answer + solved
| _ ->
flipAt rows cols rowToChange colToChange
colToChange <- colToChange + 1
rowToChange <- rowToChange + 1
if index < 0 then
s <- ReadOnlySpan<char>.Empty
else
s <- s.Slice (index + 2)
answer

View File

@@ -39,7 +39,7 @@ module TestDay13 =
[<Test>]
let part2Sample () =
sample |> Day13.part2 |> shouldEqual 525152uL
sample |> Day13.part2 |> shouldEqual 400ul
[<Test>]
let part1Actual () =
@@ -65,4 +65,4 @@ module TestDay13 =
Assert.Inconclusive ()
failwith "unreachable"
Day13.part2 s |> shouldEqual 3384337640277uL
Day13.part2 s |> shouldEqual 36474ul