Rem unused args
Some checks failed
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/pr/build Pipeline failed
ci/woodpecker/push/all-checks-complete Pipeline was successful
ci/woodpecker/pr/all-checks-complete unknown status

This commit is contained in:
Smaug123
2023-12-10 13:35:41 +00:00
parent f273d02049
commit f28551bdf0

View File

@@ -10,76 +10,64 @@ open System
[<RequireQualifiedAccess>]
module Day10 =
type Pipe =
| Empty = '.'
| NorthSouth = '|'
| EastWest = '-'
| NorthEast = 'L'
| NorthWest = '7'
| SouthEast = 'F'
| SouthWest = '7'
| Animal = 'S'
/// Returns first the line number, then the position within that line.
/// lineLength includes the newline, as does pos.
let inline toRowAndCol (lineLength : int) (lineCount : int) (pos : int) : struct (int * int) =
let inline private toRowAndCol (lineLength : int) (pos : int) : struct (int * int) =
let lineNum = pos / lineLength
let withinLine = pos % lineLength
struct (lineNum, withinLine)
let inline ofRowAndCol (lineLength : int) (lineCount : int) (lineNum : int) (col : int) : int =
lineNum * lineLength + col
let inline private ofRowAndCol (lineLength : int) (lineNum : int) (col : int) : int = lineNum * lineLength + col
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
let inline nextPoint (s : ReadOnlySpan<char>) (lineLength : int) (currPos : int) (prevPos : int) =
let struct (currLineNum, currCol) = toRowAndCol lineLength currPos
let struct (prevLineNum, prevCol) = toRowAndCol lineLength prevPos
match s.[currPos] with
| '|' ->
if prevLineNum < currLineNum then
ofRowAndCol lineLength lineCount (currLineNum + 1) currCol
ofRowAndCol lineLength (currLineNum + 1) currCol
else
ofRowAndCol lineLength lineCount (currLineNum - 1) currCol
ofRowAndCol lineLength (currLineNum - 1) currCol
| '-' ->
if prevCol < currCol then
ofRowAndCol lineLength lineCount currLineNum (currCol + 1)
ofRowAndCol lineLength currLineNum (currCol + 1)
else
ofRowAndCol lineLength lineCount currLineNum (currCol - 1)
ofRowAndCol lineLength currLineNum (currCol - 1)
| 'L' ->
if prevLineNum = currLineNum then
ofRowAndCol lineLength lineCount (currLineNum - 1) currCol
ofRowAndCol lineLength (currLineNum - 1) currCol
else
ofRowAndCol lineLength lineCount currLineNum (currCol + 1)
ofRowAndCol lineLength currLineNum (currCol + 1)
| '7' ->
if prevLineNum = currLineNum then
ofRowAndCol lineLength lineCount (currLineNum + 1) currCol
ofRowAndCol lineLength (currLineNum + 1) currCol
else
ofRowAndCol lineLength lineCount currLineNum (currCol - 1)
ofRowAndCol lineLength currLineNum (currCol - 1)
| 'F' ->
if prevLineNum = currLineNum then
ofRowAndCol lineLength lineCount (currLineNum + 1) currCol
ofRowAndCol lineLength (currLineNum + 1) currCol
else
ofRowAndCol lineLength lineCount currLineNum (currCol + 1)
ofRowAndCol lineLength currLineNum (currCol + 1)
| 'J' ->
if prevLineNum = currLineNum then
ofRowAndCol lineLength lineCount (currLineNum - 1) currCol
ofRowAndCol lineLength (currLineNum - 1) currCol
else
ofRowAndCol lineLength lineCount currLineNum (currCol - 1)
ofRowAndCol lineLength currLineNum (currCol - 1)
| c -> failwithf "unrecognised: %c" c
let part1 (s : string) =
let s = s.AsSpan ()
let lineCount = s.Count '\n'
let lineLength = (s.IndexOf '\n' + 1)
let startPos = s.IndexOf 'S'
let struct (startLine, startCol) = toRowAndCol lineLength lineCount startPos
let struct (startLine, startCol) = toRowAndCol lineLength startPos
let mutable distance = 1
let mutable prevPointA = startPos
let mutable prevPointB = startPos
let mutable pointA =
let pos = ofRowAndCol lineLength lineCount startLine (startCol - 1)
let pos = ofRowAndCol lineLength startLine (startCol - 1)
match s.[pos] with
| '-'
@@ -87,7 +75,7 @@ module Day10 =
| 'F' -> pos
| _ ->
let pos = ofRowAndCol lineLength lineCount startLine (startCol + 1)
let pos = ofRowAndCol lineLength startLine (startCol + 1)
match s.[pos] with
| '-'
@@ -95,10 +83,10 @@ module Day10 =
| '7' -> pos
| _ ->
ofRowAndCol lineLength lineCount (startLine + 1) startCol
ofRowAndCol lineLength (startLine + 1) startCol
let mutable pointB =
let pos = ofRowAndCol lineLength lineCount (startLine - 1) startCol
let pos = ofRowAndCol lineLength (startLine - 1) startCol
match s.[pos] with
| '|'
@@ -106,7 +94,7 @@ module Day10 =
| 'F' -> pos
| _ ->
let pos = ofRowAndCol lineLength lineCount (startLine + 1) startCol
let pos = ofRowAndCol lineLength (startLine + 1) startCol
match s.[pos] with
| '|'
@@ -114,21 +102,21 @@ module Day10 =
| 'J' -> pos
| _ ->
let pos = ofRowAndCol lineLength lineCount startLine (startCol + 1)
let pos = ofRowAndCol lineLength startLine (startCol + 1)
match s.[pos] with
| '-'
| 'J'
| '7' -> pos
| _ -> ofRowAndCol lineLength lineCount startLine (startCol - 1)
| _ -> ofRowAndCol lineLength startLine (startCol - 1)
while pointA <> pointB do
let currentA = pointA
pointA <- nextPoint s lineLength lineCount pointA prevPointA
pointA <- nextPoint s lineLength pointA prevPointA
prevPointA <- currentA
let currentB = pointB
pointB <- nextPoint s lineLength lineCount pointB prevPointB
pointB <- nextPoint s lineLength pointB prevPointB
prevPointB <- currentB
distance <- distance + 1
@@ -232,14 +220,14 @@ module Day10 =
}
#endif
let struct (startLine, startCol) = toRowAndCol lineLength lineCount startPos
let struct (startLine, startCol) = toRowAndCol lineLength startPos
let mutable prevPointA = startPos
let mutable prevPointB = startPos
Arr2D.set system (3 * startCol + 1) (3 * startLine + 1) 1uy
let mutable pointA =
let pos = ofRowAndCol lineLength lineCount startLine (startCol - 1)
let pos = ofRowAndCol lineLength startLine (startCol - 1)
match if pos >= 0 then s.[pos] else 'n' with
| '-'
@@ -249,7 +237,7 @@ module Day10 =
pos
| _ ->
let pos = ofRowAndCol lineLength lineCount startLine (startCol + 1)
let pos = ofRowAndCol lineLength startLine (startCol + 1)
match if pos < s.Length then s.[pos] else 'n' with
| '-'
@@ -261,10 +249,10 @@ module Day10 =
Arr2D.set system (3 * startCol) (3 * startLine) 1uy
Arr2D.set system (3 * startCol) (3 * startLine + 2) 1uy
ofRowAndCol lineLength lineCount (startLine + 1) startCol
ofRowAndCol lineLength (startLine + 1) startCol
let mutable pointB =
let pos = ofRowAndCol lineLength lineCount (startLine - 1) startCol
let pos = ofRowAndCol lineLength (startLine - 1) startCol
match if pos >= 0 then s.[pos] else 'n' with
| '|'
@@ -274,7 +262,7 @@ module Day10 =
pos
| _ ->
let pos = ofRowAndCol lineLength lineCount (startLine + 1) startCol
let pos = ofRowAndCol lineLength (startLine + 1) startCol
match if pos < s.Length then s.[pos] else 'n' with
| '|'
@@ -284,7 +272,7 @@ module Day10 =
pos
| _ ->
let pos = ofRowAndCol lineLength lineCount startLine (startCol + 1)
let pos = ofRowAndCol lineLength startLine (startCol + 1)
match if pos < s.Length then s.[pos] else 'n' with
| '-'
@@ -292,31 +280,31 @@ module Day10 =
| '7' ->
Arr2D.set system (3 * startCol + 2) (3 * startLine + 1) 1uy
pos
| _ -> ofRowAndCol lineLength lineCount startLine (startCol - 1)
| _ -> ofRowAndCol lineLength startLine (startCol - 1)
do
let struct (row, col) = toRowAndCol lineLength lineCount pointA
let struct (row, col) = toRowAndCol lineLength pointA
setAt system (3 * col + 1) (3 * row + 1) s.[pointA] 1uy
do
let struct (row, col) = toRowAndCol lineLength lineCount pointB
let struct (row, col) = toRowAndCol lineLength pointB
setAt system (3 * col + 1) (3 * row + 1) s.[pointB] 1uy
while pointA <> pointB do
let currentA = pointA
pointA <- nextPoint s lineLength lineCount pointA prevPointA
pointA <- nextPoint s lineLength pointA prevPointA
prevPointA <- currentA
do
let struct (row, col) = toRowAndCol lineLength lineCount pointA
let struct (row, col) = toRowAndCol lineLength pointA
setAt system (3 * col + 1) (3 * row + 1) s.[pointA] 1uy
let currentB = pointB
pointB <- nextPoint s lineLength lineCount pointB prevPointB
pointB <- nextPoint s lineLength pointB prevPointB
prevPointB <- currentB
do
let struct (row, col) = toRowAndCol lineLength lineCount pointB
let struct (row, col) = toRowAndCol lineLength pointB
setAt system (3 * col + 1) (3 * row + 1) s.[pointB] 1uy
let stackBuf = ResizeArray ()