13 Commits

Author SHA1 Message Date
Smaug123
c3611bffe3 Dead
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/all-checks-complete Pipeline was successful
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/all-checks-complete Pipeline was successful
2023-12-23 16:34:17 +00:00
Smaug123
a26a2a982b More 2023-12-23 16:31:04 +00:00
Smaug123
ad5dae8db7 Delete cata 2023-12-23 16:28:31 +00:00
Smaug123
1efec664ad Format 2023-12-23 16:27:28 +00:00
Smaug123
02a204ebb3 WIP 2023-12-23 16:07:15 +00:00
Smaug123
c84514aa38 No allocation please we're British 2023-12-23 15:42:20 +00:00
Smaug123
dbbb8a0326 Faster 2023-12-23 15:38:55 +00:00
Smaug123
9dd1148025 Rem todo 2023-12-23 15:28:20 +00:00
Smaug123
b0ebbe5842 Format 2023-12-23 15:24:43 +00:00
Smaug123
fd5b914237 Part 2 2023-12-23 15:24:37 +00:00
Smaug123
b66861e83b Union of interseections 2023-12-23 13:07:17 +00:00
Smaug123
8c60bfb744 Tree of ors with 'and' at leaf 2023-12-23 12:52:36 +00:00
Smaug123
8519bdfe05 wip 2023-12-23 12:52:25 +00:00
13 changed files with 82 additions and 315 deletions

View File

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
@@ -13,7 +13,6 @@
<Compile Include="Arithmetic.fs"/>
<Compile Include="Rational.fs"/>
<Compile Include="IntervalSet.fs" />
<Compile Include="Direction.fs" />
<Compile Include="List.fs" />
<Compile Include="Day1.fs"/>
<Compile Include="Day2.fs"/>
@@ -31,7 +30,6 @@
<Compile Include="Day14.fs" />
<Compile Include="Day15.fs" />
<Compile Include="Day16.fs" />
<Compile Include="Day18.fs" />
<Compile Include="Day19.fs" />
</ItemGroup>

View File

@@ -5,7 +5,6 @@
#nowarn "9"
#endif
open System
open Microsoft.FSharp.NativeInterop
[<Struct>]
@@ -101,76 +100,3 @@ module Arr2D =
#else
NativePtr.initBlock a.Elements 0uy (uint32 sizeof<'a> * uint32 a.Length)
#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 ""

View File

@@ -123,6 +123,41 @@ module Day10 =
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
@@ -275,12 +310,12 @@ module Day10 =
let stackBuf = ResizeArray ()
for line = 0 to system.Height - 1 do
Arr2D.floodFill stackBuf system 0uy 2uy 0 line
Arr2D.floodFill stackBuf system 0uy 2uy (system.Width - 1) line
floodFill stackBuf system 0 line
floodFill stackBuf system (system.Width - 1) line
for col = 0 to system.Width - 1 do
Arr2D.floodFill stackBuf system 0uy 2uy col 0
Arr2D.floodFill stackBuf system 0uy 2uy col (system.Height - 1)
floodFill stackBuf system col 0
floodFill stackBuf system col (system.Height - 1)
let mutable answer = 0

View File

@@ -137,6 +137,19 @@ module Day14 =
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

View File

@@ -10,8 +10,22 @@ open System
[<RequireQualifiedAccess>]
module Day16 =
type Direction =
| Left = 0
| Right = 1
| Up = 2
| Down = 3
let inline dirToInt (d : Direction) =
match d with
| Direction.Left -> 0us
| Direction.Right -> 1us
| Direction.Up -> 2us
| Direction.Down -> 3us
| _ -> failwith "Bad"
let inline storeDirectionAndPos (numCols : int) (col : int) (row : int) (direction : Direction) : uint16 =
4us * uint16 (col + numCols * row) + Direction.toUInt direction
4us * uint16 (col + numCols * row) + dirToInt direction
let inline getDirection (input : uint16) =
match input % 4us with

View File

@@ -1,103 +0,0 @@
namespace AdventOfCode2023
#if DEBUG
#else
#nowarn "9"
#endif
open System
open System.Collections.Generic
open System.Globalization
[<RequireQualifiedAccess>]
module Day18 =
let part1 (s : string) =
// Interleaved row and col
let mutable weHave = ResizeArray<int> ()
weHave.Add 0
weHave.Add 0
let mutable minCol = 0
let mutable maxCol = 0
let mutable minRow = 0
let mutable maxRow = 0
let mutable col = 0
let mutable row = 0
let mutable edgeCount = 0
use rows = StringSplitEnumerator.make '\n' s
for currRow in rows do
use mutable entries = StringSplitEnumerator.make' ' ' currRow
entries.MoveNext () |> ignore
let dir = Direction.ofChar entries.Current.[0]
entries.MoveNext () |> ignore
let distance =
Int32.Parse (entries.Current, NumberStyles.None, CultureInfo.InvariantCulture)
edgeCount <- edgeCount + distance
match dir with
| Direction.Down ->
for _ = 1 to distance do
row <- row + 1
weHave.Add row
weHave.Add col
maxRow <- max row maxRow
| Direction.Up ->
for _ = 1 to distance do
row <- row - 1
weHave.Add row
weHave.Add col
minRow <- min row minRow
| Direction.Left ->
for _ = 1 to distance do
col <- col - 1
weHave.Add row
weHave.Add col
minCol <- min col minCol
| Direction.Right ->
for _ = 1 to distance do
col <- col + 1
weHave.Add row
weHave.Add col
maxCol <- max col maxCol
| _ -> failwith "bad dir"
let buffer = Array.zeroCreate ((maxRow - minRow + 3) * (maxCol - minCol + 3))
#if DEBUG
let system : Arr2D<byte> =
{
Elements = buffer
Width = maxCol - minCol + 3
}
#else
use ptr = fixed buffer
let system : Arr2D<byte> =
{
Elements = ptr
Length = buffer.Length
Width = maxCol - minCol + 3
}
#endif
let mutable pointIndex = 0
while pointIndex < weHave.Count do
let row = weHave.[pointIndex]
let col = weHave.[pointIndex + 1]
Arr2D.set system (col - minCol + 1) (row - minRow + 1) 1uy
pointIndex <- pointIndex + 2
Arr2D.floodFill (ResizeArray ()) system 0uy 2uy 0 0
Arr2D.count system 0uy + edgeCount
let part2 (s : string) = -1

View File

@@ -126,7 +126,10 @@ module Day19 =
workflows
let part1 (workflows : _) (rows : byref<StringSplitEnumerator>) =
let part1 (s : string) =
use mutable rows = StringSplitEnumerator.make '\n' s
let workflows = readWorkflows &rows
let mutable answer = 0
for row in rows do
@@ -348,7 +351,10 @@ module Day19 =
store.[key] <- result
result
let part2 (workflows : _) (rows : byref<StringSplitEnumerator>) : uint64 =
let part2 (s : string) : uint64 =
use mutable rows = StringSplitEnumerator.make '\n' s
let workflows = readWorkflows &rows
let acceptanceRanges = Dictionary<string, AcceptanceCriterion> ()
let a = acceptance acceptanceRanges workflows "in"

View File

@@ -1,24 +0,0 @@
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}"

View File

@@ -316,46 +316,18 @@ module Program =
Console.WriteLine (part2.ToString ())
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
Console.WriteLine "=====Day 18====="
do
let input = Path.Combine (dir.FullName, "day18.txt") |> File.ReadAllText
sw.Restart ()
let part1 = Day18.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 = Day18.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
let part1 = Day19.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 = Day19.part2 data &sCopy
let part2 = Day19.part2 input
sw.Stop ()
Console.WriteLine (part2.ToString ())
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")

View File

@@ -30,7 +30,6 @@
<Compile Include="TestDay14.fs" />
<Compile Include="TestDay15.fs" />
<Compile Include="TestDay16.fs" />
<Compile Include="TestDay18.fs" />
<Compile Include="TestDay19.fs" />
<EmbeddedResource Include="samples\day1.txt"/>
<EmbeddedResource Include="samples\day1part1.txt"/>
@@ -51,7 +50,6 @@
<EmbeddedResource Include="samples\day14.txt" />
<EmbeddedResource Include="samples\day15.txt" />
<EmbeddedResource Include="samples\day16.txt" />
<EmbeddedResource Include="samples\day18.txt" />
<EmbeddedResource Include="samples\day19.txt" />
</ItemGroup>

View File

@@ -1,44 +0,0 @@
namespace AdventOfCode2023.Test
open AdventOfCode2023
open NUnit.Framework
open FsUnitTyped
open System.IO
[<TestFixture>]
module TestDay18 =
[<Test>]
let sample = Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day18.txt"
[<Test>]
let part1Sample () = sample |> Day18.part1 |> shouldEqual 62
[<Test>]
let part2Sample () = sample |> Day18.part2 |> shouldEqual 0
[<Test>]
let part1Actual () =
let s =
try
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day18.txt"))
with
| :? DirectoryNotFoundException
| :? FileNotFoundException ->
Assert.Inconclusive ()
failwith "unreachable"
Day18.part1 s |> shouldEqual 106459
[<Test>]
let part2Actual () =
let s =
try
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day18.txt"))
with
| :? DirectoryNotFoundException
| :? FileNotFoundException ->
Assert.Inconclusive ()
failwith "unreachable"
Day18.part2 s |> shouldEqual 0

View File

@@ -12,15 +12,11 @@ module TestDay19 =
[<Test>]
let part1Sample () =
use mutable s = StringSplitEnumerator.make '\n' sample
let workflows = Day19.readWorkflows &s
Day19.part1 workflows &s |> shouldEqual 19114
sample |> Day19.part1 |> shouldEqual 19114
[<Test>]
let part2Sample () =
use mutable s = StringSplitEnumerator.make '\n' sample
let workflows = Day19.readWorkflows &s
Day19.part2 workflows &s |> shouldEqual 167409079868000uL
sample |> Day19.part2 |> shouldEqual 167409079868000uL
[<Test>]
let part1Actual () =
@@ -33,10 +29,7 @@ module TestDay19 =
Assert.Inconclusive ()
failwith "unreachable"
use mutable s = StringSplitEnumerator.make '\n' s
let workflows = Day19.readWorkflows &s
Day19.part1 workflows &s |> shouldEqual 368964
Day19.part1 s |> shouldEqual 368964
[<Test>]
let part2Actual () =
@@ -49,7 +42,4 @@ module TestDay19 =
Assert.Inconclusive ()
failwith "unreachable"
use mutable s = StringSplitEnumerator.make '\n' s
let workflows = Day19.readWorkflows &s
Day19.part2 workflows &s |> shouldEqual 127675188176682uL
Day19.part2 s |> shouldEqual 127675188176682uL

View File

@@ -1,14 +0,0 @@
R 6 (#70c710)
D 5 (#0dc571)
L 2 (#5713f0)
D 2 (#d2c081)
R 2 (#59c680)
D 2 (#411b91)
L 5 (#8ceee2)
U 2 (#caa173)
L 1 (#1b58a2)
U 2 (#caa171)
R 2 (#7807d2)
U 3 (#a77fa3)
L 2 (#015232)
U 2 (#7a21e3)