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