Compare commits
8 Commits
woodpekcer
...
4088720fe8
Author | SHA1 | Date | |
---|---|---|---|
|
4088720fe8 | ||
|
a3bb7471cc | ||
dc0aa1ce30 | |||
362a73cc41 | |||
4e97a4d454 | |||
a24d28ab5c | |||
8af8916d46 | |||
3b98d704d1 |
@@ -0,0 +1,22 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Include="Run.fs" />
|
||||||
|
<Compile Include="Inputs.fs" />
|
||||||
|
<Compile Include="Program.fs"/>
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="BenchmarkDotNet" Version="0.13.11" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\AdventOfCode2023.FSharp.Lib\AdventOfCode2023.FSharp.Lib.fsproj" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
@@ -0,0 +1,19 @@
|
|||||||
|
namespace AdventOfCode2023
|
||||||
|
|
||||||
|
open System.IO
|
||||||
|
open System.Reflection
|
||||||
|
|
||||||
|
[<RequireQualifiedAccess>]
|
||||||
|
module Inputs =
|
||||||
|
let days =
|
||||||
|
let mutable dir = Assembly.GetEntryAssembly().Location |> FileInfo |> _.Directory
|
||||||
|
|
||||||
|
while not (dir.EnumerateDirectories () |> Seq.exists (fun i -> i.Name = "inputs")) do
|
||||||
|
dir <- dir.Parent
|
||||||
|
|
||||||
|
if isNull dir then
|
||||||
|
failwith "reached root of filesystem without finding inputs dir"
|
||||||
|
|
||||||
|
Array.init 14 (fun day -> Path.Combine (dir.FullName, "inputs", $"day%i{day + 1}.txt") |> File.ReadAllText)
|
||||||
|
|
||||||
|
let inline day (i : int) = days.[i - 1]
|
@@ -0,0 +1,70 @@
|
|||||||
|
namespace AdventOfCode2023
|
||||||
|
|
||||||
|
open BenchmarkDotNet.Attributes
|
||||||
|
open BenchmarkDotNet.Configs
|
||||||
|
open BenchmarkDotNet.Running
|
||||||
|
|
||||||
|
module Benchmarks =
|
||||||
|
type Benchmark1To5 () =
|
||||||
|
[<GlobalSetup>]
|
||||||
|
member _.Setup () = Run.shouldWrite <- false
|
||||||
|
|
||||||
|
[<Params(1, 2, 3, 4, 5)>]
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
|
type Benchmark6To10 () =
|
||||||
|
[<GlobalSetup>]
|
||||||
|
member _.Setup () = Run.shouldWrite <- false
|
||||||
|
|
||||||
|
[<Params(6, 7, 8, 9, 10)>]
|
||||||
|
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
|
||||||
|
|
||||||
|
type Benchmark11To15 () =
|
||||||
|
[<GlobalSetup>]
|
||||||
|
member _.Setup () = Run.shouldWrite <- false
|
||||||
|
|
||||||
|
[<Params(11, 12, 13, 14)>]
|
||||||
|
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>]
|
||||||
|
let main args =
|
||||||
|
let config =
|
||||||
|
ManualConfig.Create(DefaultConfig.Instance).WithOptions ConfigOptions.DisableOptimizationsValidator
|
||||||
|
|
||||||
|
let _summary = BenchmarkRunner.Run<Benchmarks.Benchmark1To5> config
|
||||||
|
let _summary = BenchmarkRunner.Run<Benchmarks.Benchmark6To10> config
|
||||||
|
let _summary = BenchmarkRunner.Run<Benchmarks.Benchmark11To15> config
|
||||||
|
0
|
216
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Bench/Run.fs
Normal file
216
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Bench/Run.fs
Normal file
@@ -0,0 +1,216 @@
|
|||||||
|
namespace AdventOfCode2023
|
||||||
|
|
||||||
|
#if DEBUG
|
||||||
|
#else
|
||||||
|
#nowarn "9"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
open System
|
||||||
|
|
||||||
|
[<RequireQualifiedAccess>]
|
||||||
|
module Run =
|
||||||
|
let mutable shouldWrite = true
|
||||||
|
|
||||||
|
let day1 (partTwo : bool) (input : string) =
|
||||||
|
if not partTwo then
|
||||||
|
let output = Day1.part1 input
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
else
|
||||||
|
let output = Day1.part2 input
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
|
||||||
|
let day2 (partTwo : bool) (input : string) =
|
||||||
|
if not partTwo then
|
||||||
|
let output = Day2.part1 input
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
else
|
||||||
|
let output = Day2.part2 input
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
|
||||||
|
let day3 (partTwo : bool) (input : string) =
|
||||||
|
let resultArr, len, lineCount = Day3.parse (input.ToCharArray () |> Array.map byte)
|
||||||
|
#if DEBUG
|
||||||
|
let contents =
|
||||||
|
{
|
||||||
|
Elements = Array.take len resultArr
|
||||||
|
Width = len / lineCount
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
use ptr = fixed resultArr
|
||||||
|
|
||||||
|
let contents =
|
||||||
|
{
|
||||||
|
Elements = ptr
|
||||||
|
Length = len
|
||||||
|
Width = len / lineCount
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
if not partTwo then
|
||||||
|
let output = Day3.part1 contents
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
else
|
||||||
|
let output = Day3.part2 contents
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
|
||||||
|
let day4 (partTwo : bool) (input : string) =
|
||||||
|
if not partTwo then
|
||||||
|
let output = Day4.part1 input
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
else
|
||||||
|
let output = Day4.part2 input
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
|
||||||
|
let day5 (partTwo : bool) (input : string) =
|
||||||
|
if not partTwo then
|
||||||
|
let output = Day5.part1 input
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
else
|
||||||
|
let output = Day5.part2 input
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
|
||||||
|
let day6 (partTwo : bool) (input : string) =
|
||||||
|
if not partTwo then
|
||||||
|
let output = Day6.part1 input
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
else
|
||||||
|
let output = Day6.part2 input
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
|
||||||
|
let day7 (partTwo : bool) (input : string) =
|
||||||
|
if not partTwo then
|
||||||
|
let output = Day7.part1 input
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
else
|
||||||
|
let output = Day7.part2 input
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
|
||||||
|
let day8 (partTwo : bool) (input : string) =
|
||||||
|
if not partTwo then
|
||||||
|
let output = Day8.part1 input
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
else
|
||||||
|
let output = Day8.part2 input
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
|
||||||
|
let day9 (partTwo : bool) (input : string) =
|
||||||
|
if not partTwo then
|
||||||
|
let output = Day9.part1 input
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
else
|
||||||
|
let output = Day9.part2 input
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
|
||||||
|
let day10 (partTwo : bool) (input : string) =
|
||||||
|
if not partTwo then
|
||||||
|
let output = Day10.part1 input
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
else
|
||||||
|
let output = Day10.part2 input
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
|
||||||
|
let day11 (partTwo : bool) (input : string) =
|
||||||
|
if not partTwo then
|
||||||
|
let output = Day11.part1 input
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
else
|
||||||
|
let output = Day11.part2 input
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
|
||||||
|
|
||||||
|
let day12 (partTwo : bool) (input : string) =
|
||||||
|
if not partTwo then
|
||||||
|
let output = Day12.part1 input
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
else
|
||||||
|
let output = Day12.part2 input
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
|
||||||
|
let day13 (partTwo : bool) (input : string) =
|
||||||
|
if not partTwo then
|
||||||
|
let output = Day13.part1 input
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
else
|
||||||
|
let output = Day13.part2 input
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
|
||||||
|
let day14 (partTwo : bool) (input : string) =
|
||||||
|
if not partTwo then
|
||||||
|
let output = Day14.part1 input
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
else
|
||||||
|
let output = Day14.part2 input
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
|
||||||
|
let allRuns =
|
||||||
|
[|
|
||||||
|
day1
|
||||||
|
day2
|
||||||
|
day3
|
||||||
|
day4
|
||||||
|
day5
|
||||||
|
day6
|
||||||
|
day7
|
||||||
|
day8
|
||||||
|
day9
|
||||||
|
day10
|
||||||
|
day11
|
||||||
|
day12
|
||||||
|
day13
|
||||||
|
day14
|
||||||
|
|]
|
@@ -1,23 +1,30 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
<GenerateDocumentationFile>true</GenerateDocumentationFile>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Arr2D.fs" />
|
<Compile Include="Arr2D.fs"/>
|
||||||
<Compile Include="ResizeArray.fs" />
|
<Compile Include="ResizeArray.fs"/>
|
||||||
<Compile Include="EfficientString.fs" />
|
<Compile Include="EfficientString.fs"/>
|
||||||
<Compile Include="Arithmetic.fs" />
|
<Compile Include="Arithmetic.fs"/>
|
||||||
<Compile Include="Day1.fs" />
|
<Compile Include="Rational.fs"/>
|
||||||
<Compile Include="Day2.fs" />
|
<Compile Include="Day1.fs"/>
|
||||||
<Compile Include="Day3.fs" />
|
<Compile Include="Day2.fs"/>
|
||||||
<Compile Include="Day4.fs" />
|
<Compile Include="Day3.fs"/>
|
||||||
<Compile Include="Day5.fs" />
|
<Compile Include="Day4.fs"/>
|
||||||
<Compile Include="Day6.fs" />
|
<Compile Include="Day5.fs"/>
|
||||||
<Compile Include="Day7.fs" />
|
<Compile Include="Day6.fs"/>
|
||||||
<Compile Include="Day8.fs" />
|
<Compile Include="Day7.fs"/>
|
||||||
</ItemGroup>
|
<Compile Include="Day8.fs"/>
|
||||||
|
<Compile Include="Day9.fs"/>
|
||||||
|
<Compile Include="Day10.fs" />
|
||||||
|
<Compile Include="Day11.fs" />
|
||||||
|
<Compile Include="Day12.fs" />
|
||||||
|
<Compile Include="Day13.fs" />
|
||||||
|
<Compile Include="Day14.fs" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
327
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day10.fs
Normal file
327
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day10.fs
Normal file
@@ -0,0 +1,327 @@
|
|||||||
|
namespace AdventOfCode2023
|
||||||
|
|
||||||
|
#if DEBUG
|
||||||
|
#else
|
||||||
|
#nowarn "9"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
open System
|
||||||
|
|
||||||
|
[<RequireQualifiedAccess>]
|
||||||
|
module Day10 =
|
||||||
|
|
||||||
|
/// Returns first the line number, then the position within that line.
|
||||||
|
/// lineLength includes the newline, as does pos.
|
||||||
|
let inline private toRowAndCol (lineLength : int) (pos : int) : struct (int * int) =
|
||||||
|
let lineNum = pos / lineLength
|
||||||
|
let withinLine = pos % lineLength
|
||||||
|
struct (lineNum, withinLine)
|
||||||
|
|
||||||
|
let inline private ofRowAndCol (lineLength : int) (lineNum : int) (col : int) : int = lineNum * lineLength + col
|
||||||
|
|
||||||
|
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 (currLineNum + 1) currCol
|
||||||
|
else
|
||||||
|
ofRowAndCol lineLength (currLineNum - 1) currCol
|
||||||
|
| '-' ->
|
||||||
|
if prevCol < currCol then
|
||||||
|
ofRowAndCol lineLength currLineNum (currCol + 1)
|
||||||
|
else
|
||||||
|
ofRowAndCol lineLength currLineNum (currCol - 1)
|
||||||
|
| 'L' ->
|
||||||
|
if prevLineNum = currLineNum then
|
||||||
|
ofRowAndCol lineLength (currLineNum - 1) currCol
|
||||||
|
else
|
||||||
|
ofRowAndCol lineLength currLineNum (currCol + 1)
|
||||||
|
| '7' ->
|
||||||
|
if prevLineNum = currLineNum then
|
||||||
|
ofRowAndCol lineLength (currLineNum + 1) currCol
|
||||||
|
else
|
||||||
|
ofRowAndCol lineLength currLineNum (currCol - 1)
|
||||||
|
| 'F' ->
|
||||||
|
if prevLineNum = currLineNum then
|
||||||
|
ofRowAndCol lineLength (currLineNum + 1) currCol
|
||||||
|
else
|
||||||
|
ofRowAndCol lineLength currLineNum (currCol + 1)
|
||||||
|
| 'J' ->
|
||||||
|
if prevLineNum = currLineNum then
|
||||||
|
ofRowAndCol lineLength (currLineNum - 1) currCol
|
||||||
|
else
|
||||||
|
ofRowAndCol lineLength currLineNum (currCol - 1)
|
||||||
|
| c -> failwithf "unrecognised: %c" c
|
||||||
|
|
||||||
|
let part1 (s : string) =
|
||||||
|
let s = s.AsSpan ()
|
||||||
|
let lineLength = (s.IndexOf '\n' + 1)
|
||||||
|
let startPos = s.IndexOf 'S'
|
||||||
|
|
||||||
|
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 startLine (startCol - 1)
|
||||||
|
|
||||||
|
match s.[pos] with
|
||||||
|
| '-'
|
||||||
|
| 'L'
|
||||||
|
| 'F' -> pos
|
||||||
|
| _ ->
|
||||||
|
|
||||||
|
let pos = ofRowAndCol lineLength startLine (startCol + 1)
|
||||||
|
|
||||||
|
match s.[pos] with
|
||||||
|
| '-'
|
||||||
|
| 'J'
|
||||||
|
| '7' -> pos
|
||||||
|
| _ ->
|
||||||
|
|
||||||
|
ofRowAndCol lineLength (startLine + 1) startCol
|
||||||
|
|
||||||
|
let mutable pointB =
|
||||||
|
let pos = ofRowAndCol lineLength (startLine - 1) startCol
|
||||||
|
|
||||||
|
match if pos >= 0 then s.[pos] else 'n' with
|
||||||
|
| '|'
|
||||||
|
| '7'
|
||||||
|
| 'F' -> pos
|
||||||
|
| _ ->
|
||||||
|
|
||||||
|
let pos = ofRowAndCol lineLength (startLine + 1) startCol
|
||||||
|
|
||||||
|
match if pos < s.Length then s.[pos] else 'n' with
|
||||||
|
| '|'
|
||||||
|
| 'L'
|
||||||
|
| 'J' -> pos
|
||||||
|
| _ ->
|
||||||
|
|
||||||
|
let pos = ofRowAndCol lineLength startLine (startCol + 1)
|
||||||
|
|
||||||
|
match if pos < s.Length then s.[pos] else 'n' with
|
||||||
|
| '-'
|
||||||
|
| 'J'
|
||||||
|
| '7' -> pos
|
||||||
|
| _ -> ofRowAndCol lineLength startLine (startCol - 1)
|
||||||
|
|
||||||
|
while pointA <> pointB do
|
||||||
|
let currentA = pointA
|
||||||
|
pointA <- nextPoint s lineLength pointA prevPointA
|
||||||
|
prevPointA <- currentA
|
||||||
|
|
||||||
|
let currentB = pointB
|
||||||
|
pointB <- nextPoint s lineLength pointB prevPointB
|
||||||
|
prevPointB <- currentB
|
||||||
|
|
||||||
|
distance <- distance + 1
|
||||||
|
|
||||||
|
distance
|
||||||
|
|
||||||
|
let floodFill (stackBuf : ResizeArray<_>) (s : Arr2D<byte>) (currX : int) (currY : int) =
|
||||||
|
stackBuf.Clear ()
|
||||||
|
stackBuf.Add currX
|
||||||
|
stackBuf.Add currY
|
||||||
|
|
||||||
|
while stackBuf.Count > 0 do
|
||||||
|
let currY = stackBuf.[stackBuf.Count - 1]
|
||||||
|
stackBuf.RemoveAt (stackBuf.Count - 1)
|
||||||
|
let currX = stackBuf.[stackBuf.Count - 1]
|
||||||
|
stackBuf.RemoveAt (stackBuf.Count - 1)
|
||||||
|
|
||||||
|
if currX > 0 then
|
||||||
|
if Arr2D.get s (currX - 1) currY = 0uy then
|
||||||
|
Arr2D.set s (currX - 1) currY 2uy
|
||||||
|
stackBuf.Add (currX - 1)
|
||||||
|
stackBuf.Add 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)
|
||||||
|
stackBuf.Add currY
|
||||||
|
|
||||||
|
if currY > 0 then
|
||||||
|
if Arr2D.get s currX (currY - 1) = 0uy then
|
||||||
|
Arr2D.set s currX (currY - 1) 2uy
|
||||||
|
stackBuf.Add currX
|
||||||
|
stackBuf.Add (currY - 1)
|
||||||
|
|
||||||
|
if currY < s.Height - 1 then
|
||||||
|
if Arr2D.get s currX (currY + 1) = 0uy then
|
||||||
|
Arr2D.set s currX (currY + 1) 2uy
|
||||||
|
stackBuf.Add currX
|
||||||
|
stackBuf.Add (currY + 1)
|
||||||
|
|
||||||
|
let print (s : Arr2D<byte>) =
|
||||||
|
for y = 0 to s.Height - 1 do
|
||||||
|
for x = 0 to s.Width - 1 do
|
||||||
|
match Arr2D.get s x y with
|
||||||
|
| 0uy -> printf " "
|
||||||
|
| 1uy -> printf "#"
|
||||||
|
| 2uy -> printf "."
|
||||||
|
| s -> failwithf "unrecognised: %i" s
|
||||||
|
|
||||||
|
printfn ""
|
||||||
|
|
||||||
|
printfn ""
|
||||||
|
printfn ""
|
||||||
|
|
||||||
|
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
|
||||||
|
Arr2D.set arr (x + 1) y target
|
||||||
|
| '|' ->
|
||||||
|
Arr2D.set arr x (y - 1) target
|
||||||
|
Arr2D.set arr x (y + 1) target
|
||||||
|
| 'L' ->
|
||||||
|
Arr2D.set arr x (y - 1) target
|
||||||
|
Arr2D.set arr (x + 1) y target
|
||||||
|
| 'J' ->
|
||||||
|
Arr2D.set arr x (y - 1) target
|
||||||
|
Arr2D.set arr (x - 1) y target
|
||||||
|
| '7' ->
|
||||||
|
Arr2D.set arr x (y + 1) target
|
||||||
|
Arr2D.set arr (x - 1) y target
|
||||||
|
| 'F' ->
|
||||||
|
Arr2D.set arr x (y + 1) target
|
||||||
|
Arr2D.set arr (x + 1) y target
|
||||||
|
| c -> failwithf "bad char: %c" c
|
||||||
|
|
||||||
|
let part2 (s : string) =
|
||||||
|
let s = s.AsSpan ()
|
||||||
|
let lineCount = s.Count '\n'
|
||||||
|
let lineLength = (s.IndexOf '\n' + 1)
|
||||||
|
let startPos = s.IndexOf 'S'
|
||||||
|
|
||||||
|
let buffer = Array.zeroCreate (lineCount * lineLength * 9)
|
||||||
|
#if DEBUG
|
||||||
|
let system : Arr2D<byte> =
|
||||||
|
{
|
||||||
|
Elements = buffer
|
||||||
|
Width = 3 * lineLength
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
use ptr = fixed buffer
|
||||||
|
|
||||||
|
let system : Arr2D<byte> =
|
||||||
|
{
|
||||||
|
Elements = ptr
|
||||||
|
Length = buffer.Length
|
||||||
|
Width = 3 * lineLength
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
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 startLine (startCol - 1)
|
||||||
|
|
||||||
|
match if pos >= 0 then s.[pos] else 'n' with
|
||||||
|
| '-'
|
||||||
|
| 'L'
|
||||||
|
| 'F' ->
|
||||||
|
Arr2D.set system (3 * startCol) (3 * startLine + 1) 1uy
|
||||||
|
pos
|
||||||
|
| _ ->
|
||||||
|
|
||||||
|
let pos = ofRowAndCol lineLength 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
|
||||||
|
| _ ->
|
||||||
|
|
||||||
|
Arr2D.set system (3 * startCol) (3 * startLine) 1uy
|
||||||
|
Arr2D.set system (3 * startCol) (3 * startLine + 2) 1uy
|
||||||
|
ofRowAndCol lineLength (startLine + 1) startCol
|
||||||
|
|
||||||
|
let mutable pointB =
|
||||||
|
let pos = ofRowAndCol lineLength (startLine - 1) startCol
|
||||||
|
|
||||||
|
match if pos >= 0 then s.[pos] else 'n' with
|
||||||
|
| '|'
|
||||||
|
| '7'
|
||||||
|
| 'F' ->
|
||||||
|
Arr2D.set system (3 * startCol + 1) (3 * startLine) 1uy
|
||||||
|
pos
|
||||||
|
| _ ->
|
||||||
|
|
||||||
|
let pos = ofRowAndCol lineLength (startLine + 1) startCol
|
||||||
|
|
||||||
|
match if pos < s.Length then s.[pos] else 'n' with
|
||||||
|
| '|'
|
||||||
|
| 'L'
|
||||||
|
| 'J' ->
|
||||||
|
Arr2D.set system (3 * startCol + 1) (3 * startLine + 2) 1uy
|
||||||
|
pos
|
||||||
|
| _ ->
|
||||||
|
|
||||||
|
let pos = ofRowAndCol lineLength 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 startLine (startCol - 1)
|
||||||
|
|
||||||
|
do
|
||||||
|
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 pointB
|
||||||
|
setAt system (3 * col + 1) (3 * row + 1) s.[pointB] 1uy
|
||||||
|
|
||||||
|
while pointA <> pointB do
|
||||||
|
let currentA = pointA
|
||||||
|
pointA <- nextPoint s lineLength pointA prevPointA
|
||||||
|
prevPointA <- currentA
|
||||||
|
|
||||||
|
do
|
||||||
|
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 pointB prevPointB
|
||||||
|
prevPointB <- currentB
|
||||||
|
|
||||||
|
do
|
||||||
|
let struct (row, col) = toRowAndCol lineLength 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)
|
||||||
|
|
||||||
|
let mutable answer = 0
|
||||||
|
|
||||||
|
for row = 0 to lineCount - 1 do
|
||||||
|
for col = 0 to lineLength - 1 do
|
||||||
|
if Arr2D.get system (3 * col + 1) (3 * row + 1) = 0uy then
|
||||||
|
answer <- answer + 1
|
||||||
|
|
||||||
|
answer
|
93
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day11.fs
Normal file
93
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day11.fs
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
namespace AdventOfCode2023
|
||||||
|
|
||||||
|
[<RequireQualifiedAccess>]
|
||||||
|
module Day11 =
|
||||||
|
|
||||||
|
type Data =
|
||||||
|
{
|
||||||
|
RowsWithoutGalaxies : ResizeArray<int>
|
||||||
|
ColsWithoutGalaxies : ResizeArray<int>
|
||||||
|
/// row * col
|
||||||
|
Galaxies : ResizeArray<int * int>
|
||||||
|
}
|
||||||
|
|
||||||
|
let parse (s : string) : Data =
|
||||||
|
let galaxies = ResizeArray ()
|
||||||
|
let rowsWithoutGalaxies = ResizeArray ()
|
||||||
|
let mutable hasAnyGalaxy = false
|
||||||
|
let mutable currRowIndex = 0
|
||||||
|
let mutable currColIndex = 0
|
||||||
|
|
||||||
|
for c in s do
|
||||||
|
if c = '\n' then
|
||||||
|
if not hasAnyGalaxy then
|
||||||
|
rowsWithoutGalaxies.Add currRowIndex
|
||||||
|
|
||||||
|
currRowIndex <- currRowIndex + 1
|
||||||
|
currColIndex <- 0
|
||||||
|
hasAnyGalaxy <- false
|
||||||
|
elif c = '#' then
|
||||||
|
hasAnyGalaxy <- true
|
||||||
|
galaxies.Add (currRowIndex, currColIndex)
|
||||||
|
currColIndex <- currColIndex + 1
|
||||||
|
else
|
||||||
|
currColIndex <- currColIndex + 1
|
||||||
|
|
||||||
|
galaxies.Sort (fun (_, c1) (_, c2) -> compare c1 c2)
|
||||||
|
|
||||||
|
let colsWithoutGalaxies =
|
||||||
|
let result = ResizeArray ()
|
||||||
|
let mutable prevCol = 0
|
||||||
|
|
||||||
|
for _, c in galaxies do
|
||||||
|
if c > prevCol then
|
||||||
|
for j = prevCol + 1 to c - 1 do
|
||||||
|
result.Add j
|
||||||
|
|
||||||
|
prevCol <- c
|
||||||
|
|
||||||
|
result
|
||||||
|
|
||||||
|
{
|
||||||
|
RowsWithoutGalaxies = rowsWithoutGalaxies
|
||||||
|
ColsWithoutGalaxies = colsWithoutGalaxies
|
||||||
|
Galaxies = galaxies
|
||||||
|
}
|
||||||
|
|
||||||
|
let solve (data : Data) (expansion : uint64) =
|
||||||
|
let mutable answer = 0uL
|
||||||
|
|
||||||
|
for galaxy1 = 0 to data.Galaxies.Count - 1 do
|
||||||
|
let row1, col1 = data.Galaxies.[galaxy1]
|
||||||
|
|
||||||
|
for galaxy2 = galaxy1 + 1 to data.Galaxies.Count - 1 do
|
||||||
|
let row2, col2 = data.Galaxies.[galaxy2]
|
||||||
|
let baseDistance = uint64 (abs (row1 - row2) + abs (col1 - col2))
|
||||||
|
|
||||||
|
let extraDistance =
|
||||||
|
let mutable extraDistance = 0uL
|
||||||
|
|
||||||
|
for i = 1 + min row1 row2 to max row1 row2 - 1 do
|
||||||
|
if data.RowsWithoutGalaxies.Contains i then
|
||||||
|
extraDistance <- extraDistance + expansion - 1uL
|
||||||
|
|
||||||
|
for i = 1 + min col1 col2 to max col1 col2 - 1 do
|
||||||
|
if data.ColsWithoutGalaxies.Contains i then
|
||||||
|
extraDistance <- extraDistance + expansion - 1uL
|
||||||
|
|
||||||
|
extraDistance
|
||||||
|
|
||||||
|
answer <- answer + extraDistance + baseDistance
|
||||||
|
|
||||||
|
answer
|
||||||
|
|
||||||
|
|
||||||
|
let part1 (s : string) =
|
||||||
|
let data = parse s
|
||||||
|
|
||||||
|
solve data 2uL
|
||||||
|
|
||||||
|
let part2 (s : string) =
|
||||||
|
let data = parse s
|
||||||
|
|
||||||
|
solve data 1_000_000uL
|
196
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day12.fs
Normal file
196
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day12.fs
Normal file
@@ -0,0 +1,196 @@
|
|||||||
|
namespace AdventOfCode2023
|
||||||
|
|
||||||
|
open System
|
||||||
|
open System.Collections.Generic
|
||||||
|
open System.Globalization
|
||||||
|
|
||||||
|
[<RequireQualifiedAccess>]
|
||||||
|
module Day12 =
|
||||||
|
|
||||||
|
let rec solve
|
||||||
|
(dict : Dictionary<int * int, uint64>)
|
||||||
|
(line : ReadOnlySpan<char>)
|
||||||
|
(groups : IReadOnlyList<int>)
|
||||||
|
(remainingToFill : int)
|
||||||
|
(currentGroupIndex : int)
|
||||||
|
: uint64
|
||||||
|
=
|
||||||
|
if line.Length = 0 then
|
||||||
|
if currentGroupIndex = groups.Count then
|
||||||
|
LanguagePrimitives.GenericOne
|
||||||
|
else
|
||||||
|
LanguagePrimitives.GenericZero
|
||||||
|
elif currentGroupIndex = groups.Count then
|
||||||
|
if line.Contains '#' then
|
||||||
|
LanguagePrimitives.GenericZero
|
||||||
|
else
|
||||||
|
LanguagePrimitives.GenericOne
|
||||||
|
else
|
||||||
|
|
||||||
|
match dict.TryGetValue ((line.Length, currentGroupIndex)) with
|
||||||
|
| true, v -> v
|
||||||
|
| false, _ ->
|
||||||
|
|
||||||
|
if remainingToFill > line.Length then
|
||||||
|
dict.Add ((line.Length, currentGroupIndex), LanguagePrimitives.GenericZero)
|
||||||
|
LanguagePrimitives.GenericZero
|
||||||
|
else
|
||||||
|
|
||||||
|
match line.[0] with
|
||||||
|
| '#' ->
|
||||||
|
if currentGroupIndex >= groups.Count then
|
||||||
|
LanguagePrimitives.GenericZero
|
||||||
|
else
|
||||||
|
let mutable isOk = true
|
||||||
|
|
||||||
|
for i = 1 to groups.[currentGroupIndex] - 1 do
|
||||||
|
if isOk && (i >= line.Length || (line.[i] <> '#' && line.[i] <> '?')) then
|
||||||
|
isOk <- false
|
||||||
|
|
||||||
|
if not isOk then
|
||||||
|
LanguagePrimitives.GenericZero
|
||||||
|
else if groups.[currentGroupIndex] < line.Length then
|
||||||
|
if line.[groups.[currentGroupIndex]] = '#' then
|
||||||
|
LanguagePrimitives.GenericZero
|
||||||
|
else
|
||||||
|
solve
|
||||||
|
dict
|
||||||
|
(line.Slice (groups.[currentGroupIndex] + 1))
|
||||||
|
groups
|
||||||
|
(remainingToFill - groups.[currentGroupIndex] - 1)
|
||||||
|
(currentGroupIndex + 1)
|
||||||
|
else
|
||||||
|
solve
|
||||||
|
dict
|
||||||
|
ReadOnlySpan<_>.Empty
|
||||||
|
groups
|
||||||
|
(remainingToFill - groups.[currentGroupIndex] - 1)
|
||||||
|
(currentGroupIndex + 1)
|
||||||
|
| '.' -> solve dict (line.Slice 1) groups remainingToFill currentGroupIndex
|
||||||
|
| '?' ->
|
||||||
|
let afterMark = line.IndexOfAnyExcept ('?', '#')
|
||||||
|
|
||||||
|
if afterMark >= 0 && groups.[currentGroupIndex] > afterMark then
|
||||||
|
// this group would extend into a dot if this were filled in!
|
||||||
|
let firstHash = line.IndexOf '#'
|
||||||
|
|
||||||
|
if firstHash >= 0 && firstHash < afterMark then
|
||||||
|
// this group *is* filled in, contradiction
|
||||||
|
LanguagePrimitives.GenericZero
|
||||||
|
else
|
||||||
|
solve dict (line.Slice afterMark) groups remainingToFill currentGroupIndex
|
||||||
|
else
|
||||||
|
|
||||||
|
let ifDot = solve dict (line.Slice 1) groups remainingToFill currentGroupIndex
|
||||||
|
dict.TryAdd ((line.Length - 1, currentGroupIndex), ifDot) |> ignore
|
||||||
|
|
||||||
|
let ifHash =
|
||||||
|
if currentGroupIndex >= groups.Count then
|
||||||
|
LanguagePrimitives.GenericZero
|
||||||
|
else
|
||||||
|
|
||||||
|
let mutable isOk = true
|
||||||
|
|
||||||
|
for i = 1 to groups.[currentGroupIndex] - 1 do
|
||||||
|
if isOk && (i >= line.Length || (line.[i] <> '#' && line.[i] <> '?')) then
|
||||||
|
isOk <- false
|
||||||
|
|
||||||
|
if not isOk then
|
||||||
|
LanguagePrimitives.GenericZero
|
||||||
|
else if groups.[currentGroupIndex] < line.Length then
|
||||||
|
if
|
||||||
|
groups.[currentGroupIndex] < line.Length
|
||||||
|
&& line.[groups.[currentGroupIndex]] = '#'
|
||||||
|
then
|
||||||
|
LanguagePrimitives.GenericZero
|
||||||
|
else
|
||||||
|
solve
|
||||||
|
dict
|
||||||
|
(line.Slice (groups.[currentGroupIndex] + 1))
|
||||||
|
groups
|
||||||
|
(remainingToFill - groups.[currentGroupIndex] - 1)
|
||||||
|
(currentGroupIndex + 1)
|
||||||
|
else
|
||||||
|
solve
|
||||||
|
dict
|
||||||
|
ReadOnlySpan<_>.Empty
|
||||||
|
groups
|
||||||
|
(remainingToFill - groups.[currentGroupIndex] - 1)
|
||||||
|
(currentGroupIndex + 1)
|
||||||
|
|
||||||
|
let ans = ifDot + ifHash
|
||||||
|
dict.TryAdd ((line.Length, currentGroupIndex), ans) |> ignore
|
||||||
|
ans
|
||||||
|
| _ ->
|
||||||
|
if currentGroupIndex = groups.Count then
|
||||||
|
LanguagePrimitives.GenericOne
|
||||||
|
else
|
||||||
|
LanguagePrimitives.GenericZero
|
||||||
|
|
||||||
|
let part1 (s : string) =
|
||||||
|
use mutable lines = StringSplitEnumerator.make '\n' s
|
||||||
|
|
||||||
|
let mutable answer = 0uL
|
||||||
|
let arr = ResizeArray ()
|
||||||
|
|
||||||
|
let dict = Dictionary ()
|
||||||
|
|
||||||
|
for line in lines do
|
||||||
|
if not line.IsEmpty then
|
||||||
|
arr.Clear ()
|
||||||
|
use ints = StringSplitEnumerator.make' ',' (line.Slice (line.IndexOf ' ' + 1))
|
||||||
|
|
||||||
|
for int in ints do
|
||||||
|
arr.Add (Int32.Parse (int, NumberStyles.None, CultureInfo.InvariantCulture))
|
||||||
|
|
||||||
|
let remainingToFill =
|
||||||
|
let mutable ans = -1
|
||||||
|
|
||||||
|
for i = 0 to arr.Count - 1 do
|
||||||
|
ans <- ans + arr.[i] + 1
|
||||||
|
|
||||||
|
ans
|
||||||
|
|
||||||
|
dict.Clear ()
|
||||||
|
let solved = solve dict line arr remainingToFill 0
|
||||||
|
answer <- answer + solved
|
||||||
|
|
||||||
|
answer
|
||||||
|
|
||||||
|
let part2 (s : string) =
|
||||||
|
use mutable lines = StringSplitEnumerator.make '\n' s
|
||||||
|
|
||||||
|
let mutable answer = 0uL
|
||||||
|
let arr = ResizeArray ()
|
||||||
|
|
||||||
|
let dict = Dictionary ()
|
||||||
|
|
||||||
|
for line in lines do
|
||||||
|
if not line.IsEmpty then
|
||||||
|
arr.Clear ()
|
||||||
|
let spaceIndex = line.IndexOf ' '
|
||||||
|
|
||||||
|
for _ = 0 to 4 do
|
||||||
|
use ints = StringSplitEnumerator.make' ',' (line.Slice (spaceIndex + 1))
|
||||||
|
|
||||||
|
for int in ints do
|
||||||
|
arr.Add (Int32.Parse (int, NumberStyles.None, CultureInfo.InvariantCulture))
|
||||||
|
|
||||||
|
let sliced = line.Slice(0, spaceIndex).ToString ()
|
||||||
|
|
||||||
|
let line =
|
||||||
|
String.Concat (sliced, '?', sliced, '?', sliced, '?', sliced, '?', sliced)
|
||||||
|
|
||||||
|
let remainingToFill =
|
||||||
|
let mutable ans = -1
|
||||||
|
|
||||||
|
for i = 0 to arr.Count - 1 do
|
||||||
|
ans <- ans + arr.[i] + 1
|
||||||
|
|
||||||
|
ans
|
||||||
|
|
||||||
|
dict.Clear ()
|
||||||
|
let solved = solve dict (line.AsSpan ()) arr remainingToFill 0
|
||||||
|
answer <- answer + solved
|
||||||
|
|
||||||
|
answer
|
191
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day13.fs
Normal file
191
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day13.fs
Normal file
@@ -0,0 +1,191 @@
|
|||||||
|
namespace AdventOfCode2023
|
||||||
|
|
||||||
|
open System
|
||||||
|
|
||||||
|
[<RequireQualifiedAccess>]
|
||||||
|
module Day13 =
|
||||||
|
|
||||||
|
let inline isPowerOf2 (i : uint32) =
|
||||||
|
// https://stackoverflow.com/a/600306/126995
|
||||||
|
(i &&& (i - 1ul)) = 0ul
|
||||||
|
|
||||||
|
let rowToInt (row : ReadOnlySpan<char>) : uint32 =
|
||||||
|
let mutable mult = 1ul
|
||||||
|
let mutable answer = 0ul
|
||||||
|
|
||||||
|
for c = row.Length - 1 downto 0 do
|
||||||
|
if row.[c] = '#' then
|
||||||
|
answer <- answer + mult
|
||||||
|
|
||||||
|
mult <- mult * 2ul
|
||||||
|
|
||||||
|
answer
|
||||||
|
|
||||||
|
let colToInt (grid : ReadOnlySpan<char>) (rowLength : int) (colNum : int) =
|
||||||
|
let mutable mult = 1ul
|
||||||
|
let mutable answer = 0ul
|
||||||
|
|
||||||
|
for i = grid.Count '\n' - 1 downto 0 do
|
||||||
|
if grid.[i * (rowLength + 1) + colNum] = '#' then
|
||||||
|
answer <- answer + mult
|
||||||
|
|
||||||
|
mult <- mult * 2ul
|
||||||
|
|
||||||
|
answer
|
||||||
|
|
||||||
|
let verifyReflection (group : ResizeArray<'a>) (smaller : int) (bigger : int) : bool =
|
||||||
|
let midPoint = (smaller + bigger) / 2
|
||||||
|
|
||||||
|
let rec isOkWithin (curr : int) =
|
||||||
|
if smaller + curr > midPoint then
|
||||||
|
true
|
||||||
|
else if group.[smaller + curr] = group.[bigger - curr] then
|
||||||
|
isOkWithin (curr + 1)
|
||||||
|
else
|
||||||
|
false
|
||||||
|
|
||||||
|
if not (isOkWithin 0) then
|
||||||
|
false
|
||||||
|
else
|
||||||
|
|
||||||
|
smaller = 0 || bigger = group.Count - 1
|
||||||
|
|
||||||
|
/// Find reflection among rows.
|
||||||
|
/// Returns 0 to indicate "no answer".
|
||||||
|
[<TailCall>]
|
||||||
|
let rec findRow (banAnswer : uint32) (rows : ResizeArray<uint32>) (currentLine : int) : uint32 =
|
||||||
|
if currentLine = rows.Count - 1 then
|
||||||
|
0ul
|
||||||
|
else
|
||||||
|
let mutable answer = UInt32.MaxValue
|
||||||
|
let mutable i = currentLine
|
||||||
|
|
||||||
|
while i < rows.Count - 1 do
|
||||||
|
i <- i + 1
|
||||||
|
|
||||||
|
if currentLine % 2 <> i % 2 then
|
||||||
|
if rows.[i] = rows.[currentLine] then
|
||||||
|
if verifyReflection rows currentLine i then
|
||||||
|
let desiredAnswer = uint32 (((currentLine + i) / 2) + 1)
|
||||||
|
|
||||||
|
if desiredAnswer <> banAnswer then
|
||||||
|
answer <- uint32 desiredAnswer
|
||||||
|
i <- Int32.MaxValue
|
||||||
|
|
||||||
|
if answer < UInt32.MaxValue then
|
||||||
|
answer
|
||||||
|
else
|
||||||
|
findRow banAnswer rows (currentLine + 1)
|
||||||
|
|
||||||
|
let render (rowBuf : ResizeArray<_>) (colBuf : ResizeArray<_>) (group : ReadOnlySpan<char>) =
|
||||||
|
rowBuf.Clear ()
|
||||||
|
colBuf.Clear ()
|
||||||
|
let lineLength = group.IndexOf '\n'
|
||||||
|
|
||||||
|
for col = 0 to lineLength - 1 do
|
||||||
|
colBuf.Add (colToInt group lineLength col)
|
||||||
|
|
||||||
|
for row in StringSplitEnumerator.make' '\n' group do
|
||||||
|
if not row.IsEmpty then
|
||||||
|
rowBuf.Add (rowToInt row)
|
||||||
|
|
||||||
|
/// Returns 0 to indicate "no solution".
|
||||||
|
let solve (banAnswer : uint32) (rowBuf : ResizeArray<_>) (colBuf : ResizeArray<_>) : uint32 =
|
||||||
|
match
|
||||||
|
findRow
|
||||||
|
(if banAnswer >= 100ul then
|
||||||
|
banAnswer / 100ul
|
||||||
|
else
|
||||||
|
UInt32.MaxValue)
|
||||||
|
rowBuf
|
||||||
|
0
|
||||||
|
with
|
||||||
|
| rowIndex when rowIndex > 0ul -> 100ul * rowIndex
|
||||||
|
| _ -> findRow banAnswer colBuf 0
|
||||||
|
|
||||||
|
/// Returns also the group with this gro
|
||||||
|
let peelGroup (s : ReadOnlySpan<char>) : ReadOnlySpan<char> =
|
||||||
|
let index = s.IndexOf "\n\n"
|
||||||
|
|
||||||
|
if index < 0 then
|
||||||
|
// last group
|
||||||
|
s
|
||||||
|
else
|
||||||
|
s.Slice (0, index + 1)
|
||||||
|
|
||||||
|
let part1 (s : string) =
|
||||||
|
let mutable s = s.AsSpan ()
|
||||||
|
let rows = ResizeArray ()
|
||||||
|
let cols = ResizeArray ()
|
||||||
|
let mutable answer = 0ul
|
||||||
|
|
||||||
|
while not s.IsEmpty do
|
||||||
|
let group = peelGroup s
|
||||||
|
|
||||||
|
render rows cols group
|
||||||
|
// There's an obvious perf optimisation where we don't compute cols
|
||||||
|
// until we know there's no row answer. Life's too short.
|
||||||
|
answer <- answer + solve UInt32.MaxValue rows cols
|
||||||
|
|
||||||
|
if group.Length >= s.Length then
|
||||||
|
s <- ReadOnlySpan<char>.Empty
|
||||||
|
else
|
||||||
|
s <- s.Slice (group.Length + 1)
|
||||||
|
|
||||||
|
answer
|
||||||
|
|
||||||
|
let flipAt (rows : ResizeArray<_>) (cols : ResizeArray<_>) (rowNum : int) (colNum : int) : unit =
|
||||||
|
rows.[rowNum] <-
|
||||||
|
let index = 1ul <<< (cols.Count - colNum - 1)
|
||||||
|
|
||||||
|
if rows.[rowNum] &&& index > 0ul then
|
||||||
|
rows.[rowNum] - index
|
||||||
|
else
|
||||||
|
rows.[rowNum] + index
|
||||||
|
|
||||||
|
cols.[colNum] <-
|
||||||
|
let index = 1ul <<< (rows.Count - rowNum - 1)
|
||||||
|
|
||||||
|
if cols.[colNum] &&& index > 0ul then
|
||||||
|
cols.[colNum] - index
|
||||||
|
else
|
||||||
|
cols.[colNum] + index
|
||||||
|
|
||||||
|
let part2 (s : string) =
|
||||||
|
let mutable s = s.AsSpan ()
|
||||||
|
let rows = ResizeArray ()
|
||||||
|
let cols = ResizeArray ()
|
||||||
|
let mutable answer = 0ul
|
||||||
|
|
||||||
|
while not s.IsEmpty do
|
||||||
|
let group = peelGroup s
|
||||||
|
|
||||||
|
render rows cols group
|
||||||
|
|
||||||
|
let bannedAnswer = solve UInt32.MaxValue rows cols
|
||||||
|
|
||||||
|
let mutable isDone = false
|
||||||
|
let mutable rowToChange = 0
|
||||||
|
|
||||||
|
while not isDone && rowToChange < rows.Count do
|
||||||
|
let mutable colToChange = 0
|
||||||
|
|
||||||
|
while not isDone && colToChange < cols.Count do
|
||||||
|
flipAt rows cols rowToChange colToChange
|
||||||
|
|
||||||
|
match solve bannedAnswer rows cols with
|
||||||
|
| solved when solved > 0ul ->
|
||||||
|
isDone <- true
|
||||||
|
answer <- answer + solved
|
||||||
|
| _ ->
|
||||||
|
flipAt rows cols rowToChange colToChange
|
||||||
|
colToChange <- colToChange + 1
|
||||||
|
|
||||||
|
rowToChange <- rowToChange + 1
|
||||||
|
|
||||||
|
if group.Length >= s.Length then
|
||||||
|
s <- ReadOnlySpan<char>.Empty
|
||||||
|
else
|
||||||
|
s <- s.Slice (group.Length + 1)
|
||||||
|
|
||||||
|
answer
|
292
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day14.fs
Normal file
292
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day14.fs
Normal file
@@ -0,0 +1,292 @@
|
|||||||
|
namespace AdventOfCode2023
|
||||||
|
|
||||||
|
#if DEBUG
|
||||||
|
#else
|
||||||
|
#nowarn "9"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
open System
|
||||||
|
|
||||||
|
[<RequireQualifiedAccess>]
|
||||||
|
module Day14 =
|
||||||
|
let slideNorth (arr : Arr2D<byte>) : unit =
|
||||||
|
for col = 0 to arr.Width - 1 do
|
||||||
|
let mutable targetPos = -1
|
||||||
|
let mutable pos = 0
|
||||||
|
|
||||||
|
while targetPos = -1 do
|
||||||
|
if Arr2D.get arr col pos = 0uy then
|
||||||
|
targetPos <- pos
|
||||||
|
|
||||||
|
pos <- pos + 1
|
||||||
|
|
||||||
|
while pos < arr.Height do
|
||||||
|
let current = Arr2D.get arr col pos
|
||||||
|
|
||||||
|
if current = 2uy then
|
||||||
|
targetPos <- pos + 1
|
||||||
|
let mutable hasMoved = false
|
||||||
|
|
||||||
|
while not hasMoved do
|
||||||
|
if Arr2D.get arr col pos = 0uy then
|
||||||
|
targetPos <- pos
|
||||||
|
hasMoved <- true
|
||||||
|
|
||||||
|
pos <- pos + 1
|
||||||
|
elif current = 1uy then
|
||||||
|
Arr2D.set arr col targetPos 1uy
|
||||||
|
Arr2D.set arr col pos 0uy
|
||||||
|
targetPos <- targetPos + 1
|
||||||
|
pos <- pos + 1
|
||||||
|
else // current = 0uy
|
||||||
|
pos <- pos + 1
|
||||||
|
|
||||||
|
let slideSouth (arr : Arr2D<byte>) : unit =
|
||||||
|
for col = 0 to arr.Width - 1 do
|
||||||
|
let mutable targetPos = arr.Height
|
||||||
|
let mutable pos = arr.Height - 1
|
||||||
|
|
||||||
|
while targetPos = arr.Height do
|
||||||
|
if Arr2D.get arr col pos = 0uy then
|
||||||
|
targetPos <- pos
|
||||||
|
|
||||||
|
pos <- pos - 1
|
||||||
|
|
||||||
|
while pos >= 0 do
|
||||||
|
let current = Arr2D.get arr col pos
|
||||||
|
|
||||||
|
if current = 2uy then
|
||||||
|
targetPos <- pos - 1
|
||||||
|
let mutable hasMoved = false
|
||||||
|
|
||||||
|
while not hasMoved do
|
||||||
|
if Arr2D.get arr col pos = 0uy then
|
||||||
|
targetPos <- pos
|
||||||
|
hasMoved <- true
|
||||||
|
|
||||||
|
pos <- pos - 1
|
||||||
|
elif current = 1uy then
|
||||||
|
Arr2D.set arr col targetPos 1uy
|
||||||
|
Arr2D.set arr col pos 0uy
|
||||||
|
targetPos <- targetPos - 1
|
||||||
|
pos <- pos - 1
|
||||||
|
else // current = 0uy
|
||||||
|
pos <- pos - 1
|
||||||
|
|
||||||
|
let slideEast (arr : Arr2D<byte>) : unit =
|
||||||
|
for row = 0 to arr.Height - 1 do
|
||||||
|
let mutable targetPos = arr.Width
|
||||||
|
let mutable pos = arr.Width - 1
|
||||||
|
|
||||||
|
while targetPos = arr.Width do
|
||||||
|
if Arr2D.get arr pos row = 0uy then
|
||||||
|
targetPos <- pos
|
||||||
|
|
||||||
|
pos <- pos - 1
|
||||||
|
|
||||||
|
while pos >= 0 do
|
||||||
|
let current = Arr2D.get arr pos row
|
||||||
|
|
||||||
|
if current = 2uy then
|
||||||
|
targetPos <- pos - 1
|
||||||
|
let mutable hasMoved = false
|
||||||
|
|
||||||
|
while not hasMoved do
|
||||||
|
if Arr2D.get arr pos row = 0uy then
|
||||||
|
targetPos <- pos
|
||||||
|
hasMoved <- true
|
||||||
|
|
||||||
|
pos <- pos - 1
|
||||||
|
elif current = 1uy then
|
||||||
|
Arr2D.set arr targetPos row 1uy
|
||||||
|
Arr2D.set arr pos row 0uy
|
||||||
|
targetPos <- targetPos - 1
|
||||||
|
pos <- pos - 1
|
||||||
|
else // current = 0uy
|
||||||
|
pos <- pos - 1
|
||||||
|
|
||||||
|
let slideWest (arr : Arr2D<byte>) : unit =
|
||||||
|
for row = 0 to arr.Height - 1 do
|
||||||
|
let mutable targetPos = -1
|
||||||
|
let mutable pos = 0
|
||||||
|
|
||||||
|
while targetPos = -1 do
|
||||||
|
if Arr2D.get arr pos row = 0uy then
|
||||||
|
targetPos <- pos
|
||||||
|
|
||||||
|
pos <- pos + 1
|
||||||
|
|
||||||
|
while pos < arr.Height do
|
||||||
|
let current = Arr2D.get arr pos row
|
||||||
|
|
||||||
|
if current = 2uy then
|
||||||
|
targetPos <- pos + 1
|
||||||
|
let mutable hasMoved = false
|
||||||
|
|
||||||
|
while not hasMoved do
|
||||||
|
if Arr2D.get arr pos row = 0uy then
|
||||||
|
targetPos <- pos
|
||||||
|
hasMoved <- true
|
||||||
|
|
||||||
|
pos <- pos + 1
|
||||||
|
elif current = 1uy then
|
||||||
|
Arr2D.set arr targetPos row 1uy
|
||||||
|
Arr2D.set arr pos row 0uy
|
||||||
|
targetPos <- targetPos + 1
|
||||||
|
pos <- pos + 1
|
||||||
|
else // current = 0uy
|
||||||
|
pos <- pos + 1
|
||||||
|
|
||||||
|
let print (board : Arr2D<byte>) =
|
||||||
|
for row = 0 to board.Height - 1 do
|
||||||
|
for col = 0 to board.Width - 1 do
|
||||||
|
match Arr2D.get board col row with
|
||||||
|
| 0uy -> printf "."
|
||||||
|
| 1uy -> printf "O"
|
||||||
|
| 2uy -> printf "#"
|
||||||
|
| _ -> failwith "bad value"
|
||||||
|
|
||||||
|
printfn ""
|
||||||
|
|
||||||
|
printfn ""
|
||||||
|
|
||||||
|
let score (board : Arr2D<byte>) =
|
||||||
|
let mutable answer = 0ul
|
||||||
|
|
||||||
|
for row = 0 to board.Height - 1 do
|
||||||
|
for col = 0 to board.Width - 1 do
|
||||||
|
if Arr2D.get board col row = 1uy then
|
||||||
|
answer <- answer + (board.Height - row |> uint32)
|
||||||
|
|
||||||
|
answer
|
||||||
|
|
||||||
|
let hash (board : Arr2D<byte>) =
|
||||||
|
let mutable hash = 0uL
|
||||||
|
let mutable pos = 0uL
|
||||||
|
|
||||||
|
for x = 0 to board.Width - 1 do
|
||||||
|
for y = 0 to board.Height - 1 do
|
||||||
|
hash <- hash + pos * uint64 (Arr2D.get board x y)
|
||||||
|
pos <- pos + 1uL
|
||||||
|
|
||||||
|
hash
|
||||||
|
|
||||||
|
let part1 (s : string) =
|
||||||
|
let s = s.AsSpan ()
|
||||||
|
let lineLength = s.IndexOf '\n'
|
||||||
|
|
||||||
|
let buffer = Array.zeroCreate (lineLength * s.Length / (lineLength + 1))
|
||||||
|
let mutable i = 0
|
||||||
|
|
||||||
|
for c in s do
|
||||||
|
match c with
|
||||||
|
| '#' -> buffer.[i] <- 2uy
|
||||||
|
| '.' -> buffer.[i] <- 0uy
|
||||||
|
| 'O' -> buffer.[i] <- 1uy
|
||||||
|
| '\n' -> i <- i - 1
|
||||||
|
| _ -> failwith "bad char"
|
||||||
|
|
||||||
|
i <- i + 1
|
||||||
|
|
||||||
|
#if DEBUG
|
||||||
|
let system : Arr2D<byte> =
|
||||||
|
{
|
||||||
|
Elements = buffer
|
||||||
|
Width = lineLength
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
use ptr = fixed buffer
|
||||||
|
|
||||||
|
let system : Arr2D<byte> =
|
||||||
|
{
|
||||||
|
Elements = ptr
|
||||||
|
Length = buffer.Length
|
||||||
|
Width = lineLength
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
slideNorth system
|
||||||
|
|
||||||
|
score system
|
||||||
|
|
||||||
|
let cycleOnce (arr : Arr2D<_>) =
|
||||||
|
slideNorth arr
|
||||||
|
slideWest arr
|
||||||
|
slideSouth arr
|
||||||
|
slideEast arr
|
||||||
|
|
||||||
|
let part2 (s : string) =
|
||||||
|
let s = s.AsSpan ()
|
||||||
|
let lineLength = s.IndexOf '\n'
|
||||||
|
|
||||||
|
let buffer = Array.zeroCreate (lineLength * s.Length / (lineLength + 1))
|
||||||
|
let mutable i = 0
|
||||||
|
|
||||||
|
for c in s do
|
||||||
|
match c with
|
||||||
|
| '#' -> buffer.[i] <- 2uy
|
||||||
|
| '.' -> buffer.[i] <- 0uy
|
||||||
|
| 'O' -> buffer.[i] <- 1uy
|
||||||
|
| '\n' -> i <- i - 1
|
||||||
|
| _ -> failwith "bad char"
|
||||||
|
|
||||||
|
i <- i + 1
|
||||||
|
|
||||||
|
#if DEBUG
|
||||||
|
let system : Arr2D<byte> =
|
||||||
|
{
|
||||||
|
Elements = buffer
|
||||||
|
Width = lineLength
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
use ptr = fixed buffer
|
||||||
|
|
||||||
|
let system : Arr2D<byte> =
|
||||||
|
{
|
||||||
|
Elements = ptr
|
||||||
|
Length = buffer.Length
|
||||||
|
Width = lineLength
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
let mutable tortoise = 1
|
||||||
|
let mutable hare = 2
|
||||||
|
let scores = ResizeArray<_> ()
|
||||||
|
scores.Add (score system, hash system)
|
||||||
|
cycleOnce system
|
||||||
|
scores.Add (score system, hash system)
|
||||||
|
cycleOnce system
|
||||||
|
scores.Add (score system, hash system)
|
||||||
|
|
||||||
|
while scores.[hare] <> scores.[tortoise] do
|
||||||
|
cycleOnce system
|
||||||
|
scores.Add (score system, hash system)
|
||||||
|
cycleOnce system
|
||||||
|
scores.Add (score system, hash system)
|
||||||
|
|
||||||
|
hare <- hare + 2
|
||||||
|
tortoise <- tortoise + 1
|
||||||
|
|
||||||
|
tortoise <- 0
|
||||||
|
// mu-table heh heh
|
||||||
|
let mutable firstRepetition = 0
|
||||||
|
|
||||||
|
while scores.[hare] <> scores.[tortoise] do
|
||||||
|
cycleOnce system
|
||||||
|
scores.Add (score system, hash system)
|
||||||
|
hare <- hare + 1
|
||||||
|
tortoise <- tortoise + 1
|
||||||
|
firstRepetition <- firstRepetition + 1
|
||||||
|
|
||||||
|
let mutable cycleLength = 1
|
||||||
|
hare <- tortoise + 1
|
||||||
|
|
||||||
|
while scores.[tortoise] <> scores.[hare] do
|
||||||
|
hare <- hare + 1
|
||||||
|
cycleOnce system
|
||||||
|
scores.Add (score system, hash system)
|
||||||
|
cycleLength <- cycleLength + 1
|
||||||
|
|
||||||
|
let cycles = (1_000_000_000uL - uint64 firstRepetition) % (uint64 cycleLength)
|
||||||
|
|
||||||
|
fst scores.[firstRepetition + int cycles]
|
55
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day9.fs
Normal file
55
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day9.fs
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
namespace AdventOfCode2023
|
||||||
|
|
||||||
|
open System
|
||||||
|
|
||||||
|
[<RequireQualifiedAccess>]
|
||||||
|
module Day9 =
|
||||||
|
|
||||||
|
let extrapolate (isStart : bool) (arr : ResizeArray<int64>) =
|
||||||
|
let mutable answer = 0L
|
||||||
|
let pos = if isStart then -1L else int64 arr.Count
|
||||||
|
|
||||||
|
for i = 0 to arr.Count - 1 do
|
||||||
|
let mutable product = Rational.ofInt arr.[i]
|
||||||
|
|
||||||
|
for j = 0 to arr.Count - 1 do
|
||||||
|
if j <> i then
|
||||||
|
product <- product * Rational.make (pos - int64 j) (int64 i - int64 j)
|
||||||
|
|
||||||
|
answer <- answer + Rational.assertIntegral product
|
||||||
|
|
||||||
|
answer
|
||||||
|
|
||||||
|
let part1 (s : string) =
|
||||||
|
use s = StringSplitEnumerator.make '\n' s
|
||||||
|
let mutable answer = 0L
|
||||||
|
let arr = ResizeArray ()
|
||||||
|
|
||||||
|
for line in s do
|
||||||
|
arr.Clear ()
|
||||||
|
use line = StringSplitEnumerator.make' ' ' line
|
||||||
|
|
||||||
|
for number in line do
|
||||||
|
let number = Int64.Parse number
|
||||||
|
arr.Add number
|
||||||
|
|
||||||
|
answer <- answer + extrapolate false arr
|
||||||
|
|
||||||
|
answer
|
||||||
|
|
||||||
|
let part2 (s : string) =
|
||||||
|
use s = StringSplitEnumerator.make '\n' s
|
||||||
|
let mutable answer = 0L
|
||||||
|
let arr = ResizeArray ()
|
||||||
|
|
||||||
|
for line in s do
|
||||||
|
arr.Clear ()
|
||||||
|
use line = StringSplitEnumerator.make' ' ' line
|
||||||
|
|
||||||
|
for number in line do
|
||||||
|
let number = Int64.Parse number
|
||||||
|
arr.Add number
|
||||||
|
|
||||||
|
answer <- answer + extrapolate true arr
|
||||||
|
|
||||||
|
answer
|
@@ -1,7 +1,6 @@
|
|||||||
namespace AdventOfCode2023
|
namespace AdventOfCode2023
|
||||||
|
|
||||||
open System
|
open System
|
||||||
open System.Globalization
|
|
||||||
open System.Runtime.CompilerServices
|
open System.Runtime.CompilerServices
|
||||||
|
|
||||||
type EfficientString = System.ReadOnlySpan<char>
|
type EfficientString = System.ReadOnlySpan<char>
|
||||||
|
@@ -0,0 +1,82 @@
|
|||||||
|
namespace AdventOfCode2023
|
||||||
|
|
||||||
|
[<Struct>]
|
||||||
|
type Rational<'a
|
||||||
|
when 'a : (static member (+) : 'a * 'a -> 'a)
|
||||||
|
and 'a : (static member (*) : 'a * 'a -> 'a)
|
||||||
|
and 'a : (static member (/) : 'a * 'a -> 'a)
|
||||||
|
and 'a : (static member (-) : 'a * 'a -> 'a)
|
||||||
|
and 'a : (static member Zero : 'a)
|
||||||
|
and 'a : (static member One : 'a)
|
||||||
|
and 'a : comparison> =
|
||||||
|
{
|
||||||
|
Numerator : 'a
|
||||||
|
Denominator : 'a
|
||||||
|
}
|
||||||
|
|
||||||
|
static member inline (+) (a : Rational<'a>, b : Rational<'a>) =
|
||||||
|
let numerator = a.Numerator * b.Denominator + b.Numerator * a.Denominator
|
||||||
|
let denominator = a.Denominator * b.Denominator
|
||||||
|
let hcf = (Arithmetic.euclideanAlgorithm numerator denominator).Hcf
|
||||||
|
|
||||||
|
{
|
||||||
|
Numerator = numerator / hcf
|
||||||
|
Denominator = denominator / hcf
|
||||||
|
}
|
||||||
|
|
||||||
|
static member inline (*) (a : Rational<'a>, b : Rational<'a>) =
|
||||||
|
let numerator = a.Numerator * b.Numerator
|
||||||
|
let denominator = a.Denominator * b.Denominator
|
||||||
|
let hcf = (Arithmetic.euclideanAlgorithm numerator denominator).Hcf
|
||||||
|
|
||||||
|
{
|
||||||
|
Numerator = numerator / hcf
|
||||||
|
Denominator = denominator / hcf
|
||||||
|
}
|
||||||
|
|
||||||
|
[<RequireQualifiedAccess>]
|
||||||
|
module Rational =
|
||||||
|
let inline ofInt< ^a
|
||||||
|
when 'a : (static member (+) : 'a * 'a -> 'a)
|
||||||
|
and 'a : (static member (*) : 'a * 'a -> 'a)
|
||||||
|
and 'a : (static member (/) : 'a * 'a -> 'a)
|
||||||
|
and 'a : (static member (-) : 'a * 'a -> 'a)
|
||||||
|
and 'a : (static member Zero : 'a)
|
||||||
|
and 'a : (static member One : 'a)
|
||||||
|
and 'a : comparison>
|
||||||
|
(a : 'a)
|
||||||
|
=
|
||||||
|
{
|
||||||
|
Numerator = a
|
||||||
|
Denominator = LanguagePrimitives.GenericOne
|
||||||
|
}
|
||||||
|
|
||||||
|
let inline make< ^a
|
||||||
|
when 'a : (static member (+) : 'a * 'a -> 'a)
|
||||||
|
and 'a : (static member (*) : 'a * 'a -> 'a)
|
||||||
|
and 'a : (static member (/) : 'a * 'a -> 'a)
|
||||||
|
and 'a : (static member (-) : 'a * 'a -> 'a)
|
||||||
|
and 'a : (static member Zero : 'a)
|
||||||
|
and 'a : (static member One : 'a)
|
||||||
|
and 'a : comparison>
|
||||||
|
(numerator : 'a)
|
||||||
|
(denominator : 'a)
|
||||||
|
=
|
||||||
|
let hcf = (Arithmetic.euclideanAlgorithm numerator denominator).Hcf
|
||||||
|
|
||||||
|
{
|
||||||
|
Numerator = numerator / hcf
|
||||||
|
Denominator = denominator / hcf
|
||||||
|
}
|
||||||
|
|
||||||
|
let inline assertIntegral< ^a
|
||||||
|
when 'a : (static member (+) : 'a * 'a -> 'a)
|
||||||
|
and 'a : (static member (*) : 'a * 'a -> 'a)
|
||||||
|
and 'a : (static member (/) : 'a * 'a -> 'a)
|
||||||
|
and 'a : (static member (-) : 'a * 'a -> 'a)
|
||||||
|
and 'a : (static member Zero : 'a)
|
||||||
|
and 'a : (static member One : 'a)
|
||||||
|
and 'a : comparison>
|
||||||
|
(r : Rational<'a>)
|
||||||
|
=
|
||||||
|
r.Numerator
|
@@ -6,6 +6,8 @@ Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Test", "Test\Test.fsproj",
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "AdventOfCode2023.FSharp.Lib", "AdventOfCode2023.FSharp.Lib\AdventOfCode2023.FSharp.Lib.fsproj", "{95CE0568-3D1A-4060-BB54-52460FB1E399}"
|
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "AdventOfCode2023.FSharp.Lib", "AdventOfCode2023.FSharp.Lib\AdventOfCode2023.FSharp.Lib.fsproj", "{95CE0568-3D1A-4060-BB54-52460FB1E399}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "AdventOfCode2023.FSharp.Bench", "AdventOfCode2023.FSharp.Bench\AdventOfCode2023.FSharp.Bench.fsproj", "{5FD3221D-2C90-4173-8FC0-90553CEE1D4A}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@@ -24,5 +26,9 @@ Global
|
|||||||
{95CE0568-3D1A-4060-BB54-52460FB1E399}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
{95CE0568-3D1A-4060-BB54-52460FB1E399}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
{95CE0568-3D1A-4060-BB54-52460FB1E399}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
{95CE0568-3D1A-4060-BB54-52460FB1E399}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
{95CE0568-3D1A-4060-BB54-52460FB1E399}.Release|Any CPU.Build.0 = Release|Any CPU
|
{95CE0568-3D1A-4060-BB54-52460FB1E399}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{5FD3221D-2C90-4173-8FC0-90553CEE1D4A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{5FD3221D-2C90-4173-8FC0-90553CEE1D4A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{5FD3221D-2C90-4173-8FC0-90553CEE1D4A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{5FD3221D-2C90-4173-8FC0-90553CEE1D4A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
EndGlobal
|
EndGlobal
|
||||||
|
@@ -1,28 +1,28 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<OutputType>Exe</OutputType>
|
<OutputType>Exe</OutputType>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
<PublishAot>true</PublishAot>
|
<PublishAot>true</PublishAot>
|
||||||
<InvariantGlobalization>true</InvariantGlobalization>
|
<InvariantGlobalization>true</InvariantGlobalization>
|
||||||
<UseSystemResourceKeys>true</UseSystemResourceKeys>
|
<UseSystemResourceKeys>true</UseSystemResourceKeys>
|
||||||
|
|
||||||
<IlcOptimizationPreference>Speed</IlcOptimizationPreference>
|
<IlcOptimizationPreference>Speed</IlcOptimizationPreference>
|
||||||
<IlcGenerateStackTraceData>false</IlcGenerateStackTraceData>
|
<IlcGenerateStackTraceData>false</IlcGenerateStackTraceData>
|
||||||
|
|
||||||
<DebuggerSupport>false</DebuggerSupport>
|
<DebuggerSupport>false</DebuggerSupport>
|
||||||
<EnableUnsafeBinaryFormatterSerialization>false</EnableUnsafeBinaryFormatterSerialization>
|
<EnableUnsafeBinaryFormatterSerialization>false</EnableUnsafeBinaryFormatterSerialization>
|
||||||
<EventSourceSupport>false</EventSourceSupport>
|
<EventSourceSupport>false</EventSourceSupport>
|
||||||
<HttpActivityPropagationSupport>false</HttpActivityPropagationSupport>
|
<HttpActivityPropagationSupport>false</HttpActivityPropagationSupport>
|
||||||
<MetadataUpdaterSupport>false</MetadataUpdaterSupport>
|
<MetadataUpdaterSupport>false</MetadataUpdaterSupport>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Program.fs"/>
|
<Compile Include="Program.fs"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\AdventOfCode2023.FSharp.Lib\AdventOfCode2023.FSharp.Lib.fsproj" />
|
<ProjectReference Include="..\AdventOfCode2023.FSharp.Lib\AdventOfCode2023.FSharp.Lib.fsproj"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@@ -179,6 +179,111 @@ module Program =
|
|||||||
Console.WriteLine (part2.ToString ())
|
Console.WriteLine (part2.ToString ())
|
||||||
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
|
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
|
||||||
|
|
||||||
|
Console.WriteLine "=====Day 9====="
|
||||||
|
|
||||||
|
do
|
||||||
|
let input = Path.Combine (dir.FullName, "day9.txt") |> File.ReadAllText
|
||||||
|
|
||||||
|
sw.Restart ()
|
||||||
|
let part1 = Day9.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 = Day9.part2 input
|
||||||
|
sw.Stop ()
|
||||||
|
Console.WriteLine (part2.ToString ())
|
||||||
|
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
|
||||||
|
|
||||||
|
Console.WriteLine "=====Day 10====="
|
||||||
|
|
||||||
|
do
|
||||||
|
let input = Path.Combine (dir.FullName, "day10.txt") |> File.ReadAllText
|
||||||
|
|
||||||
|
sw.Restart ()
|
||||||
|
let part1 = Day10.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 = Day10.part2 input
|
||||||
|
sw.Stop ()
|
||||||
|
Console.WriteLine (part2.ToString ())
|
||||||
|
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
|
||||||
|
|
||||||
|
Console.WriteLine "=====Day 11====="
|
||||||
|
|
||||||
|
do
|
||||||
|
let input = Path.Combine (dir.FullName, "day11.txt") |> File.ReadAllText
|
||||||
|
|
||||||
|
sw.Restart ()
|
||||||
|
let data = Day11.parse input
|
||||||
|
sw.Stop ()
|
||||||
|
|
||||||
|
Console.Error.WriteLine (
|
||||||
|
(1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString ()
|
||||||
|
+ "ms parse"
|
||||||
|
)
|
||||||
|
|
||||||
|
sw.Restart ()
|
||||||
|
let part1 = Day11.solve data 2uL
|
||||||
|
sw.Stop ()
|
||||||
|
Console.WriteLine (part1.ToString ())
|
||||||
|
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
|
||||||
|
sw.Restart ()
|
||||||
|
let part2 = Day11.solve data 1_000_000uL
|
||||||
|
sw.Stop ()
|
||||||
|
Console.WriteLine (part2.ToString ())
|
||||||
|
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
|
||||||
|
|
||||||
|
Console.WriteLine "=====Day 12====="
|
||||||
|
|
||||||
|
do
|
||||||
|
let input = Path.Combine (dir.FullName, "day12.txt") |> File.ReadAllText
|
||||||
|
|
||||||
|
sw.Restart ()
|
||||||
|
let part1 = Day12.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 = Day12.part2 input
|
||||||
|
sw.Stop ()
|
||||||
|
Console.WriteLine (part2.ToString ())
|
||||||
|
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
|
||||||
|
|
||||||
|
Console.WriteLine "=====Day 13====="
|
||||||
|
|
||||||
|
do
|
||||||
|
let input = Path.Combine (dir.FullName, "day13.txt") |> File.ReadAllText
|
||||||
|
|
||||||
|
sw.Restart ()
|
||||||
|
let part1 = Day13.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 = Day13.part2 input
|
||||||
|
sw.Stop ()
|
||||||
|
Console.WriteLine (part2.ToString ())
|
||||||
|
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
|
||||||
|
|
||||||
|
Console.WriteLine "=====Day 14====="
|
||||||
|
|
||||||
|
do
|
||||||
|
let input = Path.Combine (dir.FullName, "day14.txt") |> File.ReadAllText
|
||||||
|
|
||||||
|
sw.Restart ()
|
||||||
|
let part1 = Day14.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 = Day14.part2 input
|
||||||
|
sw.Stop ()
|
||||||
|
Console.WriteLine (part2.ToString ())
|
||||||
|
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
|
||||||
|
|
||||||
endToEnd.Stop ()
|
endToEnd.Stop ()
|
||||||
|
|
||||||
Console.Error.WriteLine (
|
Console.Error.WriteLine (
|
||||||
|
@@ -1,44 +1,57 @@
|
|||||||
<Project Sdk="Microsoft.NET.Sdk">
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net8.0</TargetFramework>
|
<TargetFramework>net8.0</TargetFramework>
|
||||||
|
|
||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
<IsTestProject>true</IsTestProject>
|
<IsTestProject>true</IsTestProject>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Util.fs" />
|
<Compile Include="Util.fs"/>
|
||||||
<Compile Include="TestDay1.fs" />
|
<Compile Include="TestDay1.fs"/>
|
||||||
<Compile Include="TestDay2.fs" />
|
<Compile Include="TestDay2.fs"/>
|
||||||
<Compile Include="TestDay3.fs" />
|
<Compile Include="TestDay3.fs"/>
|
||||||
<Compile Include="TestDay4.fs" />
|
<Compile Include="TestDay4.fs"/>
|
||||||
<Compile Include="TestDay5.fs" />
|
<Compile Include="TestDay5.fs"/>
|
||||||
<Compile Include="TestDay6.fs" />
|
<Compile Include="TestDay6.fs"/>
|
||||||
<Compile Include="TestDay7.fs" />
|
<Compile Include="TestDay7.fs"/>
|
||||||
<Compile Include="TestDay8.fs" />
|
<Compile Include="TestDay8.fs"/>
|
||||||
<EmbeddedResource Include="samples\day1.txt" />
|
<Compile Include="TestDay9.fs"/>
|
||||||
<EmbeddedResource Include="samples\day1part1.txt" />
|
<Compile Include="TestDay10.fs" />
|
||||||
<EmbeddedResource Include="samples\day2.txt" />
|
<Compile Include="TestDay11.fs" />
|
||||||
<EmbeddedResource Include="samples\day3.txt" />
|
<Compile Include="TestDay12.fs" />
|
||||||
<EmbeddedResource Include="samples\day4.txt" />
|
<Compile Include="TestDay13.fs" />
|
||||||
<EmbeddedResource Include="samples\day5.txt" />
|
<Compile Include="TestDay14.fs" />
|
||||||
<EmbeddedResource Include="samples\day6.txt" />
|
<EmbeddedResource Include="samples\day1.txt"/>
|
||||||
<EmbeddedResource Include="samples\day7.txt" />
|
<EmbeddedResource Include="samples\day1part1.txt"/>
|
||||||
<EmbeddedResource Include="samples\day8part1.txt" />
|
<EmbeddedResource Include="samples\day2.txt"/>
|
||||||
<EmbeddedResource Include="samples\day8.txt" />
|
<EmbeddedResource Include="samples\day3.txt"/>
|
||||||
</ItemGroup>
|
<EmbeddedResource Include="samples\day4.txt"/>
|
||||||
|
<EmbeddedResource Include="samples\day5.txt"/>
|
||||||
|
<EmbeddedResource Include="samples\day6.txt"/>
|
||||||
|
<EmbeddedResource Include="samples\day7.txt"/>
|
||||||
|
<EmbeddedResource Include="samples\day8part1.txt"/>
|
||||||
|
<EmbeddedResource Include="samples\day8.txt"/>
|
||||||
|
<EmbeddedResource Include="samples\day9.txt"/>
|
||||||
|
<EmbeddedResource Include="samples\day10part1.txt" />
|
||||||
|
<EmbeddedResource Include="samples\day10.txt" />
|
||||||
|
<EmbeddedResource Include="samples\day11.txt" />
|
||||||
|
<EmbeddedResource Include="samples\day12.txt" />
|
||||||
|
<EmbeddedResource Include="samples\day13.txt" />
|
||||||
|
<EmbeddedResource Include="samples\day14.txt" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="FsUnit" Version="5.6.1" />
|
<PackageReference Include="FsUnit" Version="5.6.1"/>
|
||||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/>
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0"/>
|
||||||
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1"/>
|
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1"/>
|
||||||
<PackageReference Include="NUnit.Analyzers" Version="3.6.1"/>
|
<PackageReference Include="NUnit.Analyzers" Version="3.6.1"/>
|
||||||
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
|
<PackageReference Include="coverlet.collector" Version="6.0.0"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\AdventOfCode2023.FSharp.Lib\AdventOfCode2023.FSharp.Lib.fsproj" />
|
<ProjectReference Include="..\AdventOfCode2023.FSharp.Lib\AdventOfCode2023.FSharp.Lib.fsproj"/>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
104
AdventOfCode2023.FSharp/Test/TestDay10.fs
Normal file
104
AdventOfCode2023.FSharp/Test/TestDay10.fs
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
namespace AdventOfCode2023.Test
|
||||||
|
|
||||||
|
open AdventOfCode2023
|
||||||
|
open NUnit.Framework
|
||||||
|
open FsUnitTyped
|
||||||
|
open System.IO
|
||||||
|
|
||||||
|
[<TestFixture>]
|
||||||
|
module TestDay10 =
|
||||||
|
|
||||||
|
let part1Sample1 () =
|
||||||
|
""".....
|
||||||
|
.S-7.
|
||||||
|
.|.|.
|
||||||
|
.L-J.
|
||||||
|
.....
|
||||||
|
"""
|
||||||
|
|> Day10.part1
|
||||||
|
|> shouldEqual 4
|
||||||
|
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part1Sample () =
|
||||||
|
Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day10part1.txt"
|
||||||
|
|> Day10.part1
|
||||||
|
|> shouldEqual 8
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Sample1 () =
|
||||||
|
"""...........
|
||||||
|
.S-------7.
|
||||||
|
.|F-----7|.
|
||||||
|
.||.....||.
|
||||||
|
.||.....||.
|
||||||
|
.|L-7.F-J|.
|
||||||
|
.|..|.|..|.
|
||||||
|
.L--J.L--J.
|
||||||
|
...........
|
||||||
|
"""
|
||||||
|
|> Day10.part2
|
||||||
|
|> shouldEqual 4
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Sample2 () =
|
||||||
|
"""..........
|
||||||
|
.S------7.
|
||||||
|
.|F----7|.
|
||||||
|
.||....||.
|
||||||
|
.||....||.
|
||||||
|
.|L-7F-J|.
|
||||||
|
.|..||..|.
|
||||||
|
.L--JL--J.
|
||||||
|
..........
|
||||||
|
"""
|
||||||
|
|> Day10.part2
|
||||||
|
|> shouldEqual 4
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Sample3 () =
|
||||||
|
""".F----7F7F7F7F-7....
|
||||||
|
.|F--7||||||||FJ....
|
||||||
|
.||.FJ||||||||L7....
|
||||||
|
FJL7L7LJLJ||LJ.L-7..
|
||||||
|
L--J.L7...LJS7F-7L7.
|
||||||
|
....F-J..F7FJ|L7L7L7
|
||||||
|
....L7.F7||L7|.L7L7|
|
||||||
|
.....|FJLJ|FJ|F7|.LJ
|
||||||
|
....FJL-7.||.||||...
|
||||||
|
....L---J.LJ.LJLJ...
|
||||||
|
"""
|
||||||
|
|> Day10.part2
|
||||||
|
|> shouldEqual 8
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Sample () =
|
||||||
|
Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day10.txt"
|
||||||
|
|> Day10.part2
|
||||||
|
|> shouldEqual 10
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part1Actual () =
|
||||||
|
let s =
|
||||||
|
try
|
||||||
|
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day10.txt"))
|
||||||
|
with
|
||||||
|
| :? DirectoryNotFoundException
|
||||||
|
| :? FileNotFoundException ->
|
||||||
|
Assert.Inconclusive ()
|
||||||
|
failwith "unreachable"
|
||||||
|
|
||||||
|
Day10.part1 s |> shouldEqual 6842
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Actual () =
|
||||||
|
let s =
|
||||||
|
try
|
||||||
|
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day10.txt"))
|
||||||
|
with
|
||||||
|
| :? DirectoryNotFoundException
|
||||||
|
| :? FileNotFoundException ->
|
||||||
|
Assert.Inconclusive ()
|
||||||
|
failwith "unreachable"
|
||||||
|
|
||||||
|
Day10.part2 s |> shouldEqual 393
|
47
AdventOfCode2023.FSharp/Test/TestDay11.fs
Normal file
47
AdventOfCode2023.FSharp/Test/TestDay11.fs
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
namespace AdventOfCode2023.Test
|
||||||
|
|
||||||
|
open AdventOfCode2023
|
||||||
|
open NUnit.Framework
|
||||||
|
open FsUnitTyped
|
||||||
|
open System.IO
|
||||||
|
|
||||||
|
[<TestFixture>]
|
||||||
|
module TestDay11 =
|
||||||
|
|
||||||
|
let sample = Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day11.txt"
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part1Sample () =
|
||||||
|
sample |> Day11.part1 |> shouldEqual 374uL
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Sample () =
|
||||||
|
let data = sample |> Day11.parse
|
||||||
|
Day11.solve data 10uL |> shouldEqual 1030uL
|
||||||
|
Day11.solve data 100uL |> shouldEqual 8410uL
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part1Actual () =
|
||||||
|
let s =
|
||||||
|
try
|
||||||
|
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day11.txt"))
|
||||||
|
with
|
||||||
|
| :? DirectoryNotFoundException
|
||||||
|
| :? FileNotFoundException ->
|
||||||
|
Assert.Inconclusive ()
|
||||||
|
failwith "unreachable"
|
||||||
|
|
||||||
|
Day11.part1 s |> shouldEqual 9947476uL
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Actual () =
|
||||||
|
let s =
|
||||||
|
try
|
||||||
|
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day11.txt"))
|
||||||
|
with
|
||||||
|
| :? DirectoryNotFoundException
|
||||||
|
| :? FileNotFoundException ->
|
||||||
|
Assert.Inconclusive ()
|
||||||
|
failwith "unreachable"
|
||||||
|
|
||||||
|
Day11.part2 s |> shouldEqual 519939907614uL
|
45
AdventOfCode2023.FSharp/Test/TestDay12.fs
Normal file
45
AdventOfCode2023.FSharp/Test/TestDay12.fs
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
namespace AdventOfCode2023.Test
|
||||||
|
|
||||||
|
open AdventOfCode2023
|
||||||
|
open NUnit.Framework
|
||||||
|
open FsUnitTyped
|
||||||
|
open System.IO
|
||||||
|
|
||||||
|
[<TestFixture>]
|
||||||
|
module TestDay12 =
|
||||||
|
|
||||||
|
let sample = Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day12.txt"
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part1Sample () =
|
||||||
|
sample |> Day12.part1 |> shouldEqual 21uL
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Sample () =
|
||||||
|
sample |> Day12.part2 |> shouldEqual 525152uL
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part1Actual () =
|
||||||
|
let s =
|
||||||
|
try
|
||||||
|
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day12.txt"))
|
||||||
|
with
|
||||||
|
| :? DirectoryNotFoundException
|
||||||
|
| :? FileNotFoundException ->
|
||||||
|
Assert.Inconclusive ()
|
||||||
|
failwith "unreachable"
|
||||||
|
|
||||||
|
Day12.part1 s |> shouldEqual 7402uL
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Actual () =
|
||||||
|
let s =
|
||||||
|
try
|
||||||
|
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day12.txt"))
|
||||||
|
with
|
||||||
|
| :? DirectoryNotFoundException
|
||||||
|
| :? FileNotFoundException ->
|
||||||
|
Assert.Inconclusive ()
|
||||||
|
failwith "unreachable"
|
||||||
|
|
||||||
|
Day12.part2 s |> shouldEqual 3384337640277uL
|
68
AdventOfCode2023.FSharp/Test/TestDay13.fs
Normal file
68
AdventOfCode2023.FSharp/Test/TestDay13.fs
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
namespace AdventOfCode2023.Test
|
||||||
|
|
||||||
|
open System
|
||||||
|
|
||||||
|
open AdventOfCode2023
|
||||||
|
open NUnit.Framework
|
||||||
|
open FsUnitTyped
|
||||||
|
open System.IO
|
||||||
|
|
||||||
|
[<TestFixture>]
|
||||||
|
module TestDay13 =
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let ``rowToInt test`` () =
|
||||||
|
Day13.rowToInt ("#.##..##.".AsSpan ()) |> shouldEqual 358ul
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let ``colToInt test`` () =
|
||||||
|
let s =
|
||||||
|
"""#.##..##.
|
||||||
|
..#.##.#.
|
||||||
|
##......#
|
||||||
|
##......#
|
||||||
|
..#.##.#.
|
||||||
|
..##..##.
|
||||||
|
#.#.##.#.
|
||||||
|
"""
|
||||||
|
|
||||||
|
Day13.colToInt (s.AsSpan ()) 9 0
|
||||||
|
|> shouldEqual (List.sum [ 1 ; 8 ; 16 ; 64 ] |> uint32)
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
|
||||||
|
let sample = Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day13.txt"
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part1Sample () =
|
||||||
|
sample |> Day13.part1 |> shouldEqual 405ul
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Sample () =
|
||||||
|
sample |> Day13.part2 |> shouldEqual 400ul
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part1Actual () =
|
||||||
|
let s =
|
||||||
|
try
|
||||||
|
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day13.txt"))
|
||||||
|
with
|
||||||
|
| :? DirectoryNotFoundException
|
||||||
|
| :? FileNotFoundException ->
|
||||||
|
Assert.Inconclusive ()
|
||||||
|
failwith "unreachable"
|
||||||
|
|
||||||
|
Day13.part1 s |> shouldEqual 30158ul
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Actual () =
|
||||||
|
let s =
|
||||||
|
try
|
||||||
|
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day13.txt"))
|
||||||
|
with
|
||||||
|
| :? DirectoryNotFoundException
|
||||||
|
| :? FileNotFoundException ->
|
||||||
|
Assert.Inconclusive ()
|
||||||
|
failwith "unreachable"
|
||||||
|
|
||||||
|
Day13.part2 s |> shouldEqual 36474ul
|
48
AdventOfCode2023.FSharp/Test/TestDay14.fs
Normal file
48
AdventOfCode2023.FSharp/Test/TestDay14.fs
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
namespace AdventOfCode2023.Test
|
||||||
|
|
||||||
|
open System
|
||||||
|
|
||||||
|
open AdventOfCode2023
|
||||||
|
open NUnit.Framework
|
||||||
|
open FsUnitTyped
|
||||||
|
open System.IO
|
||||||
|
|
||||||
|
[<TestFixture>]
|
||||||
|
module TestDay14 =
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let sample = Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day14.txt"
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part1Sample () =
|
||||||
|
sample |> Day14.part1 |> shouldEqual 136ul
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Sample () =
|
||||||
|
sample |> Day14.part2 |> shouldEqual 64ul
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part1Actual () =
|
||||||
|
let s =
|
||||||
|
try
|
||||||
|
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day14.txt"))
|
||||||
|
with
|
||||||
|
| :? DirectoryNotFoundException
|
||||||
|
| :? FileNotFoundException ->
|
||||||
|
Assert.Inconclusive ()
|
||||||
|
failwith "unreachable"
|
||||||
|
|
||||||
|
Day14.part1 s |> shouldEqual 111339ul
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Actual () =
|
||||||
|
let s =
|
||||||
|
try
|
||||||
|
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day14.txt"))
|
||||||
|
with
|
||||||
|
| :? DirectoryNotFoundException
|
||||||
|
| :? FileNotFoundException ->
|
||||||
|
Assert.Inconclusive ()
|
||||||
|
failwith "unreachable"
|
||||||
|
|
||||||
|
Day14.part2 s |> shouldEqual 93736ul
|
@@ -1,6 +1,5 @@
|
|||||||
namespace AdventOfCode2023.Test
|
namespace AdventOfCode2023.Test
|
||||||
|
|
||||||
open System
|
|
||||||
open AdventOfCode2023
|
open AdventOfCode2023
|
||||||
open NUnit.Framework
|
open NUnit.Framework
|
||||||
open FsUnitTyped
|
open FsUnitTyped
|
||||||
|
@@ -1,6 +1,5 @@
|
|||||||
namespace AdventOfCode2023.Test
|
namespace AdventOfCode2023.Test
|
||||||
|
|
||||||
open System
|
|
||||||
open AdventOfCode2023
|
open AdventOfCode2023
|
||||||
open NUnit.Framework
|
open NUnit.Framework
|
||||||
open FsUnitTyped
|
open FsUnitTyped
|
||||||
|
@@ -1,6 +1,5 @@
|
|||||||
namespace AdventOfCode2023.Test
|
namespace AdventOfCode2023.Test
|
||||||
|
|
||||||
open System
|
|
||||||
open AdventOfCode2023
|
open AdventOfCode2023
|
||||||
open NUnit.Framework
|
open NUnit.Framework
|
||||||
open FsUnitTyped
|
open FsUnitTyped
|
||||||
|
44
AdventOfCode2023.FSharp/Test/TestDay9.fs
Normal file
44
AdventOfCode2023.FSharp/Test/TestDay9.fs
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
namespace AdventOfCode2023.Test
|
||||||
|
|
||||||
|
open AdventOfCode2023
|
||||||
|
open NUnit.Framework
|
||||||
|
open FsUnitTyped
|
||||||
|
open System.IO
|
||||||
|
|
||||||
|
[<TestFixture>]
|
||||||
|
module TestDay9 =
|
||||||
|
|
||||||
|
let sample = Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day9.txt"
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part1Sample () =
|
||||||
|
sample |> Day9.part1 |> shouldEqual 114L
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Sample () = sample |> Day9.part2 |> shouldEqual 2L
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part1Actual () =
|
||||||
|
let s =
|
||||||
|
try
|
||||||
|
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day9.txt"))
|
||||||
|
with
|
||||||
|
| :? DirectoryNotFoundException
|
||||||
|
| :? FileNotFoundException ->
|
||||||
|
Assert.Inconclusive ()
|
||||||
|
failwith "unreachable"
|
||||||
|
|
||||||
|
Day9.part1 s |> shouldEqual 1898776583L
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Actual () =
|
||||||
|
let s =
|
||||||
|
try
|
||||||
|
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day9.txt"))
|
||||||
|
with
|
||||||
|
| :? DirectoryNotFoundException
|
||||||
|
| :? FileNotFoundException ->
|
||||||
|
Assert.Inconclusive ()
|
||||||
|
failwith "unreachable"
|
||||||
|
|
||||||
|
Day9.part2 s |> shouldEqual 1100L
|
10
AdventOfCode2023.FSharp/Test/samples/day10.txt
Normal file
10
AdventOfCode2023.FSharp/Test/samples/day10.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
FF7FSF7F7F7F7F7F---7
|
||||||
|
L|LJ||||||||||||F--J
|
||||||
|
FL-7LJLJ||||||LJL-77
|
||||||
|
F--JF--7||LJLJ7F7FJ-
|
||||||
|
L---JF-JLJ.||-FJLJJ7
|
||||||
|
|F|F-JF---7F7-L7L|7|
|
||||||
|
|FFJF7L7F-JF7|JL---7
|
||||||
|
7-L-JL7||F7|L7F-7F7|
|
||||||
|
L.L7LFJ|||||FJL7||LJ
|
||||||
|
L7JLJL-JLJLJL--JLJ.L
|
5
AdventOfCode2023.FSharp/Test/samples/day10part1.txt
Normal file
5
AdventOfCode2023.FSharp/Test/samples/day10part1.txt
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
..F7.
|
||||||
|
.FJ|.
|
||||||
|
SJ.L7
|
||||||
|
|F--J
|
||||||
|
LJ...
|
10
AdventOfCode2023.FSharp/Test/samples/day11.txt
Normal file
10
AdventOfCode2023.FSharp/Test/samples/day11.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
...#......
|
||||||
|
.......#..
|
||||||
|
#.........
|
||||||
|
..........
|
||||||
|
......#...
|
||||||
|
.#........
|
||||||
|
.........#
|
||||||
|
..........
|
||||||
|
.......#..
|
||||||
|
#...#.....
|
6
AdventOfCode2023.FSharp/Test/samples/day12.txt
Normal file
6
AdventOfCode2023.FSharp/Test/samples/day12.txt
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
???.### 1,1,3
|
||||||
|
.??..??...?##. 1,1,3
|
||||||
|
?#?#?#?#?#?#?#? 1,3,1,6
|
||||||
|
????.#...#... 4,1,1
|
||||||
|
????.######..#####. 1,6,5
|
||||||
|
?###???????? 3,2,1
|
15
AdventOfCode2023.FSharp/Test/samples/day13.txt
Normal file
15
AdventOfCode2023.FSharp/Test/samples/day13.txt
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
#.##..##.
|
||||||
|
..#.##.#.
|
||||||
|
##......#
|
||||||
|
##......#
|
||||||
|
..#.##.#.
|
||||||
|
..##..##.
|
||||||
|
#.#.##.#.
|
||||||
|
|
||||||
|
#...##..#
|
||||||
|
#....#..#
|
||||||
|
..##..###
|
||||||
|
#####.##.
|
||||||
|
#####.##.
|
||||||
|
..##..###
|
||||||
|
#....#..#
|
10
AdventOfCode2023.FSharp/Test/samples/day14.txt
Normal file
10
AdventOfCode2023.FSharp/Test/samples/day14.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
O....#....
|
||||||
|
O.OO#....#
|
||||||
|
.....##...
|
||||||
|
OO.#O....O
|
||||||
|
.O.....O#.
|
||||||
|
O.#..O.#.#
|
||||||
|
..O..#O..O
|
||||||
|
.......O..
|
||||||
|
#....###..
|
||||||
|
#OO..#....
|
3
AdventOfCode2023.FSharp/Test/samples/day9.txt
Normal file
3
AdventOfCode2023.FSharp/Test/samples/day9.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
0 3 6 9 12 15
|
||||||
|
1 3 6 10 15 21
|
||||||
|
10 13 16 21 30 45
|
104
README.md
104
README.md
@@ -7,23 +7,109 @@
|
|||||||
Ahead-of-time compiled with `PublishAot`, M1 Max.
|
Ahead-of-time compiled with `PublishAot`, M1 Max.
|
||||||
The format is: "answer part1\ntime\nanswer part2\ntime\n...", with possible extra lines indicating how long it took to parse the input if I happen to have split that out.
|
The format is: "answer part1\ntime\nanswer part2\ntime\n...", with possible extra lines indicating how long it took to parse the input if I happen to have split that out.
|
||||||
|
|
||||||
After day 3:
|
BenchmarkDotNet:
|
||||||
|
|
||||||
```
|
```
|
||||||
|
| Day | IsPartOne | Mean | Error | StdDev |
|
||||||
|
|---- |---------- |----------:|---------:|---------:|
|
||||||
|
| 1 | True | 10.23 us | 0.036 us | 0.032 us |
|
||||||
|
| 1 | False | 17.93 us | 0.203 us | 0.180 us |
|
||||||
|
| 2 | True | 17.39 us | 0.080 us | 0.075 us |
|
||||||
|
| 2 | False | 25.42 us | 0.155 us | 0.145 us |
|
||||||
|
| 3 | True | 65.60 us | 0.393 us | 0.328 us |
|
||||||
|
| 3 | False | 145.52 us | 0.256 us | 0.200 us |
|
||||||
|
| 4 | True | 109.78 us | 0.236 us | 0.209 us |
|
||||||
|
| 4 | False | 110.34 us | 0.081 us | 0.063 us |
|
||||||
|
| 5 | True | 13.44 us | 0.045 us | 0.042 us |
|
||||||
|
| 5 | False | 61.70 us | 0.199 us | 0.177 us |
|
||||||
|
|
||||||
|
| Day | IsPartOne | Mean | Error | StdDev |
|
||||||
|
|---- |---------- |---------------:|------------:|------------:|
|
||||||
|
| 6 | True | 314.7 ns | 1.87 ns | 1.65 ns |
|
||||||
|
| 6 | False | 316.3 ns | 0.31 ns | 0.26 ns |
|
||||||
|
| 7 | True | 89,256.3 ns | 578.24 ns | 540.88 ns |
|
||||||
|
| 7 | False | 95,062.7 ns | 921.75 ns | 862.21 ns |
|
||||||
|
| 8 | True | 423,461.0 ns | 7,218.95 ns | 6,752.61 ns |
|
||||||
|
| 8 | False | 2,045,302.1 ns | 4,338.61 ns | 3,846.06 ns |
|
||||||
|
| 9 | True | 1,390,976.2 ns | 2,171.39 ns | 1,813.21 ns |
|
||||||
|
| 9 | False | 2,173,468.1 ns | 3,171.04 ns | 2,647.96 ns |
|
||||||
|
| 10 | True | 57,460.7 ns | 1,135.45 ns | 2,160.31 ns |
|
||||||
|
| 10 | False | 694,553.9 ns | 2,935.74 ns | 2,746.09 ns |
|
||||||
|
|
||||||
|
| Day | IsPartOne | Mean | Error | StdDev | Median |
|
||||||
|
|---- |---------- |----------:|----------:|----------:|----------:|
|
||||||
|
| 11 | True | 39.085 ms | 0.0355 ms | 0.0297 ms | 39.082 ms |
|
||||||
|
| 11 | False | 38.608 ms | 0.0270 ms | 0.0211 ms | 38.617 ms |
|
||||||
|
| 12 | True | 1.846 ms | 0.0044 ms | 0.0041 ms | 1.845 ms |
|
||||||
|
| 12 | False | 18.692 ms | 0.3676 ms | 0.3775 ms | 18.962 ms |
|
||||||
|
```
|
||||||
|
|
||||||
|
After day 12, a single run of the ahead-of-time compiled version:
|
||||||
|
|
||||||
|
```
|
||||||
|
=====Day 1=====
|
||||||
54304
|
54304
|
||||||
0.549458ms
|
3.418417ms
|
||||||
54418
|
54418
|
||||||
0.710375ms
|
0.317958ms
|
||||||
|
=====Day 2=====
|
||||||
2727
|
2727
|
||||||
0.119959ms
|
0.079917ms
|
||||||
56580
|
56580
|
||||||
0.155708ms
|
0.107292ms
|
||||||
0.1395ms parse
|
=====Day 3=====
|
||||||
|
0.140292ms parse
|
||||||
540131
|
540131
|
||||||
0.1395ms
|
0.140292ms
|
||||||
86879020
|
86879020
|
||||||
0.840791ms
|
0.664416ms
|
||||||
4.144166ms total
|
=====Day 4=====
|
||||||
|
27454
|
||||||
|
0.390541ms
|
||||||
|
6857330
|
||||||
|
0.360375ms
|
||||||
|
=====Day 5=====
|
||||||
|
806029445
|
||||||
|
0.161917ms
|
||||||
|
59370572
|
||||||
|
0.249708ms
|
||||||
|
=====Day 6=====
|
||||||
|
32076
|
||||||
|
0.002917ms
|
||||||
|
34278221
|
||||||
|
0.001667ms
|
||||||
|
=====Day 7=====
|
||||||
|
250058342
|
||||||
|
0.409792ms
|
||||||
|
250506580
|
||||||
|
0.431167ms
|
||||||
|
=====Day 8=====
|
||||||
|
19199
|
||||||
|
1.192792ms
|
||||||
|
13663968099527
|
||||||
|
5.276083ms
|
||||||
|
=====Day 9=====
|
||||||
|
1898776583
|
||||||
|
3.775667ms
|
||||||
|
1100
|
||||||
|
5.365875ms
|
||||||
|
=====Day 10=====
|
||||||
|
6842
|
||||||
|
0.201208ms
|
||||||
|
393
|
||||||
|
2.226042ms
|
||||||
|
=====Day 11=====
|
||||||
|
0.11225ms parse
|
||||||
|
9947476
|
||||||
|
48.423ms
|
||||||
|
519939907614
|
||||||
|
34.836125ms
|
||||||
|
=====Day 12=====
|
||||||
|
7402
|
||||||
|
4.704375ms
|
||||||
|
3384337640277
|
||||||
|
31.825583ms
|
||||||
|
151.644334ms total
|
||||||
```
|
```
|
||||||
|
|
||||||
# Building yourself
|
# Building yourself
|
||||||
|
Reference in New Issue
Block a user