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
18 changed files with 97 additions and 146 deletions

View File

@@ -3,7 +3,7 @@
"isRoot": true,
"tools": {
"fantomas": {
"version": "7.0.3",
"version": "6.2.3",
"commands": [
"fantomas"
]

View File

@@ -2,6 +2,7 @@ root=true
[*]
charset=utf-8
end_of_line=crlf
trim_trailing_whitespace=true
insert_final_newline=true
indent_style=space

1
.envrc
View File

@@ -1 +0,0 @@
use flake

1
.gitignore vendored
View File

@@ -8,7 +8,6 @@ _ReSharper.Caches/
.DS_Store
result
.profile*
.direnv/
inputs/
AdventOfCode2023.FSharp/Test/TestResults/

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"/>

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

@@ -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

@@ -322,24 +322,12 @@ module Program =
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

@@ -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

@@ -3,7 +3,9 @@ namespace AdventOfCode2023.Test
open System.IO
open System.Reflection
type Dummy = class end
type Dummy =
class
end
[<RequireQualifiedAccess>]
module Assembly =

12
flake.lock generated
View File

@@ -5,11 +5,11 @@
"systems": "systems"
},
"locked": {
"lastModified": 1731533236,
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
"lastModified": 1694529238,
"narHash": "sha256-zsNZZGTGnMOf9YpHKJqMSsa0dXbfmxeoJ7xHlrt+xmY=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
"rev": "ff7b65b44d01cf9ba6a71320833626af21126384",
"type": "github"
},
"original": {
@@ -20,11 +20,11 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1757068644,
"narHash": "sha256-NOrUtIhTkIIumj1E/Rsv1J37Yi3xGStISEo8tZm3KW4=",
"lastModified": 1701253981,
"narHash": "sha256-ztaDIyZ7HrTAfEEUt9AtTDNoCYxUdSd6NrRHaYOIxtk=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "8eb28adfa3dc4de28e792e3bf49fcf9007ca8ac9",
"rev": "e92039b55bcd58469325ded85d4f58dd5a4eaf58",
"type": "github"
},
"original": {

View File

@@ -14,12 +14,15 @@
system: let
pkgs = nixpkgs.legacyPackages.${system};
in
# Conditionally include Swift for Darwin systems
# Conditionally include Swift and Apple SDK for Darwin systems
let
darwinDeps =
if system == "x86_64-darwin" || system == "aarch64-darwin"
then [
pkgs.swift
pkgs.darwin.apple_sdk.frameworks.Foundation
pkgs.darwin.apple_sdk.frameworks.CryptoKit
pkgs.darwin.apple_sdk.frameworks.GSS
]
else [];
in {