Compare commits
7 Commits
dc0aa1ce30
...
main
Author | SHA1 | Date | |
---|---|---|---|
8f4d6a9062 | |||
46b4f2d715 | |||
16b801f267 | |||
7646fb71c3 | |||
f2a2e630d6 | |||
735a3dbdde | |||
9ec99c8ee9 |
@@ -3,7 +3,7 @@
|
|||||||
"isRoot": true,
|
"isRoot": true,
|
||||||
"tools": {
|
"tools": {
|
||||||
"fantomas": {
|
"fantomas": {
|
||||||
"version": "6.2.3",
|
"version": "7.0.3",
|
||||||
"commands": [
|
"commands": [
|
||||||
"fantomas"
|
"fantomas"
|
||||||
]
|
]
|
||||||
|
@@ -2,7 +2,6 @@ root=true
|
|||||||
|
|
||||||
[*]
|
[*]
|
||||||
charset=utf-8
|
charset=utf-8
|
||||||
end_of_line=crlf
|
|
||||||
trim_trailing_whitespace=true
|
trim_trailing_whitespace=true
|
||||||
insert_final_newline=true
|
insert_final_newline=true
|
||||||
indent_style=space
|
indent_style=space
|
||||||
|
2
.gitignore
vendored
2
.gitignore
vendored
@@ -8,5 +8,7 @@ _ReSharper.Caches/
|
|||||||
.DS_Store
|
.DS_Store
|
||||||
result
|
result
|
||||||
.profile*
|
.profile*
|
||||||
|
.direnv/
|
||||||
|
|
||||||
inputs/
|
inputs/
|
||||||
|
AdventOfCode2023.FSharp/Test/TestResults/
|
||||||
|
@@ -14,6 +14,6 @@ module Inputs =
|
|||||||
if isNull dir then
|
if isNull dir then
|
||||||
failwith "reached root of filesystem without finding inputs dir"
|
failwith "reached root of filesystem without finding inputs dir"
|
||||||
|
|
||||||
Array.init 13 (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]
|
let inline day (i : int) = days.[i - 1]
|
||||||
|
@@ -44,7 +44,24 @@ module Benchmarks =
|
|||||||
[<GlobalSetup>]
|
[<GlobalSetup>]
|
||||||
member _.Setup () = Run.shouldWrite <- false
|
member _.Setup () = Run.shouldWrite <- false
|
||||||
|
|
||||||
[<Params(11, 12, 13)>]
|
[<Params(11, 12, 13, 14, 15)>]
|
||||||
|
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 Benchmark16To20 () =
|
||||||
|
[<GlobalSetup>]
|
||||||
|
member _.Setup () = Run.shouldWrite <- false
|
||||||
|
|
||||||
|
[<Params(16)>]
|
||||||
member val Day = 0 with get, set
|
member val Day = 0 with get, set
|
||||||
|
|
||||||
[<Params(false, true)>]
|
[<Params(false, true)>]
|
||||||
|
@@ -185,6 +185,42 @@ module Run =
|
|||||||
if shouldWrite then
|
if shouldWrite then
|
||||||
Console.WriteLine output
|
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 day15 (partTwo : bool) (input : string) =
|
||||||
|
if not partTwo then
|
||||||
|
let output = Day15.part1 input
|
||||||
|
|
||||||
|
if shouldWrite then
|
||||||
|
Console.WriteLine output
|
||||||
|
else
|
||||||
|
let output = Day15.part2 input
|
||||||
|
|
||||||
|
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 =
|
let allRuns =
|
||||||
[|
|
[|
|
||||||
day1
|
day1
|
||||||
@@ -200,4 +236,7 @@ module Run =
|
|||||||
day11
|
day11
|
||||||
day12
|
day12
|
||||||
day13
|
day13
|
||||||
|
day14
|
||||||
|
day15
|
||||||
|
day16
|
||||||
|]
|
|]
|
||||||
|
@@ -1,8 +1,9 @@
|
|||||||
<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>
|
||||||
|
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@@ -11,6 +12,9 @@
|
|||||||
<Compile Include="EfficientString.fs"/>
|
<Compile Include="EfficientString.fs"/>
|
||||||
<Compile Include="Arithmetic.fs"/>
|
<Compile Include="Arithmetic.fs"/>
|
||||||
<Compile Include="Rational.fs"/>
|
<Compile Include="Rational.fs"/>
|
||||||
|
<Compile Include="IntervalSet.fs" />
|
||||||
|
<Compile Include="Direction.fs" />
|
||||||
|
<Compile Include="List.fs" />
|
||||||
<Compile Include="Day1.fs"/>
|
<Compile Include="Day1.fs"/>
|
||||||
<Compile Include="Day2.fs"/>
|
<Compile Include="Day2.fs"/>
|
||||||
<Compile Include="Day3.fs"/>
|
<Compile Include="Day3.fs"/>
|
||||||
@@ -24,6 +28,10 @@
|
|||||||
<Compile Include="Day11.fs" />
|
<Compile Include="Day11.fs" />
|
||||||
<Compile Include="Day12.fs" />
|
<Compile Include="Day12.fs" />
|
||||||
<Compile Include="Day13.fs" />
|
<Compile Include="Day13.fs" />
|
||||||
|
<Compile Include="Day14.fs" />
|
||||||
|
<Compile Include="Day15.fs" />
|
||||||
|
<Compile Include="Day16.fs" />
|
||||||
|
<Compile Include="Day19.fs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
@@ -5,6 +5,7 @@
|
|||||||
#nowarn "9"
|
#nowarn "9"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
open System
|
||||||
open Microsoft.FSharp.NativeInterop
|
open Microsoft.FSharp.NativeInterop
|
||||||
|
|
||||||
[<Struct>]
|
[<Struct>]
|
||||||
@@ -100,3 +101,76 @@ module Arr2D =
|
|||||||
#else
|
#else
|
||||||
NativePtr.initBlock a.Elements 0uy (uint32 sizeof<'a> * uint32 a.Length)
|
NativePtr.initBlock a.Elements 0uy (uint32 sizeof<'a> * uint32 a.Length)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/// Pass in a buffer of memory which we will use entirely for our own purposes (and may resize)
|
||||||
|
/// to maintain state.
|
||||||
|
/// `empty` is the value in empty cells; we will fill them with `fillWith`.
|
||||||
|
let floodFill
|
||||||
|
(stackBuf : ResizeArray<int>)
|
||||||
|
(s : Arr2D<'a>)
|
||||||
|
(empty : 'a)
|
||||||
|
(fillWith : 'a)
|
||||||
|
(currX : int)
|
||||||
|
(currY : int)
|
||||||
|
: unit
|
||||||
|
=
|
||||||
|
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 get s (currX - 1) currY = empty then
|
||||||
|
set s (currX - 1) currY fillWith
|
||||||
|
stackBuf.Add (currX - 1)
|
||||||
|
stackBuf.Add currY
|
||||||
|
|
||||||
|
if currX < s.Width - 1 then
|
||||||
|
if get s (currX + 1) currY = empty then
|
||||||
|
set s (currX + 1) currY fillWith
|
||||||
|
stackBuf.Add (currX + 1)
|
||||||
|
stackBuf.Add currY
|
||||||
|
|
||||||
|
if currY > 0 then
|
||||||
|
if get s currX (currY - 1) = empty then
|
||||||
|
set s currX (currY - 1) fillWith
|
||||||
|
stackBuf.Add currX
|
||||||
|
stackBuf.Add (currY - 1)
|
||||||
|
|
||||||
|
if currY < s.Height - 1 then
|
||||||
|
if get s currX (currY + 1) = empty then
|
||||||
|
set s currX (currY + 1) fillWith
|
||||||
|
stackBuf.Add currX
|
||||||
|
stackBuf.Add (currY + 1)
|
||||||
|
|
||||||
|
/// SIMD go brr
|
||||||
|
let inline count< ^a when 'a : equality and 'a : unmanaged and 'a :> IEquatable<'a>>
|
||||||
|
(arr : Arr2D<'a>)
|
||||||
|
(x : 'a)
|
||||||
|
: int
|
||||||
|
=
|
||||||
|
let span =
|
||||||
|
#if DEBUG
|
||||||
|
arr.Elements.AsSpan ()
|
||||||
|
#else
|
||||||
|
ReadOnlySpan<'a> (NativePtr.toVoidPtr arr.Elements, arr.Length)
|
||||||
|
#endif
|
||||||
|
MemoryExtensions.Count (span, x)
|
||||||
|
|
||||||
|
let print (arr : Arr2D<byte>) =
|
||||||
|
for row = 0 to arr.Height - 1 do
|
||||||
|
for col = 0 to arr.Width - 1 do
|
||||||
|
match get arr col row with
|
||||||
|
| 1uy -> printf "#"
|
||||||
|
| 0uy -> printf "."
|
||||||
|
| 2uy -> printf "O"
|
||||||
|
| _ -> failwith "bad"
|
||||||
|
|
||||||
|
printfn ""
|
||||||
|
|
||||||
|
printfn ""
|
||||||
|
@@ -123,41 +123,6 @@ module Day10 =
|
|||||||
|
|
||||||
distance
|
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>) =
|
let print (s : Arr2D<byte>) =
|
||||||
for y = 0 to s.Height - 1 do
|
for y = 0 to s.Height - 1 do
|
||||||
for x = 0 to s.Width - 1 do
|
for x = 0 to s.Width - 1 do
|
||||||
@@ -310,12 +275,12 @@ module Day10 =
|
|||||||
let stackBuf = ResizeArray ()
|
let stackBuf = ResizeArray ()
|
||||||
|
|
||||||
for line = 0 to system.Height - 1 do
|
for line = 0 to system.Height - 1 do
|
||||||
floodFill stackBuf system 0 line
|
Arr2D.floodFill stackBuf system 0uy 2uy 0 line
|
||||||
floodFill stackBuf system (system.Width - 1) line
|
Arr2D.floodFill stackBuf system 0uy 2uy (system.Width - 1) line
|
||||||
|
|
||||||
for col = 0 to system.Width - 1 do
|
for col = 0 to system.Width - 1 do
|
||||||
floodFill stackBuf system col 0
|
Arr2D.floodFill stackBuf system 0uy 2uy col 0
|
||||||
floodFill stackBuf system col (system.Height - 1)
|
Arr2D.floodFill stackBuf system 0uy 2uy col (system.Height - 1)
|
||||||
|
|
||||||
let mutable answer = 0
|
let mutable answer = 0
|
||||||
|
|
||||||
|
279
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day14.fs
Normal file
279
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day14.fs
Normal file
@@ -0,0 +1,279 @@
|
|||||||
|
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 pos < arr.Height && 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 pos >= 0 && 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 pos >= 0 && 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 pos < arr.Width && 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 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]
|
114
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day15.fs
Normal file
114
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day15.fs
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
namespace AdventOfCode2023
|
||||||
|
|
||||||
|
open System
|
||||||
|
open System.Globalization
|
||||||
|
|
||||||
|
[<RequireQualifiedAccess>]
|
||||||
|
module Day15 =
|
||||||
|
|
||||||
|
let hash (s : ReadOnlySpan<char>) : int =
|
||||||
|
let mutable v = 0
|
||||||
|
|
||||||
|
for c in s do
|
||||||
|
v <- v + int (byte c)
|
||||||
|
v <- (17 * v) % 256
|
||||||
|
|
||||||
|
v
|
||||||
|
|
||||||
|
let part1 (s : string) =
|
||||||
|
let s = s.AsSpan().TrimEnd ()
|
||||||
|
use chunks = StringSplitEnumerator.make' ',' s
|
||||||
|
let mutable answer = 0
|
||||||
|
|
||||||
|
for chunk in chunks do
|
||||||
|
answer <- answer + hash chunk
|
||||||
|
|
||||||
|
answer
|
||||||
|
|
||||||
|
let inline removeFirst<'a> ([<InlineIfLambda>] toRemove : 'a -> bool) (arr : ResizeArray<'a>) : unit =
|
||||||
|
let mutable i = 0
|
||||||
|
|
||||||
|
while i < arr.Count do
|
||||||
|
if toRemove arr.[i] then
|
||||||
|
for j = i to arr.Count - 2 do
|
||||||
|
arr.[j] <- arr.[j + 1]
|
||||||
|
|
||||||
|
arr.RemoveAt (arr.Count - 1)
|
||||||
|
i <- arr.Count
|
||||||
|
|
||||||
|
i <- i + 1
|
||||||
|
|
||||||
|
let inline replace
|
||||||
|
([<InlineIfLambda>] withKey : 'a -> 'key)
|
||||||
|
(key : 'key)
|
||||||
|
(value : 'a)
|
||||||
|
(arr : ResizeArray<'a>)
|
||||||
|
: unit
|
||||||
|
=
|
||||||
|
let mutable i = 0
|
||||||
|
|
||||||
|
while i < arr.Count do
|
||||||
|
if withKey arr.[i] = key then
|
||||||
|
arr.[i] <- value
|
||||||
|
i <- arr.Count
|
||||||
|
|
||||||
|
i <- i + 1
|
||||||
|
|
||||||
|
if i < arr.Count + 1 then
|
||||||
|
// no replacement was made
|
||||||
|
arr.Add value
|
||||||
|
|
||||||
|
let inline getLength (labelAndLength : uint64) : uint32 =
|
||||||
|
(labelAndLength % uint64 UInt32.MaxValue) |> uint32
|
||||||
|
|
||||||
|
let inline getLabel (labelAndLength : uint64) : uint32 =
|
||||||
|
(labelAndLength / uint64 UInt32.MaxValue) |> uint32
|
||||||
|
|
||||||
|
let inline focusingPower (boxNumber : uint32) (arr : ResizeArray<_>) =
|
||||||
|
let mutable answer = 0ul
|
||||||
|
|
||||||
|
for i = 0 to arr.Count - 1 do
|
||||||
|
answer <- answer + (boxNumber + 1ul) * (uint32 i + 1ul) * getLength arr.[i]
|
||||||
|
|
||||||
|
answer
|
||||||
|
|
||||||
|
let inline toUint32 (s : ReadOnlySpan<char>) : uint32 =
|
||||||
|
let mutable answer = 0ul
|
||||||
|
|
||||||
|
for c in s do
|
||||||
|
answer <- answer * 26ul + uint32 (byte c - byte 'a')
|
||||||
|
|
||||||
|
answer
|
||||||
|
|
||||||
|
let inline pack (label : uint32) (focalLength : uint32) : uint64 =
|
||||||
|
uint64 label * uint64 UInt32.MaxValue + uint64 focalLength
|
||||||
|
|
||||||
|
let part2 (s : string) =
|
||||||
|
let s = s.AsSpan().TrimEnd ()
|
||||||
|
use chunks = StringSplitEnumerator.make' ',' s
|
||||||
|
// The max length of a label turns out to be 6, which means we need 26^6 < 2^32 entries.
|
||||||
|
// So we'll use a uint32 instead of our string, to save hopping around memory.
|
||||||
|
// We'll also pack the focal length into the elements, to save tupling.
|
||||||
|
let lenses = Array.init 256 (fun _ -> ResizeArray<uint64> ())
|
||||||
|
|
||||||
|
for chunk in chunks do
|
||||||
|
if chunk.[chunk.Length - 1] = '-' then
|
||||||
|
let label = chunk.Slice (0, chunk.Length - 1)
|
||||||
|
let labelShrunk = toUint32 label
|
||||||
|
removeFirst (fun labelAndLength -> getLabel labelAndLength = labelShrunk) lenses.[hash label]
|
||||||
|
else
|
||||||
|
let equalsPos = chunk.IndexOf '='
|
||||||
|
|
||||||
|
let focalLength =
|
||||||
|
UInt32.Parse (chunk.Slice (equalsPos + 1), NumberStyles.None, CultureInfo.InvariantCulture)
|
||||||
|
|
||||||
|
let label = chunk.Slice (0, equalsPos)
|
||||||
|
let labelShrunk = toUint32 label
|
||||||
|
replace getLabel labelShrunk (pack labelShrunk focalLength) lenses.[hash label]
|
||||||
|
|
||||||
|
let mutable answer = 0ul
|
||||||
|
|
||||||
|
for i = 0 to 255 do
|
||||||
|
answer <- answer + focusingPower (uint32 i) lenses.[i]
|
||||||
|
|
||||||
|
answer
|
283
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day16.fs
Normal file
283
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day16.fs
Normal file
@@ -0,0 +1,283 @@
|
|||||||
|
namespace AdventOfCode2023
|
||||||
|
|
||||||
|
#if DEBUG
|
||||||
|
#else
|
||||||
|
#nowarn "9"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
open System
|
||||||
|
|
||||||
|
[<RequireQualifiedAccess>]
|
||||||
|
module Day16 =
|
||||||
|
|
||||||
|
let inline storeDirectionAndPos (numCols : int) (col : int) (row : int) (direction : Direction) : uint16 =
|
||||||
|
4us * uint16 (col + numCols * row) + Direction.toUInt 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
|
357
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day19.fs
Normal file
357
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day19.fs
Normal file
@@ -0,0 +1,357 @@
|
|||||||
|
namespace AdventOfCode2023
|
||||||
|
|
||||||
|
open System
|
||||||
|
open System.Collections.Generic
|
||||||
|
open System.Globalization
|
||||||
|
|
||||||
|
[<RequireQualifiedAccess>]
|
||||||
|
module Day19 =
|
||||||
|
|
||||||
|
type Component =
|
||||||
|
| X = 0
|
||||||
|
| M = 1
|
||||||
|
| A = 2
|
||||||
|
| S = 3
|
||||||
|
|
||||||
|
[<RequireQualifiedAccess>]
|
||||||
|
module Component =
|
||||||
|
let ofChar (c : char) : Component =
|
||||||
|
match c with
|
||||||
|
| 'x' -> Component.X
|
||||||
|
| 'm' -> Component.M
|
||||||
|
| 'a' -> Component.A
|
||||||
|
| 's' -> Component.S
|
||||||
|
| c -> failwith $"bad component: %c{c}"
|
||||||
|
|
||||||
|
type Dest =
|
||||||
|
| Register of string
|
||||||
|
| Accept
|
||||||
|
| Reject
|
||||||
|
|
||||||
|
static member OfString (s : EfficientString) : Dest =
|
||||||
|
if s.Length = 1 && s.[0] = 'A' then Dest.Accept
|
||||||
|
elif s.Length = 1 && s.[0] = 'R' then Dest.Reject
|
||||||
|
else Dest.Register (s.ToString ())
|
||||||
|
|
||||||
|
type Rule =
|
||||||
|
{
|
||||||
|
Component : Component
|
||||||
|
IsLess : bool
|
||||||
|
Operand : int
|
||||||
|
Target : Dest
|
||||||
|
}
|
||||||
|
|
||||||
|
static member OfString (s : EfficientString) : Choice<Rule, Dest> =
|
||||||
|
match s.IndexOf ':' with
|
||||||
|
| -1 -> Choice2Of2 (Dest.OfString (s.Slice (0, s.Length)))
|
||||||
|
| colon ->
|
||||||
|
|
||||||
|
let dest = Dest.OfString (s.Slice (colon + 1))
|
||||||
|
let comp = Component.ofChar s.[0]
|
||||||
|
|
||||||
|
let isLess =
|
||||||
|
match s.[1] with
|
||||||
|
| '>' -> false
|
||||||
|
| '<' -> true
|
||||||
|
| c -> failwith $"Bad comparison: %c{c}"
|
||||||
|
|
||||||
|
let operand =
|
||||||
|
Int32.Parse (s.Slice (2, colon - 2), NumberStyles.None, NumberFormatInfo.InvariantInfo)
|
||||||
|
|
||||||
|
{
|
||||||
|
Component = comp
|
||||||
|
IsLess = isLess
|
||||||
|
Target = dest
|
||||||
|
Operand = operand
|
||||||
|
}
|
||||||
|
|> Choice1Of2
|
||||||
|
|
||||||
|
let inline matches (rule : Rule) x m a s =
|
||||||
|
match rule.Component with
|
||||||
|
| Component.A ->
|
||||||
|
if (rule.IsLess && a < rule.Operand) || (not rule.IsLess && a > rule.Operand) then
|
||||||
|
Some rule.Target
|
||||||
|
else
|
||||||
|
None
|
||||||
|
| Component.X ->
|
||||||
|
if (rule.IsLess && x < rule.Operand) || (not rule.IsLess && x > rule.Operand) then
|
||||||
|
Some rule.Target
|
||||||
|
else
|
||||||
|
None
|
||||||
|
| Component.M ->
|
||||||
|
if (rule.IsLess && m < rule.Operand) || (not rule.IsLess && m > rule.Operand) then
|
||||||
|
Some rule.Target
|
||||||
|
else
|
||||||
|
None
|
||||||
|
| Component.S ->
|
||||||
|
if (rule.IsLess && s < rule.Operand) || (not rule.IsLess && s > rule.Operand) then
|
||||||
|
Some rule.Target
|
||||||
|
else
|
||||||
|
None
|
||||||
|
| _ -> failwith "bad component"
|
||||||
|
|
||||||
|
let rec computePart1 (components : Dictionary<string, Rule ResizeArray * Dest>) x m a s (reg : string) =
|
||||||
|
match components.TryGetValue reg with
|
||||||
|
| false, _ -> failwith $"no rule matched: %s{reg}"
|
||||||
|
| true, (rules, dest) ->
|
||||||
|
let mutable result = ValueNone
|
||||||
|
let mutable i = 0
|
||||||
|
|
||||||
|
while result.IsNone do
|
||||||
|
if i = rules.Count then
|
||||||
|
result <- ValueSome dest
|
||||||
|
else
|
||||||
|
|
||||||
|
match matches rules.[i] x m a s with
|
||||||
|
| Some dest -> result <- ValueSome dest
|
||||||
|
| None -> i <- i + 1
|
||||||
|
|
||||||
|
match result.Value with
|
||||||
|
| Register reg -> computePart1 components x m a s reg
|
||||||
|
| Accept -> true
|
||||||
|
| Reject -> false
|
||||||
|
|
||||||
|
let readWorkflows (rows : byref<StringSplitEnumerator>) =
|
||||||
|
let workflows = Dictionary<string, Rule ResizeArray * Dest> ()
|
||||||
|
|
||||||
|
while rows.MoveNext () && not rows.Current.IsEmpty do
|
||||||
|
let brace = rows.Current.IndexOf '{'
|
||||||
|
let name = rows.Current.Slice(0, brace).ToString ()
|
||||||
|
let rules = ResizeArray ()
|
||||||
|
|
||||||
|
for rule in StringSplitEnumerator.make' ',' (rows.Current.Slice(brace + 1).TrimEnd '}') do
|
||||||
|
match Rule.OfString rule with
|
||||||
|
| Choice1Of2 rule -> rules.Add rule
|
||||||
|
| Choice2Of2 dest -> workflows.[name] <- (rules, dest)
|
||||||
|
|
||||||
|
workflows
|
||||||
|
|
||||||
|
let part1 (workflows : _) (rows : byref<StringSplitEnumerator>) =
|
||||||
|
let mutable answer = 0
|
||||||
|
|
||||||
|
for row in rows do
|
||||||
|
if not row.IsEmpty then
|
||||||
|
let mutable x = 0
|
||||||
|
let mutable m = 0
|
||||||
|
let mutable a = 0
|
||||||
|
let mutable s = 0
|
||||||
|
|
||||||
|
for comp in StringSplitEnumerator.make' ',' (row.Slice (1, row.Length - 2)) do
|
||||||
|
let number =
|
||||||
|
Int32.Parse (comp.Slice 2, NumberStyles.None, NumberFormatInfo.InvariantInfo)
|
||||||
|
|
||||||
|
match comp.[0] with
|
||||||
|
| 'x' -> x <- number
|
||||||
|
| 'm' -> m <- number
|
||||||
|
| 'a' -> a <- number
|
||||||
|
| 's' -> s <- number
|
||||||
|
| c -> failwith $"Bad char: %c{c}"
|
||||||
|
|
||||||
|
if computePart1 workflows x m a s "in" then
|
||||||
|
answer <- answer + x + m + a + s
|
||||||
|
|
||||||
|
answer
|
||||||
|
|
||||||
|
type AcceptanceCriterion =
|
||||||
|
| True
|
||||||
|
| False
|
||||||
|
| Base of Component * low : int * high : int
|
||||||
|
| And of AcceptanceCriterion * AcceptanceCriterion
|
||||||
|
| Or of AcceptanceCriterion * AcceptanceCriterion
|
||||||
|
|
||||||
|
let rec acAnd a b =
|
||||||
|
match a, b with
|
||||||
|
| AcceptanceCriterion.Or (a1, a2), _ -> AcceptanceCriterion.Or (acAnd a1 b, acAnd a2 b)
|
||||||
|
| AcceptanceCriterion.True, _ -> b
|
||||||
|
| AcceptanceCriterion.False, _ -> False
|
||||||
|
| _, AcceptanceCriterion.Or (b1, b2) -> AcceptanceCriterion.Or (acAnd a b1, acAnd a b2)
|
||||||
|
| _, AcceptanceCriterion.True -> a
|
||||||
|
| _, AcceptanceCriterion.False -> False
|
||||||
|
| _, _ -> AcceptanceCriterion.And (a, b)
|
||||||
|
|
||||||
|
let inline acOr a b =
|
||||||
|
match a, b with
|
||||||
|
| AcceptanceCriterion.False, _ -> b
|
||||||
|
| AcceptanceCriterion.True, _ -> AcceptanceCriterion.True
|
||||||
|
| _, AcceptanceCriterion.False -> a
|
||||||
|
| _, AcceptanceCriterion.True -> AcceptanceCriterion.True
|
||||||
|
| _, _ -> AcceptanceCriterion.Or (a, b)
|
||||||
|
|
||||||
|
/// "low > high" means "empty interval"
|
||||||
|
[<Struct>]
|
||||||
|
type Interval =
|
||||||
|
{
|
||||||
|
Low : int
|
||||||
|
High : int
|
||||||
|
}
|
||||||
|
|
||||||
|
static member Empty =
|
||||||
|
{
|
||||||
|
Low = 1
|
||||||
|
High = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
[<RequireQualifiedAccess>]
|
||||||
|
module Interval =
|
||||||
|
let size (i : Interval) =
|
||||||
|
if i.Low > i.High then 0 else i.High - i.Low + 1
|
||||||
|
|
||||||
|
let intersect (i1 : Interval) (i2 : Interval) =
|
||||||
|
if i1.Low > i1.High || i2.Low > i2.High then
|
||||||
|
i1
|
||||||
|
else if i1.High < i2.Low then
|
||||||
|
Interval.Empty
|
||||||
|
elif i2.High < i1.Low then
|
||||||
|
Interval.Empty
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Low = max i1.Low i2.Low
|
||||||
|
High = min i1.High i2.High
|
||||||
|
}
|
||||||
|
|
||||||
|
type Conjunction =
|
||||||
|
{
|
||||||
|
X : Interval
|
||||||
|
M : Interval
|
||||||
|
A : Interval
|
||||||
|
S : Interval
|
||||||
|
}
|
||||||
|
|
||||||
|
static member All =
|
||||||
|
{
|
||||||
|
X =
|
||||||
|
{
|
||||||
|
Low = 1
|
||||||
|
High = 4000
|
||||||
|
}
|
||||||
|
M =
|
||||||
|
{
|
||||||
|
Low = 1
|
||||||
|
High = 4000
|
||||||
|
}
|
||||||
|
A =
|
||||||
|
{
|
||||||
|
Low = 1
|
||||||
|
High = 4000
|
||||||
|
}
|
||||||
|
S =
|
||||||
|
{
|
||||||
|
Low = 1
|
||||||
|
High = 4000
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[<RequireQualifiedAccess>]
|
||||||
|
module Conjunction =
|
||||||
|
let conjAnd (c1 : Conjunction) (c2 : Conjunction) =
|
||||||
|
{
|
||||||
|
X = Interval.intersect c1.X c2.X
|
||||||
|
M = Interval.intersect c1.M c2.M
|
||||||
|
A = Interval.intersect c1.A c2.A
|
||||||
|
S = Interval.intersect c1.S c2.S
|
||||||
|
}
|
||||||
|
|
||||||
|
/// We rely on the intervals being disjoint by construction.
|
||||||
|
let size (x : Conjunction) : uint64 =
|
||||||
|
uint64 (Interval.size x.X)
|
||||||
|
* uint64 (Interval.size x.M)
|
||||||
|
* uint64 (Interval.size x.A)
|
||||||
|
* uint64 (Interval.size x.S)
|
||||||
|
|
||||||
|
type UnionOfConjunctions = Conjunction list
|
||||||
|
|
||||||
|
let rec toUnion (ac : AcceptanceCriterion) : UnionOfConjunctions =
|
||||||
|
match ac with
|
||||||
|
| Base (comp, low, high) ->
|
||||||
|
match comp with
|
||||||
|
| Component.A ->
|
||||||
|
{ Conjunction.All with
|
||||||
|
A =
|
||||||
|
{
|
||||||
|
Low = low
|
||||||
|
High = high
|
||||||
|
}
|
||||||
|
}
|
||||||
|
| Component.M ->
|
||||||
|
{ Conjunction.All with
|
||||||
|
M =
|
||||||
|
{
|
||||||
|
Low = low
|
||||||
|
High = high
|
||||||
|
}
|
||||||
|
}
|
||||||
|
| Component.S ->
|
||||||
|
{ Conjunction.All with
|
||||||
|
S =
|
||||||
|
{
|
||||||
|
Low = low
|
||||||
|
High = high
|
||||||
|
}
|
||||||
|
}
|
||||||
|
| Component.X ->
|
||||||
|
{ Conjunction.All with
|
||||||
|
X =
|
||||||
|
{
|
||||||
|
Low = low
|
||||||
|
High = high
|
||||||
|
}
|
||||||
|
}
|
||||||
|
| _ -> failwith "bad"
|
||||||
|
|> List.singleton
|
||||||
|
| And (a, b) ->
|
||||||
|
[
|
||||||
|
Conjunction.conjAnd (List.exactlyOne (toUnion a)) (List.exactlyOne (toUnion b))
|
||||||
|
]
|
||||||
|
| Or (a, b) -> toUnion a @ toUnion b
|
||||||
|
| True -> failwith "already stripped out"
|
||||||
|
| False -> failwith "already stripped out"
|
||||||
|
|
||||||
|
let rec acceptance
|
||||||
|
(store : Dictionary<string, AcceptanceCriterion>)
|
||||||
|
(workflows : Dictionary<string, ResizeArray<Rule> * Dest>)
|
||||||
|
(key : string)
|
||||||
|
: AcceptanceCriterion
|
||||||
|
=
|
||||||
|
match store.TryGetValue key with
|
||||||
|
| true, v -> v
|
||||||
|
| false, _ ->
|
||||||
|
|
||||||
|
let rules, final = workflows.[key]
|
||||||
|
|
||||||
|
let mutable result =
|
||||||
|
match final with
|
||||||
|
| Register s -> acceptance store workflows s
|
||||||
|
| Accept -> AcceptanceCriterion.True
|
||||||
|
| Reject -> AcceptanceCriterion.False
|
||||||
|
|
||||||
|
for i = rules.Count - 1 downto 0 do
|
||||||
|
let rule = rules.[i]
|
||||||
|
|
||||||
|
let cond =
|
||||||
|
if rule.IsLess then
|
||||||
|
AcceptanceCriterion.Base (rule.Component, 1, rule.Operand - 1)
|
||||||
|
else
|
||||||
|
AcceptanceCriterion.Base (rule.Component, rule.Operand + 1, 4000)
|
||||||
|
|
||||||
|
let negCond =
|
||||||
|
if rule.IsLess then
|
||||||
|
AcceptanceCriterion.Base (rule.Component, rule.Operand, 4000)
|
||||||
|
else
|
||||||
|
AcceptanceCriterion.Base (rule.Component, 1, rule.Operand)
|
||||||
|
|
||||||
|
result <-
|
||||||
|
match rule.Target with
|
||||||
|
| Register target -> acOr (acAnd cond (acceptance store workflows target)) (acAnd negCond result)
|
||||||
|
| Accept -> acOr cond (acAnd negCond result)
|
||||||
|
| Reject -> acAnd negCond result
|
||||||
|
|
||||||
|
store.[key] <- result
|
||||||
|
result
|
||||||
|
|
||||||
|
let part2 (workflows : _) (rows : byref<StringSplitEnumerator>) : uint64 =
|
||||||
|
let acceptanceRanges = Dictionary<string, AcceptanceCriterion> ()
|
||||||
|
|
||||||
|
let a = acceptance acceptanceRanges workflows "in"
|
||||||
|
let union = toUnion a
|
||||||
|
|
||||||
|
union |> List.sumBy Conjunction.size
|
@@ -0,0 +1,24 @@
|
|||||||
|
namespace AdventOfCode2023
|
||||||
|
|
||||||
|
type Direction =
|
||||||
|
| Left = 0
|
||||||
|
| Right = 1
|
||||||
|
| Up = 2
|
||||||
|
| Down = 3
|
||||||
|
|
||||||
|
module Direction =
|
||||||
|
let inline toUInt (d : Direction) =
|
||||||
|
match d with
|
||||||
|
| Direction.Left -> 0us
|
||||||
|
| Direction.Right -> 1us
|
||||||
|
| Direction.Up -> 2us
|
||||||
|
| Direction.Down -> 3us
|
||||||
|
| _ -> failwith "Bad"
|
||||||
|
|
||||||
|
let inline ofChar (c : char) : Direction =
|
||||||
|
match c with
|
||||||
|
| 'L' -> Direction.Left
|
||||||
|
| 'R' -> Direction.Right
|
||||||
|
| 'U' -> Direction.Up
|
||||||
|
| 'D' -> Direction.Down
|
||||||
|
| c -> failwith $"Bad: %c{c}"
|
@@ -0,0 +1,70 @@
|
|||||||
|
namespace AdventOfCode2023
|
||||||
|
|
||||||
|
type IntervalSet = private | IntervalSet of (int * int) list
|
||||||
|
|
||||||
|
[<RequireQualifiedAccess>]
|
||||||
|
module IntervalSet =
|
||||||
|
let empty = IntervalSet []
|
||||||
|
|
||||||
|
let private add' (low : int) (high : int) intervals : _ list =
|
||||||
|
let rec go (low : int) (high : int) (intervals : (int * int) list) =
|
||||||
|
match intervals with
|
||||||
|
| [] -> [ low, high ]
|
||||||
|
| (lowExisting, highExisting) :: intervals ->
|
||||||
|
if low > highExisting then
|
||||||
|
(lowExisting, highExisting) :: go low high intervals
|
||||||
|
elif high < lowExisting then
|
||||||
|
(low, high) :: (lowExisting, highExisting) :: intervals
|
||||||
|
elif high = lowExisting then
|
||||||
|
(low, highExisting) :: intervals
|
||||||
|
elif high <= highExisting then
|
||||||
|
(min low lowExisting, highExisting) :: intervals
|
||||||
|
else
|
||||||
|
// low <= highExisting, highExisting < high
|
||||||
|
(min low lowExisting, highExisting) :: go (highExisting + 1) high intervals
|
||||||
|
|
||||||
|
go low high intervals
|
||||||
|
|
||||||
|
let add (low : int) (high : int) (IntervalSet intervals) : IntervalSet = add' low high intervals |> IntervalSet
|
||||||
|
|
||||||
|
let contains (x : int) (IntervalSet intervals) : bool =
|
||||||
|
let rec go (intervals : (int * int) list) =
|
||||||
|
match intervals with
|
||||||
|
| [] -> false
|
||||||
|
| (low, high) :: intervals ->
|
||||||
|
if low > x then false
|
||||||
|
elif x <= high then true
|
||||||
|
else go intervals
|
||||||
|
|
||||||
|
go intervals
|
||||||
|
|
||||||
|
let private union' i1 i2 =
|
||||||
|
(i2, i1) ||> List.fold (fun i (low, high) -> add' low high i)
|
||||||
|
|
||||||
|
let union (IntervalSet i1) (IntervalSet i2) : IntervalSet = union' i1 i2 |> IntervalSet
|
||||||
|
|
||||||
|
let private intersectionHelper (low : int) (high : int) (ints : (int * int) list) =
|
||||||
|
let rec go (low : int) (high : int) (ints : (int * int) list) =
|
||||||
|
match ints with
|
||||||
|
| [] -> []
|
||||||
|
| (lowExisting, highExisting) :: ints ->
|
||||||
|
if low > highExisting then
|
||||||
|
go low high ints
|
||||||
|
elif high < lowExisting then
|
||||||
|
[]
|
||||||
|
elif high <= highExisting then
|
||||||
|
[ max low lowExisting, high ]
|
||||||
|
else
|
||||||
|
(max low lowExisting, highExisting) :: go (highExisting + 1) high ints
|
||||||
|
|
||||||
|
go low high ints
|
||||||
|
|
||||||
|
let private intersection' i1 i2 =
|
||||||
|
// a int (b U c) = (a int b) U (a int c)
|
||||||
|
([], i1)
|
||||||
|
||> List.fold (fun soFar (low, high) -> union' soFar (intersectionHelper low high i2))
|
||||||
|
|
||||||
|
let intersection (IntervalSet i1) (IntervalSet i2) : IntervalSet = intersection' i1 i2 |> IntervalSet
|
||||||
|
|
||||||
|
let count (IntervalSet i) =
|
||||||
|
i |> List.sumBy (fun (low, high) -> high - low + 1)
|
38
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/List.fs
Normal file
38
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/List.fs
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
namespace AdventOfCode2023
|
||||||
|
|
||||||
|
open System
|
||||||
|
|
||||||
|
[<RequireQualifiedAccess>]
|
||||||
|
module List =
|
||||||
|
let rec nTuples (n : int) (xs : 'a list) : 'a list list =
|
||||||
|
#if DEBUG
|
||||||
|
if n < 0 then
|
||||||
|
raise (ArgumentException "n cannot be negative")
|
||||||
|
#endif
|
||||||
|
match n, xs with
|
||||||
|
| 0, _ -> [ [] ]
|
||||||
|
| _, [] -> []
|
||||||
|
| _, x :: xs ->
|
||||||
|
let withX = nTuples (n - 1) xs |> List.map (fun xs -> x :: xs)
|
||||||
|
let withoutX = nTuples n xs
|
||||||
|
withX @ withoutX
|
||||||
|
|
||||||
|
let inline inclusionExclusion (sizeOf : 'a -> 'ret) (sizeOfTuple : 'a list -> 'ret) (xs : 'a list) : 'ret =
|
||||||
|
let mutable result = List.sumBy sizeOf xs
|
||||||
|
let mutable i = 2
|
||||||
|
|
||||||
|
while i <= xs.Length do
|
||||||
|
let nTuples = nTuples i xs
|
||||||
|
let sum = List.sumBy sizeOfTuple nTuples
|
||||||
|
|
||||||
|
if sum = LanguagePrimitives.GenericZero then
|
||||||
|
i <- Int32.MaxValue
|
||||||
|
else
|
||||||
|
if i % 2 = 0 then
|
||||||
|
result <- result - sum
|
||||||
|
else
|
||||||
|
result <- result + sum
|
||||||
|
|
||||||
|
i <- i + 1
|
||||||
|
|
||||||
|
result
|
@@ -268,6 +268,82 @@ 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 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")
|
||||||
|
|
||||||
|
Console.WriteLine "=====Day 15====="
|
||||||
|
|
||||||
|
do
|
||||||
|
let input = Path.Combine (dir.FullName, "day15.txt") |> File.ReadAllText
|
||||||
|
|
||||||
|
sw.Restart ()
|
||||||
|
let part1 = Day15.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 = Day15.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 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")
|
||||||
|
|
||||||
|
Console.WriteLine "=====Day 19====="
|
||||||
|
|
||||||
|
do
|
||||||
|
let input = Path.Combine (dir.FullName, "day19.txt") |> File.ReadAllText
|
||||||
|
|
||||||
|
sw.Restart ()
|
||||||
|
use mutable s = StringSplitEnumerator.make '\n' input
|
||||||
|
let data = Day19.readWorkflows &s
|
||||||
|
sw.Stop ()
|
||||||
|
|
||||||
|
Console.Error.WriteLine (
|
||||||
|
(1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString ()
|
||||||
|
+ "ms parse"
|
||||||
|
)
|
||||||
|
|
||||||
|
let mutable sCopy = s
|
||||||
|
|
||||||
|
sw.Restart ()
|
||||||
|
let part1 = Day19.part1 data &s
|
||||||
|
sw.Stop ()
|
||||||
|
Console.WriteLine (part1.ToString ())
|
||||||
|
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
|
||||||
|
sw.Restart ()
|
||||||
|
let part2 = Day19.part2 data &sCopy
|
||||||
|
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 (
|
||||||
|
@@ -5,10 +5,15 @@
|
|||||||
|
|
||||||
<IsPackable>false</IsPackable>
|
<IsPackable>false</IsPackable>
|
||||||
<IsTestProject>true</IsTestProject>
|
<IsTestProject>true</IsTestProject>
|
||||||
|
|
||||||
|
<EnableStaticNativeInstrumentation>False</EnableStaticNativeInstrumentation>
|
||||||
|
<EnableDynamicNativeInstrumentation>False</EnableDynamicNativeInstrumentation>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Util.fs"/>
|
<Compile Include="Util.fs"/>
|
||||||
|
<Compile Include="TestIntervalSet.fs" />
|
||||||
|
<Compile Include="TestList.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"/>
|
||||||
@@ -22,6 +27,10 @@
|
|||||||
<Compile Include="TestDay11.fs" />
|
<Compile Include="TestDay11.fs" />
|
||||||
<Compile Include="TestDay12.fs" />
|
<Compile Include="TestDay12.fs" />
|
||||||
<Compile Include="TestDay13.fs" />
|
<Compile Include="TestDay13.fs" />
|
||||||
|
<Compile Include="TestDay14.fs" />
|
||||||
|
<Compile Include="TestDay15.fs" />
|
||||||
|
<Compile Include="TestDay16.fs" />
|
||||||
|
<Compile Include="TestDay19.fs" />
|
||||||
<EmbeddedResource Include="samples\day1.txt"/>
|
<EmbeddedResource Include="samples\day1.txt"/>
|
||||||
<EmbeddedResource Include="samples\day1part1.txt"/>
|
<EmbeddedResource Include="samples\day1part1.txt"/>
|
||||||
<EmbeddedResource Include="samples\day2.txt"/>
|
<EmbeddedResource Include="samples\day2.txt"/>
|
||||||
@@ -38,11 +47,17 @@
|
|||||||
<EmbeddedResource Include="samples\day11.txt" />
|
<EmbeddedResource Include="samples\day11.txt" />
|
||||||
<EmbeddedResource Include="samples\day12.txt" />
|
<EmbeddedResource Include="samples\day12.txt" />
|
||||||
<EmbeddedResource Include="samples\day13.txt" />
|
<EmbeddedResource Include="samples\day13.txt" />
|
||||||
|
<EmbeddedResource Include="samples\day14.txt" />
|
||||||
|
<EmbeddedResource Include="samples\day15.txt" />
|
||||||
|
<EmbeddedResource Include="samples\day16.txt" />
|
||||||
|
<EmbeddedResource Include="samples\day19.txt" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<PackageReference Include="FsCheck" Version="2.16.6" />
|
||||||
<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.8.0"/>
|
||||||
|
<PackageReference Include="Microsoft.CodeCoverage" Version="17.8.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"/>
|
||||||
|
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
|
46
AdventOfCode2023.FSharp/Test/TestDay15.fs
Normal file
46
AdventOfCode2023.FSharp/Test/TestDay15.fs
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
namespace AdventOfCode2023.Test
|
||||||
|
|
||||||
|
open AdventOfCode2023
|
||||||
|
open NUnit.Framework
|
||||||
|
open FsUnitTyped
|
||||||
|
open System.IO
|
||||||
|
|
||||||
|
[<TestFixture>]
|
||||||
|
module TestDay15 =
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let sample = Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day15.txt"
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part1Sample () =
|
||||||
|
sample |> Day15.part1 |> shouldEqual 1320
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Sample () =
|
||||||
|
sample |> Day15.part2 |> shouldEqual 145ul
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part1Actual () =
|
||||||
|
let s =
|
||||||
|
try
|
||||||
|
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day15.txt"))
|
||||||
|
with
|
||||||
|
| :? DirectoryNotFoundException
|
||||||
|
| :? FileNotFoundException ->
|
||||||
|
Assert.Inconclusive ()
|
||||||
|
failwith "unreachable"
|
||||||
|
|
||||||
|
Day15.part1 s |> shouldEqual 521434
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Actual () =
|
||||||
|
let s =
|
||||||
|
try
|
||||||
|
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day15.txt"))
|
||||||
|
with
|
||||||
|
| :? DirectoryNotFoundException
|
||||||
|
| :? FileNotFoundException ->
|
||||||
|
Assert.Inconclusive ()
|
||||||
|
failwith "unreachable"
|
||||||
|
|
||||||
|
Day15.part2 s |> shouldEqual 248279ul
|
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
|
55
AdventOfCode2023.FSharp/Test/TestDay19.fs
Normal file
55
AdventOfCode2023.FSharp/Test/TestDay19.fs
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
namespace AdventOfCode2023.Test
|
||||||
|
|
||||||
|
open AdventOfCode2023
|
||||||
|
open NUnit.Framework
|
||||||
|
open FsUnitTyped
|
||||||
|
open System.IO
|
||||||
|
|
||||||
|
[<TestFixture>]
|
||||||
|
module TestDay19 =
|
||||||
|
[<Test>]
|
||||||
|
let sample = Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day19.txt"
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part1Sample () =
|
||||||
|
use mutable s = StringSplitEnumerator.make '\n' sample
|
||||||
|
let workflows = Day19.readWorkflows &s
|
||||||
|
Day19.part1 workflows &s |> shouldEqual 19114
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Sample () =
|
||||||
|
use mutable s = StringSplitEnumerator.make '\n' sample
|
||||||
|
let workflows = Day19.readWorkflows &s
|
||||||
|
Day19.part2 workflows &s |> shouldEqual 167409079868000uL
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part1Actual () =
|
||||||
|
let s =
|
||||||
|
try
|
||||||
|
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day19.txt"))
|
||||||
|
with
|
||||||
|
| :? DirectoryNotFoundException
|
||||||
|
| :? FileNotFoundException ->
|
||||||
|
Assert.Inconclusive ()
|
||||||
|
failwith "unreachable"
|
||||||
|
|
||||||
|
use mutable s = StringSplitEnumerator.make '\n' s
|
||||||
|
let workflows = Day19.readWorkflows &s
|
||||||
|
|
||||||
|
Day19.part1 workflows &s |> shouldEqual 368964
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Actual () =
|
||||||
|
let s =
|
||||||
|
try
|
||||||
|
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day19.txt"))
|
||||||
|
with
|
||||||
|
| :? DirectoryNotFoundException
|
||||||
|
| :? FileNotFoundException ->
|
||||||
|
Assert.Inconclusive ()
|
||||||
|
failwith "unreachable"
|
||||||
|
|
||||||
|
use mutable s = StringSplitEnumerator.make '\n' s
|
||||||
|
let workflows = Day19.readWorkflows &s
|
||||||
|
|
||||||
|
Day19.part2 workflows &s |> shouldEqual 127675188176682uL
|
86
AdventOfCode2023.FSharp/Test/TestIntervalSet.fs
Normal file
86
AdventOfCode2023.FSharp/Test/TestIntervalSet.fs
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
namespace AdventOfCode2023.Test
|
||||||
|
|
||||||
|
open System.Threading
|
||||||
|
open AdventOfCode2023
|
||||||
|
open NUnit.Framework
|
||||||
|
open FsUnitTyped
|
||||||
|
open FsCheck
|
||||||
|
|
||||||
|
[<TestFixture>]
|
||||||
|
module TestIntervalSet =
|
||||||
|
|
||||||
|
/// Normalises e.g. (5, 3) to (3, 5) too.
|
||||||
|
let toIntervalSet (model : (int * int) list) =
|
||||||
|
(IntervalSet.empty, model)
|
||||||
|
||> List.fold (fun intervals (x1, x2) -> IntervalSet.add (min x1 x2) (max x1 x2) intervals)
|
||||||
|
|
||||||
|
let modelContains (x : int) (model : (int * int) list) =
|
||||||
|
model
|
||||||
|
|> List.exists (fun (x1, x2) ->
|
||||||
|
let x1, x2 = min x1 x2, max x1 x2
|
||||||
|
x1 <= x && x <= x2
|
||||||
|
)
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let ``IntervalSet add works`` () =
|
||||||
|
let property (pos : int ref) (neg : int ref) (x : int) (xs : (int * int) list) =
|
||||||
|
let intervals = toIntervalSet xs
|
||||||
|
|
||||||
|
let actual = IntervalSet.contains x intervals
|
||||||
|
let expected = modelContains x xs
|
||||||
|
|
||||||
|
if actual then
|
||||||
|
Interlocked.Increment pos |> ignore
|
||||||
|
else
|
||||||
|
Interlocked.Increment neg |> ignore
|
||||||
|
|
||||||
|
expected = actual
|
||||||
|
|
||||||
|
let pos = ref 0
|
||||||
|
let neg = ref 0
|
||||||
|
|
||||||
|
Check.One (
|
||||||
|
{ Config.Default with
|
||||||
|
MaxTest = 1000
|
||||||
|
},
|
||||||
|
property pos neg
|
||||||
|
)
|
||||||
|
|
||||||
|
printfn "Fraction of positive cases: %f" ((float pos.Value) / (float pos.Value + float neg.Value))
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let ``Intersection works`` () =
|
||||||
|
let property
|
||||||
|
(pos : int ref)
|
||||||
|
(neg : int ref)
|
||||||
|
(trials : int list)
|
||||||
|
(xsModel : (int * int) list)
|
||||||
|
(ysModel : (int * int) list)
|
||||||
|
=
|
||||||
|
let xs = toIntervalSet xsModel
|
||||||
|
let ys = toIntervalSet ysModel
|
||||||
|
|
||||||
|
let intervals = IntervalSet.intersection xs ys
|
||||||
|
|
||||||
|
for x in trials do
|
||||||
|
let actual = IntervalSet.contains x intervals
|
||||||
|
let expected = modelContains x xsModel && modelContains x ysModel
|
||||||
|
|
||||||
|
expected |> shouldEqual actual
|
||||||
|
|
||||||
|
if actual then
|
||||||
|
Interlocked.Increment pos |> ignore
|
||||||
|
else
|
||||||
|
Interlocked.Increment neg |> ignore
|
||||||
|
|
||||||
|
let pos = ref 0
|
||||||
|
let neg = ref 0
|
||||||
|
|
||||||
|
Check.One (
|
||||||
|
{ Config.Default with
|
||||||
|
MaxTest = 1000
|
||||||
|
},
|
||||||
|
property pos neg
|
||||||
|
)
|
||||||
|
|
||||||
|
printfn "Fraction of positive cases: %f" ((float pos.Value) / (float pos.Value + float neg.Value))
|
20
AdventOfCode2023.FSharp/Test/TestList.fs
Normal file
20
AdventOfCode2023.FSharp/Test/TestList.fs
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
namespace Test
|
||||||
|
|
||||||
|
open NUnit.Framework
|
||||||
|
open FsUnitTyped
|
||||||
|
open FsCheck
|
||||||
|
open AdventOfCode2023
|
||||||
|
|
||||||
|
[<TestFixture>]
|
||||||
|
module TestList =
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let ``n-tuples have the right length`` () =
|
||||||
|
let property (n : int) (xs : char list) =
|
||||||
|
let n = min (abs n) 6
|
||||||
|
let xs = xs |> List.take (min 10 xs.Length)
|
||||||
|
let tuples = List.nTuples n xs
|
||||||
|
tuples |> List.forall (fun i -> i.Length = n)
|
||||||
|
|
||||||
|
property 1 [ 'v' ] |> shouldEqual true
|
||||||
|
Check.QuickThrowOnFailure property
|
@@ -3,9 +3,7 @@ namespace AdventOfCode2023.Test
|
|||||||
open System.IO
|
open System.IO
|
||||||
open System.Reflection
|
open System.Reflection
|
||||||
|
|
||||||
type Dummy =
|
type Dummy = class end
|
||||||
class
|
|
||||||
end
|
|
||||||
|
|
||||||
[<RequireQualifiedAccess>]
|
[<RequireQualifiedAccess>]
|
||||||
module Assembly =
|
module Assembly =
|
||||||
|
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..#....
|
1
AdventOfCode2023.FSharp/Test/samples/day15.txt
Normal file
1
AdventOfCode2023.FSharp/Test/samples/day15.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7
|
10
AdventOfCode2023.FSharp/Test/samples/day16.txt
Normal file
10
AdventOfCode2023.FSharp/Test/samples/day16.txt
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
.|...\....
|
||||||
|
|.-.\.....
|
||||||
|
.....|-...
|
||||||
|
........|.
|
||||||
|
..........
|
||||||
|
.........\
|
||||||
|
..../.\\..
|
||||||
|
.-.-/..|..
|
||||||
|
.|....-|.\
|
||||||
|
..//.|....
|
17
AdventOfCode2023.FSharp/Test/samples/day19.txt
Normal file
17
AdventOfCode2023.FSharp/Test/samples/day19.txt
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
px{a<2006:qkq,m>2090:A,rfg}
|
||||||
|
pv{a>1716:R,A}
|
||||||
|
lnx{m>1548:A,A}
|
||||||
|
rfg{s<537:gd,x>2440:R,A}
|
||||||
|
qs{s>3448:A,lnx}
|
||||||
|
qkq{x<1416:A,crn}
|
||||||
|
crn{x>2662:A,R}
|
||||||
|
in{s<1351:px,qqz}
|
||||||
|
qqz{s>2770:qs,m<1801:hdj,R}
|
||||||
|
gd{a>3333:R,R}
|
||||||
|
hdj{m>838:A,pv}
|
||||||
|
|
||||||
|
{x=787,m=2655,a=1222,s=2876}
|
||||||
|
{x=1679,m=44,a=2067,s=496}
|
||||||
|
{x=2036,m=264,a=79,s=2244}
|
||||||
|
{x=2461,m=1339,a=466,s=291}
|
||||||
|
{x=2127,m=1623,a=2188,s=1013}
|
12
flake.lock
generated
12
flake.lock
generated
@@ -5,11 +5,11 @@
|
|||||||
"systems": "systems"
|
"systems": "systems"
|
||||||
},
|
},
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1694529238,
|
"lastModified": 1731533236,
|
||||||
"narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=",
|
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
|
||||||
"owner": "numtide",
|
"owner": "numtide",
|
||||||
"repo": "flake-utils",
|
"repo": "flake-utils",
|
||||||
"rev": "ff7b65b44d01cf9ba6a71320833626af21126384",
|
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
@@ -20,11 +20,11 @@
|
|||||||
},
|
},
|
||||||
"nixpkgs": {
|
"nixpkgs": {
|
||||||
"locked": {
|
"locked": {
|
||||||
"lastModified": 1701253981,
|
"lastModified": 1757068644,
|
||||||
"narHash": "sha256-ztaDIyZ7HrTAfEEUt9AtTDNoCYxUdSd6NrRHaYOIxtk=",
|
"narHash": "sha256-NOrUtIhTkIIumj1E/Rsv1J37Yi3xGStISEo8tZm3KW4=",
|
||||||
"owner": "NixOS",
|
"owner": "NixOS",
|
||||||
"repo": "nixpkgs",
|
"repo": "nixpkgs",
|
||||||
"rev": "e92039b55bcd58469325ded85d4f58dd5a4eaf58",
|
"rev": "8eb28adfa3dc4de28e792e3bf49fcf9007ca8ac9",
|
||||||
"type": "github"
|
"type": "github"
|
||||||
},
|
},
|
||||||
"original": {
|
"original": {
|
||||||
|
@@ -14,15 +14,12 @@
|
|||||||
system: let
|
system: let
|
||||||
pkgs = nixpkgs.legacyPackages.${system};
|
pkgs = nixpkgs.legacyPackages.${system};
|
||||||
in
|
in
|
||||||
# Conditionally include Swift and Apple SDK for Darwin systems
|
# Conditionally include Swift for Darwin systems
|
||||||
let
|
let
|
||||||
darwinDeps =
|
darwinDeps =
|
||||||
if system == "x86_64-darwin" || system == "aarch64-darwin"
|
if system == "x86_64-darwin" || system == "aarch64-darwin"
|
||||||
then [
|
then [
|
||||||
pkgs.swift
|
pkgs.swift
|
||||||
pkgs.darwin.apple_sdk.frameworks.Foundation
|
|
||||||
pkgs.darwin.apple_sdk.frameworks.CryptoKit
|
|
||||||
pkgs.darwin.apple_sdk.frameworks.GSS
|
|
||||||
]
|
]
|
||||||
else [];
|
else [];
|
||||||
in {
|
in {
|
||||||
|
30
mathematica/day_21.m
Normal file
30
mathematica/day_21.m
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
(* ::Package:: *)
|
||||||
|
|
||||||
|
(* ::Input:: *)
|
||||||
|
(*s="...........*)
|
||||||
|
(*.....###.#.*)
|
||||||
|
(*.###.##..#.*)
|
||||||
|
(*..#.#...#..*)
|
||||||
|
(*....#.#....*)
|
||||||
|
(*.##..S####.*)
|
||||||
|
(*.##..#...#.*)
|
||||||
|
(*.......##..*)
|
||||||
|
(*.##.#.####.*)
|
||||||
|
(*.##..##.##.*)
|
||||||
|
(*...........";*)
|
||||||
|
|
||||||
|
|
||||||
|
(* ::Input:: *)
|
||||||
|
(*reachable[grid_,{row_Integer,col_Integer}]:=reachable[grid,{row,col}]=Select[{{row+1,col},{row-1,col},{row,col+1},{row,col-1}},*)
|
||||||
|
(*1<=#[[1]]<=Length[grid]&&1<=#[[2]]<=Length[First@grid]&&grid[[#[[1]],#[[2]]]]!="#"&*)
|
||||||
|
(*]*)
|
||||||
|
|
||||||
|
|
||||||
|
(* ::Input:: *)
|
||||||
|
(*f[grid_,pos_,0]:={pos}*)
|
||||||
|
(*f[grid_,pos_,timestepsRemaining_Integer]:=*)
|
||||||
|
(*f[grid,pos,timestepsRemaining]=DeleteDuplicates@Flatten[f[grid,#,timestepsRemaining-1]&/@reachable[grid,pos],1]*)
|
||||||
|
|
||||||
|
|
||||||
|
(* ::Input:: *)
|
||||||
|
(*With[{grid=Characters/@StringSplit[s,"\n"]},f[grid,FirstPosition[grid,"S"],64]//Length]*)
|
Reference in New Issue
Block a user