This commit is contained in:
Smaug123
2023-12-10 13:28:39 +00:00
parent b6de2b4688
commit c63aae951f
2 changed files with 34 additions and 9 deletions

View File

@@ -33,6 +33,7 @@ module Day10 =
let inline nextPoint (s : ReadOnlySpan<char>) (lineLength : int) (lineCount : int) (currPos : int) (prevPos : int) = let inline nextPoint (s : ReadOnlySpan<char>) (lineLength : int) (lineCount : int) (currPos : int) (prevPos : int) =
let struct (currLineNum, currCol) = toRowAndCol lineLength lineCount currPos let struct (currLineNum, currCol) = toRowAndCol lineLength lineCount currPos
let struct (prevLineNum, prevCol) = toRowAndCol lineLength lineCount prevPos let struct (prevLineNum, prevCol) = toRowAndCol lineLength lineCount prevPos
match s.[currPos] with match s.[currPos] with
| '|' -> | '|' ->
if prevLineNum < currLineNum then if prevLineNum < currLineNum then
@@ -67,7 +68,7 @@ module Day10 =
| c -> failwithf "unrecognised: %c" c | c -> failwithf "unrecognised: %c" c
let part1 (s : string) = let part1 (s : string) =
let s = s.AsSpan() let s = s.AsSpan ()
let lineCount = s.Count '\n' let lineCount = s.Count '\n'
let lineLength = (s.IndexOf '\n' + 1) let lineLength = (s.IndexOf '\n' + 1)
let startPos = s.IndexOf 'S' let startPos = s.IndexOf 'S'
@@ -79,6 +80,7 @@ module Day10 =
let mutable pointA = let mutable pointA =
let pos = ofRowAndCol lineLength lineCount startLine (startCol - 1) let pos = ofRowAndCol lineLength lineCount startLine (startCol - 1)
match s.[pos] with match s.[pos] with
| '-' | '-'
| 'L' | 'L'
@@ -86,6 +88,7 @@ module Day10 =
| _ -> | _ ->
let pos = ofRowAndCol lineLength lineCount startLine (startCol + 1) let pos = ofRowAndCol lineLength lineCount startLine (startCol + 1)
match s.[pos] with match s.[pos] with
| '-' | '-'
| 'J' | 'J'
@@ -96,6 +99,7 @@ module Day10 =
let mutable pointB = let mutable pointB =
let pos = ofRowAndCol lineLength lineCount (startLine - 1) startCol let pos = ofRowAndCol lineLength lineCount (startLine - 1) startCol
match s.[pos] with match s.[pos] with
| '|' | '|'
| '7' | '7'
@@ -103,6 +107,7 @@ module Day10 =
| _ -> | _ ->
let pos = ofRowAndCol lineLength lineCount (startLine + 1) startCol let pos = ofRowAndCol lineLength lineCount (startLine + 1) startCol
match s.[pos] with match s.[pos] with
| '|' | '|'
| 'L' | 'L'
@@ -110,6 +115,7 @@ module Day10 =
| _ -> | _ ->
let pos = ofRowAndCol lineLength lineCount startLine (startCol + 1) let pos = ofRowAndCol lineLength lineCount startLine (startCol + 1)
match s.[pos] with match s.[pos] with
| '-' | '-'
| 'J' | 'J'
@@ -132,6 +138,7 @@ module Day10 =
let floodFill (stackBuf : ResizeArray<_>) (s : Arr2D<byte>) (currX : int) (currY : int) = let floodFill (stackBuf : ResizeArray<_>) (s : Arr2D<byte>) (currX : int) (currY : int) =
stackBuf.Clear () stackBuf.Clear ()
stackBuf.Add (currX, currY) stackBuf.Add (currX, currY)
while stackBuf.Count > 0 do while stackBuf.Count > 0 do
let currX, currY = stackBuf.[stackBuf.Count - 1] let currX, currY = stackBuf.[stackBuf.Count - 1]
stackBuf.RemoveAt (stackBuf.Count - 1) stackBuf.RemoveAt (stackBuf.Count - 1)
@@ -140,14 +147,17 @@ module Day10 =
if Arr2D.get s (currX - 1) currY = 0uy then if Arr2D.get s (currX - 1) currY = 0uy then
Arr2D.set s (currX - 1) currY 2uy Arr2D.set s (currX - 1) currY 2uy
stackBuf.Add ((currX - 1, currY)) stackBuf.Add ((currX - 1, currY))
if currX < s.Width - 1 then if currX < s.Width - 1 then
if Arr2D.get s (currX + 1) currY = 0uy then if Arr2D.get s (currX + 1) currY = 0uy then
Arr2D.set s (currX + 1) currY 2uy Arr2D.set s (currX + 1) currY 2uy
stackBuf.Add ((currX + 1, currY)) stackBuf.Add ((currX + 1, currY))
if currY > 0 then if currY > 0 then
if Arr2D.get s currX (currY - 1) = 0uy then if Arr2D.get s currX (currY - 1) = 0uy then
Arr2D.set s currX (currY - 1) 2uy Arr2D.set s currX (currY - 1) 2uy
stackBuf.Add ((currX, currY - 1)) stackBuf.Add ((currX, currY - 1))
if currY < s.Height - 1 then if currY < s.Height - 1 then
if Arr2D.get s currX (currY + 1) = 0uy then if Arr2D.get s currX (currY + 1) = 0uy then
Arr2D.set s currX (currY + 1) 2uy Arr2D.set s currX (currY + 1) 2uy
@@ -161,6 +171,7 @@ module Day10 =
| 1uy -> printf "#" | 1uy -> printf "#"
| 2uy -> printf "." | 2uy -> printf "."
| s -> failwithf "unrecognised: %i" s | s -> failwithf "unrecognised: %i" s
printfn "" printfn ""
printfn "" printfn ""
@@ -168,6 +179,7 @@ module Day10 =
let inline setAt (arr : Arr2D<byte>) (x : int) (y : int) (matching : char) (target : byte) = let inline setAt (arr : Arr2D<byte>) (x : int) (y : int) (matching : char) (target : byte) =
Arr2D.set arr x y target Arr2D.set arr x y target
match matching with match matching with
| '-' -> | '-' ->
Arr2D.set arr (x - 1) y target Arr2D.set arr (x - 1) y target
@@ -190,7 +202,7 @@ module Day10 =
| c -> failwithf "bad char: %c" c | c -> failwithf "bad char: %c" c
let part2 (s : string) = let part2 (s : string) =
let s = s.AsSpan() let s = s.AsSpan ()
let lineCount = s.Count '\n' let lineCount = s.Count '\n'
let lineLength = (s.IndexOf '\n' + 1) let lineLength = (s.IndexOf '\n' + 1)
let startPos = s.IndexOf 'S' let startPos = s.IndexOf 'S'
@@ -204,10 +216,12 @@ module Day10 =
} }
#else #else
use ptr = fixed buffer use ptr = fixed buffer
let system : Arr2D<byte> = let system : Arr2D<byte> =
{ Elements = ptr {
Length = buffer.Length Elements = ptr
Width = 3 * lineLength Length = buffer.Length
Width = 3 * lineLength
} }
#endif #endif
@@ -219,6 +233,7 @@ module Day10 =
let mutable pointA = let mutable pointA =
let pos = ofRowAndCol lineLength lineCount startLine (startCol - 1) let pos = ofRowAndCol lineLength lineCount startLine (startCol - 1)
match if pos >= 0 then s.[pos] else 'n' with match if pos >= 0 then s.[pos] else 'n' with
| '-' | '-'
| 'L' | 'L'
@@ -228,6 +243,7 @@ module Day10 =
| _ -> | _ ->
let pos = ofRowAndCol lineLength lineCount startLine (startCol + 1) let pos = ofRowAndCol lineLength lineCount startLine (startCol + 1)
match if pos < s.Length then s.[pos] else 'n' with match if pos < s.Length then s.[pos] else 'n' with
| '-' | '-'
| 'J' | 'J'
@@ -242,6 +258,7 @@ module Day10 =
let mutable pointB = let mutable pointB =
let pos = ofRowAndCol lineLength lineCount (startLine - 1) startCol let pos = ofRowAndCol lineLength lineCount (startLine - 1) startCol
match if pos >= 0 then s.[pos] else 'n' with match if pos >= 0 then s.[pos] else 'n' with
| '|' | '|'
| '7' | '7'
@@ -251,6 +268,7 @@ module Day10 =
| _ -> | _ ->
let pos = ofRowAndCol lineLength lineCount (startLine + 1) startCol let pos = ofRowAndCol lineLength lineCount (startLine + 1) startCol
match if pos < s.Length then s.[pos] else 'n' with match if pos < s.Length then s.[pos] else 'n' with
| '|' | '|'
| 'L' | 'L'
@@ -260,18 +278,19 @@ module Day10 =
| _ -> | _ ->
let pos = ofRowAndCol lineLength lineCount startLine (startCol + 1) let pos = ofRowAndCol lineLength lineCount startLine (startCol + 1)
match if pos < s.Length then s.[pos] else 'n' with match if pos < s.Length then s.[pos] else 'n' with
| '-' | '-'
| 'J' | 'J'
| '7' -> | '7' ->
Arr2D.set system (3 * startCol + 2) (3 * startLine + 1) 1uy Arr2D.set system (3 * startCol + 2) (3 * startLine + 1) 1uy
pos pos
| _ -> | _ -> ofRowAndCol lineLength lineCount startLine (startCol - 1)
ofRowAndCol lineLength lineCount startLine (startCol - 1)
do do
let struct (row, col) = toRowAndCol lineLength lineCount pointA let struct (row, col) = toRowAndCol lineLength lineCount pointA
setAt system (3 * col + 1) (3 * row + 1) s.[pointA] 1uy setAt system (3 * col + 1) (3 * row + 1) s.[pointA] 1uy
do do
let struct (row, col) = toRowAndCol lineLength lineCount pointB let struct (row, col) = toRowAndCol lineLength lineCount pointB
setAt system (3 * col + 1) (3 * row + 1) s.[pointB] 1uy setAt system (3 * col + 1) (3 * row + 1) s.[pointB] 1uy
@@ -280,6 +299,7 @@ module Day10 =
let currentA = pointA let currentA = pointA
pointA <- nextPoint s lineLength lineCount pointA prevPointA pointA <- nextPoint s lineLength lineCount pointA prevPointA
prevPointA <- currentA prevPointA <- currentA
do do
let struct (row, col) = toRowAndCol lineLength lineCount pointA let struct (row, col) = toRowAndCol lineLength lineCount pointA
setAt system (3 * col + 1) (3 * row + 1) s.[pointA] 1uy setAt system (3 * col + 1) (3 * row + 1) s.[pointA] 1uy
@@ -287,14 +307,17 @@ module Day10 =
let currentB = pointB let currentB = pointB
pointB <- nextPoint s lineLength lineCount pointB prevPointB pointB <- nextPoint s lineLength lineCount pointB prevPointB
prevPointB <- currentB prevPointB <- currentB
do do
let struct (row, col) = toRowAndCol lineLength lineCount pointB let struct (row, col) = toRowAndCol lineLength lineCount pointB
setAt system (3 * col + 1) (3 * row + 1) s.[pointB] 1uy setAt system (3 * col + 1) (3 * row + 1) s.[pointB] 1uy
let stackBuf = ResizeArray () let stackBuf = ResizeArray ()
for line = 0 to system.Height - 1 do for line = 0 to system.Height - 1 do
floodFill stackBuf system 0 line floodFill stackBuf system 0 line
floodFill stackBuf system (system.Width - 1) line floodFill stackBuf system (system.Width - 1) line
for col = 0 to system.Width - 1 do for col = 0 to system.Width - 1 do
floodFill stackBuf system col 0 floodFill stackBuf system col 0
floodFill stackBuf system col (system.Height - 1) floodFill stackBuf system col (system.Height - 1)

View File

@@ -15,13 +15,15 @@ module TestDay10 =
.L-J. .L-J.
..... .....
""" """
|> Day10.part1 |> shouldEqual 4 |> Day10.part1
|> shouldEqual 4
[<Test>] [<Test>]
let part1Sample () = let part1Sample () =
Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day10part1.txt" Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day10part1.txt"
|> Day10.part1 |> shouldEqual 8 |> Day10.part1
|> shouldEqual 8
[<Test>] [<Test>]
let part2Sample1 () = let part2Sample1 () =