Day 16 (#18)
Co-authored-by: Smaug123 <patrick+github@patrickstevens.co.uk> Reviewed-on: #18
This commit is contained in:
@@ -14,6 +14,6 @@ module Inputs =
|
||||
if isNull dir then
|
||||
failwith "reached root of filesystem without finding inputs dir"
|
||||
|
||||
Array.init 15 (fun day -> Path.Combine (dir.FullName, "inputs", $"day%i{day + 1}.txt") |> File.ReadAllText)
|
||||
Array.init 16 (fun day -> Path.Combine (dir.FullName, "inputs", $"day%i{day + 1}.txt") |> File.ReadAllText)
|
||||
|
||||
let inline day (i : int) = days.[i - 1]
|
||||
|
@@ -57,6 +57,23 @@ module Benchmarks =
|
||||
[<GlobalCleanup>]
|
||||
member _.Cleanup () = Run.shouldWrite <- true
|
||||
|
||||
type Benchmark16To20 () =
|
||||
[<GlobalSetup>]
|
||||
member _.Setup () = Run.shouldWrite <- false
|
||||
|
||||
[<Params(16)>]
|
||||
member val Day = 0 with get, set
|
||||
|
||||
[<Params(false, true)>]
|
||||
member val IsPartOne = false with get, set
|
||||
|
||||
[<Benchmark>]
|
||||
member this.Benchmark () : unit =
|
||||
Run.allRuns.[this.Day - 1] (not this.IsPartOne) (Inputs.day this.Day)
|
||||
|
||||
[<GlobalCleanup>]
|
||||
member _.Cleanup () = Run.shouldWrite <- true
|
||||
|
||||
module Program =
|
||||
|
||||
[<EntryPoint>]
|
||||
|
@@ -209,6 +209,18 @@ module Run =
|
||||
if shouldWrite then
|
||||
Console.WriteLine output
|
||||
|
||||
let day16 (partTwo : bool) (input : string) =
|
||||
if not partTwo then
|
||||
let output = Day16.part1 input
|
||||
|
||||
if shouldWrite then
|
||||
Console.WriteLine output
|
||||
else
|
||||
let output = Day16.part2 input
|
||||
|
||||
if shouldWrite then
|
||||
Console.WriteLine output
|
||||
|
||||
let allRuns =
|
||||
[|
|
||||
day1
|
||||
@@ -226,4 +238,5 @@ module Run =
|
||||
day13
|
||||
day14
|
||||
day15
|
||||
day16
|
||||
|]
|
||||
|
@@ -3,6 +3,7 @@
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
@@ -26,6 +27,7 @@
|
||||
<Compile Include="Day13.fs" />
|
||||
<Compile Include="Day14.fs" />
|
||||
<Compile Include="Day15.fs" />
|
||||
<Compile Include="Day16.fs" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
297
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day16.fs
Normal file
297
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day16.fs
Normal file
@@ -0,0 +1,297 @@
|
||||
namespace AdventOfCode2023
|
||||
|
||||
#if DEBUG
|
||||
#else
|
||||
#nowarn "9"
|
||||
#endif
|
||||
|
||||
open System
|
||||
|
||||
[<RequireQualifiedAccess>]
|
||||
module Day16 =
|
||||
|
||||
type Direction =
|
||||
| Left = 0
|
||||
| Right = 1
|
||||
| Up = 2
|
||||
| Down = 3
|
||||
|
||||
let inline dirToInt (d : Direction) =
|
||||
match d with
|
||||
| Direction.Left -> 0us
|
||||
| Direction.Right -> 1us
|
||||
| Direction.Up -> 2us
|
||||
| Direction.Down -> 3us
|
||||
| _ -> failwith "Bad"
|
||||
|
||||
let inline storeDirectionAndPos (numCols : int) (col : int) (row : int) (direction : Direction) : uint16 =
|
||||
4us * uint16 (col + numCols * row) + dirToInt direction
|
||||
|
||||
let inline getDirection (input : uint16) =
|
||||
match input % 4us with
|
||||
| 0us -> Direction.Left
|
||||
| 1us -> Direction.Right
|
||||
| 2us -> Direction.Up
|
||||
| 3us -> Direction.Down
|
||||
| _ -> failwith "bad"
|
||||
|
||||
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 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 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
|
||||
Arr2D.set arr col row true
|
||||
|
||||
match dir with
|
||||
| Direction.Right ->
|
||||
match getAt numCols s row col with
|
||||
| '-'
|
||||
| '.' ->
|
||||
if col < arr.Width - 1 then
|
||||
going.[going.Count - 1] <- storeDirectionAndPos numCols (col + 1) row dir
|
||||
else
|
||||
going.RemoveAt (going.Count - 1)
|
||||
| '/' ->
|
||||
if row > 0 then
|
||||
going.[going.Count - 1] <- storeDirectionAndPos numCols col (row - 1) Direction.Up
|
||||
else
|
||||
going.RemoveAt (going.Count - 1)
|
||||
| '\\' ->
|
||||
if row < numLines - 1 then
|
||||
going.[going.Count - 1] <- storeDirectionAndPos numCols col (row + 1) Direction.Down
|
||||
else
|
||||
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}"
|
||||
| Direction.Left ->
|
||||
match getAt numCols s row col with
|
||||
| '-'
|
||||
| '.' ->
|
||||
if col > 0 then
|
||||
going.[going.Count - 1] <- storeDirectionAndPos numCols (col - 1) row dir
|
||||
else
|
||||
going.RemoveAt (going.Count - 1)
|
||||
| '\\' ->
|
||||
if row > 0 then
|
||||
going.[going.Count - 1] <- storeDirectionAndPos numCols col (row - 1) Direction.Up
|
||||
else
|
||||
going.RemoveAt (going.Count - 1)
|
||||
| '/' ->
|
||||
if row < numLines - 1 then
|
||||
going.[going.Count - 1] <- storeDirectionAndPos numCols col (row + 1) Direction.Down
|
||||
else
|
||||
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}"
|
||||
| Direction.Up ->
|
||||
match getAt numCols s row col with
|
||||
| '|'
|
||||
| '.' ->
|
||||
if row > 0 then
|
||||
going.[going.Count - 1] <- storeDirectionAndPos numCols col (row - 1) dir
|
||||
else
|
||||
going.RemoveAt (going.Count - 1)
|
||||
| '/' ->
|
||||
if col < numCols - 1 then
|
||||
going.[going.Count - 1] <- storeDirectionAndPos numCols (col + 1) row Direction.Right
|
||||
else
|
||||
going.RemoveAt (going.Count - 1)
|
||||
| '\\' ->
|
||||
if col > 0 then
|
||||
going.[going.Count - 1] <- storeDirectionAndPos numCols (col - 1) row Direction.Left
|
||||
else
|
||||
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}"
|
||||
| Direction.Down ->
|
||||
match getAt numCols s row col with
|
||||
| '|'
|
||||
| '.' ->
|
||||
if row < arr.Height - 1 then
|
||||
going.[going.Count - 1] <- storeDirectionAndPos numCols col (row + 1) dir
|
||||
else
|
||||
going.RemoveAt (going.Count - 1)
|
||||
| '\\' ->
|
||||
if col < numCols - 1 then
|
||||
going.[going.Count - 1] <- storeDirectionAndPos numCols (col + 1) row Direction.Right
|
||||
else
|
||||
going.RemoveAt (going.Count - 1)
|
||||
| '/' ->
|
||||
if col > 0 then
|
||||
going.[going.Count - 1] <- storeDirectionAndPos numCols (col - 1) row Direction.Left
|
||||
else
|
||||
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}"
|
||||
| _ -> failwith "bad"
|
||||
|
||||
let part1 (s : string) =
|
||||
let numLines = s.AsSpan().Count '\n'
|
||||
let numCols = s.IndexOf '\n'
|
||||
let buf = Array.zeroCreate (numLines * numCols)
|
||||
#if DEBUG
|
||||
let arr : Arr2D<bool> =
|
||||
{
|
||||
Elements = buf
|
||||
Width = numCols
|
||||
}
|
||||
#else
|
||||
use ptr = fixed buf
|
||||
|
||||
let arr : Arr2D<bool> =
|
||||
{
|
||||
Elements = ptr
|
||||
Width = numCols
|
||||
Length = buf.Length
|
||||
}
|
||||
#endif
|
||||
let going = ResizeArray ()
|
||||
|
||||
going.Add (storeDirectionAndPos numCols 0 0 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
|
||||
|
||||
buf.AsSpan().Count true
|
||||
|
||||
let part2 (s : string) =
|
||||
let numLines = s.AsSpan().Count '\n'
|
||||
let numCols = s.IndexOf '\n'
|
||||
let buf = Array.zeroCreate (numLines * numCols)
|
||||
#if DEBUG
|
||||
let arr : Arr2D<bool> =
|
||||
{
|
||||
Elements = buf
|
||||
Width = numCols
|
||||
}
|
||||
#else
|
||||
use ptr = fixed buf
|
||||
|
||||
let arr : Arr2D<bool> =
|
||||
{
|
||||
Elements = ptr
|
||||
Width = numCols
|
||||
Length = buf.Length
|
||||
}
|
||||
#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 buf
|
||||
going.Add (storeDirectionAndPos numCols start 0 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 = buf.AsSpan().Count true
|
||||
best <- max best lit
|
||||
|
||||
going.Clear ()
|
||||
Array.Clear seen
|
||||
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)
|
||||
| false ->
|
||||
seen.[int nextUp] <- true
|
||||
advance arr going s nextUp
|
||||
|
||||
let lit = buf.AsSpan().Count true
|
||||
best <- max best lit
|
||||
|
||||
for start = 0 to numLines - 1 do
|
||||
going.Clear ()
|
||||
Array.Clear seen
|
||||
Array.Clear buf
|
||||
going.Add (storeDirectionAndPos numCols 0 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 = buf.AsSpan().Count true
|
||||
best <- max best lit
|
||||
|
||||
going.Clear ()
|
||||
Array.Clear seen
|
||||
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)
|
||||
| false ->
|
||||
seen.[int nextUp] <- true
|
||||
advance arr going s nextUp
|
||||
|
||||
let lit = buf.AsSpan().Count true
|
||||
best <- max best lit
|
||||
|
||||
best
|
@@ -300,6 +300,22 @@ module Program =
|
||||
Console.WriteLine (part2.ToString ())
|
||||
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
|
||||
|
||||
Console.WriteLine "=====Day 16====="
|
||||
|
||||
do
|
||||
let input = Path.Combine (dir.FullName, "day16.txt") |> File.ReadAllText
|
||||
|
||||
sw.Restart ()
|
||||
let part1 = Day16.part1 input
|
||||
sw.Stop ()
|
||||
Console.WriteLine (part1.ToString ())
|
||||
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
|
||||
sw.Restart ()
|
||||
let part2 = Day16.part2 input
|
||||
sw.Stop ()
|
||||
Console.WriteLine (part2.ToString ())
|
||||
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
|
||||
|
||||
endToEnd.Stop ()
|
||||
|
||||
Console.Error.WriteLine (
|
||||
|
@@ -24,6 +24,7 @@
|
||||
<Compile Include="TestDay13.fs" />
|
||||
<Compile Include="TestDay14.fs" />
|
||||
<Compile Include="TestDay15.fs" />
|
||||
<Compile Include="TestDay16.fs" />
|
||||
<EmbeddedResource Include="samples\day1.txt"/>
|
||||
<EmbeddedResource Include="samples\day1part1.txt"/>
|
||||
<EmbeddedResource Include="samples\day2.txt"/>
|
||||
@@ -42,6 +43,7 @@
|
||||
<EmbeddedResource Include="samples\day13.txt" />
|
||||
<EmbeddedResource Include="samples\day14.txt" />
|
||||
<EmbeddedResource Include="samples\day15.txt" />
|
||||
<EmbeddedResource Include="samples\day16.txt" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
44
AdventOfCode2023.FSharp/Test/TestDay16.fs
Normal file
44
AdventOfCode2023.FSharp/Test/TestDay16.fs
Normal file
@@ -0,0 +1,44 @@
|
||||
namespace AdventOfCode2023.Test
|
||||
|
||||
open AdventOfCode2023
|
||||
open NUnit.Framework
|
||||
open FsUnitTyped
|
||||
open System.IO
|
||||
|
||||
[<TestFixture>]
|
||||
module TestDay16 =
|
||||
|
||||
[<Test>]
|
||||
let sample = Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day16.txt"
|
||||
|
||||
[<Test>]
|
||||
let part1Sample () = sample |> Day16.part1 |> shouldEqual 46
|
||||
|
||||
[<Test>]
|
||||
let part2Sample () = sample |> Day16.part2 |> shouldEqual 51
|
||||
|
||||
[<Test>]
|
||||
let part1Actual () =
|
||||
let s =
|
||||
try
|
||||
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day16.txt"))
|
||||
with
|
||||
| :? DirectoryNotFoundException
|
||||
| :? FileNotFoundException ->
|
||||
Assert.Inconclusive ()
|
||||
failwith "unreachable"
|
||||
|
||||
Day16.part1 s |> shouldEqual 8112
|
||||
|
||||
[<Test>]
|
||||
let part2Actual () =
|
||||
let s =
|
||||
try
|
||||
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day16.txt"))
|
||||
with
|
||||
| :? DirectoryNotFoundException
|
||||
| :? FileNotFoundException ->
|
||||
Assert.Inconclusive ()
|
||||
failwith "unreachable"
|
||||
|
||||
Day16.part2 s |> shouldEqual 8314
|
10
AdventOfCode2023.FSharp/Test/samples/day16.txt
Normal file
10
AdventOfCode2023.FSharp/Test/samples/day16.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
.|...\....
|
||||
|.-.\.....
|
||||
.....|-...
|
||||
........|.
|
||||
..........
|
||||
.........\
|
||||
..../.\\..
|
||||
.-.-/..|..
|
||||
.|....-|.\
|
||||
..//.|....
|
Reference in New Issue
Block a user