Format
This commit is contained in:
@@ -35,25 +35,22 @@ module Day16 =
|
||||
| 3us -> Direction.Down
|
||||
| _ -> failwith "bad"
|
||||
|
||||
let inline getCol (numCols : int) (input : uint16) =
|
||||
(input / 4us) % uint16 numCols
|
||||
|> int
|
||||
let inline getCol (numCols : int) (input : uint16) = (input / 4us) % uint16 numCols |> int
|
||||
|
||||
let inline getRow (numCols : int) (input : uint16) =
|
||||
(input / 4us) / uint16 numCols
|
||||
|> int
|
||||
let inline getRow (numCols : int) (input : uint16) = (input / 4us) / uint16 numCols |> int
|
||||
|
||||
let maxEncoded (numCols : int) (numRows : int) : uint16 =
|
||||
4us * uint16 ((numCols - 1) + numCols * (numRows - 1)) + 3us
|
||||
|
||||
let inline getAt (numCols : int) (s : string) (row : int) (col : int) =
|
||||
s.[row * (numCols + 1) + col]
|
||||
let inline getAt (numCols : int) (s : string) (row : int) (col : int) = s.[row * (numCols + 1) + col]
|
||||
|
||||
let printArr (arr : Arr2D<bool>) =
|
||||
for row = 0 to arr.Height - 1 do
|
||||
for col = 0 to arr.Width - 1 do
|
||||
if Arr2D.get arr col row then printf "#" else printf "."
|
||||
|
||||
printfn ""
|
||||
|
||||
printfn ""
|
||||
|
||||
let advance (arr : Arr2D<_>) (going : ResizeArray<_>) (s : string) (nextUp : uint16) =
|
||||
@@ -63,6 +60,7 @@ module Day16 =
|
||||
let row = getRow numCols nextUp
|
||||
let dir = getDirection nextUp
|
||||
Arr2D.set arr col row true
|
||||
|
||||
match dir with
|
||||
| Direction.Right ->
|
||||
match getAt numCols s row col with
|
||||
@@ -84,12 +82,13 @@ module Day16 =
|
||||
going.RemoveAt (going.Count - 1)
|
||||
| '|' ->
|
||||
going.RemoveAt (going.Count - 1)
|
||||
|
||||
if row < numLines - 1 then
|
||||
going.Add (storeDirectionAndPos numCols col (row + 1) Direction.Down)
|
||||
|
||||
if row > 0 then
|
||||
going.Add (storeDirectionAndPos numCols col (row - 1) Direction.Up)
|
||||
| c ->
|
||||
failwith $"Unrecognised char: %c{c}"
|
||||
| c -> failwith $"Unrecognised char: %c{c}"
|
||||
| Direction.Left ->
|
||||
match getAt numCols s row col with
|
||||
| '-'
|
||||
@@ -110,12 +109,13 @@ module Day16 =
|
||||
going.RemoveAt (going.Count - 1)
|
||||
| '|' ->
|
||||
going.RemoveAt (going.Count - 1)
|
||||
|
||||
if row < numLines - 1 then
|
||||
going.Add (storeDirectionAndPos numCols col (row + 1) Direction.Down)
|
||||
|
||||
if row > 0 then
|
||||
going.Add (storeDirectionAndPos numCols col (row - 1) Direction.Up)
|
||||
| c ->
|
||||
failwith $"Unrecognised char: %c{c}"
|
||||
| c -> failwith $"Unrecognised char: %c{c}"
|
||||
| Direction.Up ->
|
||||
match getAt numCols s row col with
|
||||
| '|'
|
||||
@@ -136,12 +136,13 @@ module Day16 =
|
||||
going.RemoveAt (going.Count - 1)
|
||||
| '-' ->
|
||||
going.RemoveAt (going.Count - 1)
|
||||
|
||||
if col < numCols - 1 then
|
||||
going.Add (storeDirectionAndPos numCols (col + 1) row Direction.Right)
|
||||
|
||||
if col > 0 then
|
||||
going.Add (storeDirectionAndPos numCols (col - 1) row Direction.Left)
|
||||
| c ->
|
||||
failwith $"Unrecognised char: %c{c}"
|
||||
| c -> failwith $"Unrecognised char: %c{c}"
|
||||
| Direction.Down ->
|
||||
match getAt numCols s row col with
|
||||
| '|'
|
||||
@@ -162,64 +163,71 @@ module Day16 =
|
||||
going.RemoveAt (going.Count - 1)
|
||||
| '-' ->
|
||||
going.RemoveAt (going.Count - 1)
|
||||
|
||||
if col < numCols - 1 then
|
||||
going.Add (storeDirectionAndPos numCols (col + 1) row Direction.Right)
|
||||
|
||||
if col > 0 then
|
||||
going.Add (storeDirectionAndPos numCols (col - 1) row Direction.Left)
|
||||
| c ->
|
||||
failwith $"Unrecognised char: %c{c}"
|
||||
| c -> failwith $"Unrecognised char: %c{c}"
|
||||
|
||||
let part1 (s : string) =
|
||||
let numLines = s.AsSpan().Count '\n'
|
||||
let numCols = s.IndexOf '\n'
|
||||
let arr = Array.zeroCreate (numLines * numCols)
|
||||
let buf = Array.zeroCreate (numLines * numCols)
|
||||
#if DEBUG
|
||||
let arr : Arr2D<bool> =
|
||||
{
|
||||
Elements = arr
|
||||
Elements = buf
|
||||
Width = numCols
|
||||
}
|
||||
#else
|
||||
use ptr = fixed arr
|
||||
use ptr = fixed buf
|
||||
|
||||
let arr : Arr2D<bool> =
|
||||
{
|
||||
Elements = arr
|
||||
Elements = ptr
|
||||
Width = numCols
|
||||
Length = arr.Count
|
||||
Length = buf.Length
|
||||
}
|
||||
#endif
|
||||
let going = ResizeArray ()
|
||||
going.Add (storeDirectionAndPos numCols LanguagePrimitives.GenericZero LanguagePrimitives.GenericZero Direction.Right)
|
||||
|
||||
going.Add (
|
||||
storeDirectionAndPos numCols LanguagePrimitives.GenericZero LanguagePrimitives.GenericZero Direction.Right
|
||||
)
|
||||
|
||||
let seen = Array.zeroCreate (int (maxEncoded numCols numLines) + 1)
|
||||
|
||||
while going.Count > 0 do
|
||||
let nextUp = going.[going.Count - 1]
|
||||
|
||||
match seen.[int nextUp] with
|
||||
| true ->
|
||||
going.RemoveAt (going.Count - 1)
|
||||
| true -> going.RemoveAt (going.Count - 1)
|
||||
| false ->
|
||||
seen.[int nextUp] <- true
|
||||
advance arr going s nextUp
|
||||
|
||||
arr.Elements.AsSpan().Count true
|
||||
buf.AsSpan().Count true
|
||||
|
||||
let part2 (s : string) =
|
||||
let numLines = s.AsSpan().Count '\n'
|
||||
let numCols = s.IndexOf '\n'
|
||||
let arr = Array.zeroCreate (numLines * numCols)
|
||||
let buf = Array.zeroCreate (numLines * numCols)
|
||||
#if DEBUG
|
||||
let arr : Arr2D<bool> =
|
||||
{
|
||||
Elements = arr
|
||||
Elements = buf
|
||||
Width = numCols
|
||||
}
|
||||
#else
|
||||
use ptr = fixed arr
|
||||
use ptr = fixed buf
|
||||
|
||||
let arr : Arr2D<bool> =
|
||||
{
|
||||
Elements = arr
|
||||
Elements = ptr
|
||||
Width = numCols
|
||||
Length = arr.Count
|
||||
Length = buf.Length
|
||||
}
|
||||
#endif
|
||||
let going = ResizeArray ()
|
||||
@@ -229,67 +237,71 @@ module Day16 =
|
||||
for start = 0 to numCols - 1 do
|
||||
going.Clear ()
|
||||
Array.Clear seen
|
||||
Array.Clear arr.Elements
|
||||
Array.Clear buf
|
||||
going.Add (storeDirectionAndPos numCols start LanguagePrimitives.GenericZero Direction.Down)
|
||||
|
||||
while going.Count > 0 do
|
||||
let nextUp = going.[going.Count - 1]
|
||||
|
||||
match seen.[int nextUp] with
|
||||
| true ->
|
||||
going.RemoveAt (going.Count - 1)
|
||||
| true -> going.RemoveAt (going.Count - 1)
|
||||
| false ->
|
||||
seen.[int nextUp] <- true
|
||||
advance arr going s nextUp
|
||||
let lit = arr.Elements.AsSpan().Count true
|
||||
|
||||
let lit = buf.AsSpan().Count true
|
||||
best <- max best lit
|
||||
|
||||
going.Clear ()
|
||||
Array.Clear seen
|
||||
Array.Clear arr.Elements
|
||||
Array.Clear buf
|
||||
going.Add (storeDirectionAndPos numCols start (numLines - 1) Direction.Up)
|
||||
|
||||
while going.Count > 0 do
|
||||
let nextUp = going.[going.Count - 1]
|
||||
|
||||
match seen.[int nextUp] with
|
||||
| true ->
|
||||
going.RemoveAt (going.Count - 1)
|
||||
| true -> going.RemoveAt (going.Count - 1)
|
||||
| false ->
|
||||
seen.[int nextUp] <- true
|
||||
advance arr going s nextUp
|
||||
let lit = arr.Elements.AsSpan().Count true
|
||||
|
||||
let lit = buf.AsSpan().Count true
|
||||
best <- max best lit
|
||||
|
||||
for start = 0 to numLines - 1 do
|
||||
going.Clear ()
|
||||
Array.Clear seen
|
||||
Array.Clear arr.Elements
|
||||
Array.Clear buf
|
||||
going.Add (storeDirectionAndPos numCols LanguagePrimitives.GenericZero start Direction.Right)
|
||||
|
||||
while going.Count > 0 do
|
||||
let nextUp = going.[going.Count - 1]
|
||||
|
||||
match seen.[int nextUp] with
|
||||
| true ->
|
||||
going.RemoveAt (going.Count - 1)
|
||||
| true -> going.RemoveAt (going.Count - 1)
|
||||
| false ->
|
||||
seen.[int nextUp] <- true
|
||||
advance arr going s nextUp
|
||||
let lit = arr.Elements.AsSpan().Count true
|
||||
|
||||
let lit = buf.AsSpan().Count true
|
||||
best <- max best lit
|
||||
|
||||
going.Clear ()
|
||||
Array.Clear seen
|
||||
Array.Clear arr.Elements
|
||||
Array.Clear buf
|
||||
going.Add (storeDirectionAndPos numCols (numCols - 1) start Direction.Left)
|
||||
|
||||
while going.Count > 0 do
|
||||
let nextUp = going.[going.Count - 1]
|
||||
|
||||
match seen.[int nextUp] with
|
||||
| true ->
|
||||
going.RemoveAt (going.Count - 1)
|
||||
| true -> going.RemoveAt (going.Count - 1)
|
||||
| false ->
|
||||
seen.[int nextUp] <- true
|
||||
advance arr going s nextUp
|
||||
let lit = arr.Elements.AsSpan().Count true
|
||||
|
||||
let lit = buf.AsSpan().Count true
|
||||
best <- max best lit
|
||||
|
||||
best
|
||||
|
@@ -12,12 +12,10 @@ module TestDay16 =
|
||||
let sample = Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day16.txt"
|
||||
|
||||
[<Test>]
|
||||
let part1Sample () =
|
||||
sample |> Day16.part1 |> shouldEqual 46
|
||||
let part1Sample () = sample |> Day16.part1 |> shouldEqual 46
|
||||
|
||||
[<Test>]
|
||||
let part2Sample () =
|
||||
sample |> Day16.part2 |> shouldEqual 51
|
||||
let part2Sample () = sample |> Day16.part2 |> shouldEqual 51
|
||||
|
||||
[<Test>]
|
||||
let part1Actual () =
|
||||
|
Reference in New Issue
Block a user