This commit is contained in:
Smaug123
2023-12-16 11:33:29 +00:00
parent 87c422e6d5
commit 012e7c193c
2 changed files with 206 additions and 116 deletions

View File

@@ -6,8 +6,6 @@ namespace AdventOfCode2023
#endif
open System
open System.Collections.Generic
open System.Globalization
[<RequireQualifiedAccess>]
module Day16 =
@@ -58,36 +56,9 @@ module Day16 =
printfn ""
printfn ""
let part1 (s : string) =
let numLines = s.AsSpan().Count '\n'
let numCols = s.IndexOf '\n'
let arr = Array.zeroCreate (numLines * numCols)
#if DEBUG
let arr : Arr2D<bool> =
{
Elements = arr
Width = numCols
}
#else
use ptr = fixed arr
let arr : Arr2D<bool> =
{
Elements = arr
Width = numCols
Length = arr.Count
}
#endif
let going = ResizeArray ()
going.Add (storeDirectionAndPos numCols LanguagePrimitives.GenericZero LanguagePrimitives.GenericZero Direction.Right)
let seen = Array.zeroCreate (int (maxEncoded numCols numLines))
while going.Count > 0 do
let nextUp = going.[going.Count - 1]
match seen.[int nextUp] with
| true ->
going.RemoveAt (going.Count - 1)
| false ->
seen.[int nextUp] <- true
let advance (arr : Arr2D<_>) (going : ResizeArray<_>) (s : string) (nextUp : uint16) =
let numCols = arr.Width
let numLines = arr.Height
let col = getCol numCols nextUp
let row = getRow numCols nextUp
let dir = getDirection nextUp
@@ -198,8 +169,127 @@ module Day16 =
| 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)
#if DEBUG
let arr : Arr2D<bool> =
{
Elements = arr
Width = numCols
}
#else
use ptr = fixed arr
let arr : Arr2D<bool> =
{
Elements = arr
Width = numCols
Length = arr.Count
}
#endif
let going = ResizeArray ()
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)
| false ->
seen.[int nextUp] <- true
advance arr going s nextUp
arr.Elements.AsSpan().Count true
let part2 (s : string) =
use lines = StringSplitEnumerator.make '\n' s
0
let numLines = s.AsSpan().Count '\n'
let numCols = s.IndexOf '\n'
let arr = Array.zeroCreate (numLines * numCols)
#if DEBUG
let arr : Arr2D<bool> =
{
Elements = arr
Width = numCols
}
#else
use ptr = fixed arr
let arr : Arr2D<bool> =
{
Elements = arr
Width = numCols
Length = arr.Count
}
#endif
let going = ResizeArray ()
let seen = Array.zeroCreate (int (maxEncoded numCols numLines) + 1)
let mutable best = 0
for start = 0 to numCols - 1 do
going.Clear ()
Array.Clear seen
Array.Clear arr.Elements
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)
| false ->
seen.[int nextUp] <- true
advance arr going s nextUp
let lit = arr.Elements.AsSpan().Count true
best <- max best lit
going.Clear ()
Array.Clear seen
Array.Clear arr.Elements
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)
| false ->
seen.[int nextUp] <- true
advance arr going s nextUp
let lit = arr.Elements.AsSpan().Count true
best <- max best lit
for start = 0 to numLines - 1 do
going.Clear ()
Array.Clear seen
Array.Clear arr.Elements
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)
| false ->
seen.[int nextUp] <- true
advance arr going s nextUp
let lit = arr.Elements.AsSpan().Count true
best <- max best lit
going.Clear ()
Array.Clear seen
Array.Clear arr.Elements
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)
| false ->
seen.[int nextUp] <- true
advance arr going s nextUp
let lit = arr.Elements.AsSpan().Count true
best <- max best lit
best

View File

@@ -43,4 +43,4 @@ module TestDay16 =
Assert.Inconclusive ()
failwith "unreachable"
Day16.part2 s |> shouldEqual 0
Day16.part2 s |> shouldEqual 8314