Compare commits

18 Commits

Author SHA1 Message Date
Smaug123
71e6d8479a strace
Some checks failed
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/pr/build Pipeline failed
ci/woodpecker/push/all-checks-complete Pipeline was successful
ci/woodpecker/pr/all-checks-complete unknown status
2023-12-08 22:59:45 +00:00
Smaug123
f31a856838 why
Some checks failed
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/pr/build Pipeline failed
ci/woodpecker/push/all-checks-complete Pipeline was successful
ci/woodpecker/pr/all-checks-complete unknown status
2023-12-08 22:52:37 +00:00
Smaug123
10e2602b50 why
Some checks failed
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/pr/build Pipeline failed
ci/woodpecker/push/all-checks-complete Pipeline was successful
ci/woodpecker/pr/all-checks-complete unknown status
2023-12-08 22:45:04 +00:00
Smaug123
729a205dc8 sigh
Some checks failed
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/pr/build Pipeline failed
ci/woodpecker/push/all-checks-complete Pipeline was successful
ci/woodpecker/pr/all-checks-complete unknown status
2023-12-08 22:40:02 +00:00
Smaug123
3499326730 sigh
Some checks failed
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/pr/build Pipeline failed
ci/woodpecker/push/all-checks-complete Pipeline was successful
ci/woodpecker/pr/all-checks-complete unknown status
2023-12-08 22:35:59 +00:00
Smaug123
b67efa2198 hmm
Some checks failed
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/pr/build Pipeline failed
ci/woodpecker/push/all-checks-complete Pipeline was successful
ci/woodpecker/pr/all-checks-complete unknown status
2023-12-08 22:31:05 +00:00
Smaug123
5c445e8175 mooore
Some checks failed
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/pr/build Pipeline failed
ci/woodpecker/push/all-checks-complete Pipeline was successful
ci/woodpecker/pr/all-checks-complete unknown status
2023-12-08 22:24:13 +00:00
Smaug123
a47d3e635e pls pipeline
Some checks failed
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/pr/build Pipeline failed
ci/woodpecker/push/all-checks-complete Pipeline was successful
ci/woodpecker/pr/all-checks-complete unknown status
2023-12-08 22:18:17 +00:00
Smaug123
53e247d222 Go
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/all-checks-complete Pipeline was successful
ci/woodpecker/manual/build Pipeline was successful
ci/woodpecker/manual/all-checks-complete Pipeline was successful
2023-12-08 22:16:15 +00:00
Smaug123
78680ccaf8 Quote
Some checks failed
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/all-checks-complete Pipeline was successful
ci/woodpecker/pr/build Pipeline failed
ci/woodpecker/pr/all-checks-complete unknown status
2023-12-08 22:11:03 +00:00
Smaug123
a0b9e50f50 debug
Some checks failed
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/pr/build Pipeline failed
ci/woodpecker/push/all-checks-complete Pipeline was successful
ci/woodpecker/pr/all-checks-complete unknown status
2023-12-08 22:04:54 +00:00
Smaug123
2510873e5f Oops
Some checks failed
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/pr/build Pipeline failed
ci/woodpecker/push/all-checks-complete Pipeline was successful
ci/woodpecker/pr/all-checks-complete unknown status
2023-12-08 21:57:12 +00:00
Smaug123
9d81279173 How about this
Some checks failed
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/pr/build Pipeline failed
ci/woodpecker/push/all-checks-complete Pipeline was successful
ci/woodpecker/pr/all-checks-complete unknown status
2023-12-08 21:52:32 +00:00
Smaug123
7d1ba3e6a1 Go
Some checks failed
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/pr/build Pipeline failed
ci/woodpecker/push/all-checks-complete Pipeline was successful
ci/woodpecker/pr/all-checks-complete unknown status
2023-12-08 21:35:50 +00:00
Smaug123
e8fa607279 Sigh, here we go
Some checks failed
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/all-checks-complete Pipeline was successful
ci/woodpecker/pr/build Pipeline failed
ci/woodpecker/pr/all-checks-complete unknown status
2023-12-08 21:30:58 +00:00
d94663ae0e Day 8 (#8)
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/all-checks-complete Pipeline was successful
Blown way through my time budget and there's a bunch of low hanging fruit (data dependencies in LCM, for example) but I'm late for work.

Co-authored-by: Smaug123 <patrick+github@patrickstevens.co.uk>
Reviewed-on: #8
2023-12-08 09:07:04 +00:00
bcd2bb6349 Speed up day 7 a bit (#7)
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/all-checks-complete Pipeline was successful
I'm going mad, why am I doing this

Co-authored-by: Smaug123 <patrick+github@patrickstevens.co.uk>
Reviewed-on: #7
2023-12-07 23:52:34 +00:00
2fbdf2c362 Day 7 (#6)
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/all-checks-complete Pipeline was successful
Co-authored-by: Smaug123 <patrick+github@patrickstevens.co.uk>
Reviewed-on: #6
2023-12-07 09:32:21 +00:00
19 changed files with 735 additions and 67 deletions

View File

@@ -3,6 +3,17 @@ steps:
image: nixos/nix
commands:
- echo 'experimental-features = flakes nix-command' >> /etc/nix/nix.conf
- "nix develop --command dotnet publish AdventOfCode2023.FSharp/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.fsproj --configuration Release -p:PublishAot=true || echo 'First publish failed'"
- "nix develop --command sh -c 'patchelf --set-interpreter $LINKER_PATH /tmp/dotnet-home/.nuget/packages/runtime.linux-x64.microsoft.dotnet.ilcompiler/8.0.0/tools/ilc'"
- "ls -al /tmp/dotnet-home/.nuget/packages/runtime.linux-x64.microsoft.dotnet.ilcompiler/8.0.0/tools/ilc"
- "chmod a+x /tmp/dotnet-home/.nuget/packages/runtime.linux-x64.microsoft.dotnet.ilcompiler/8.0.0/tools/ilc"
- "ls -al /tmp/dotnet-home/.nuget/packages/runtime.linux-x64.microsoft.dotnet.ilcompiler/8.0.0/tools/ilc"
- "whoami"
- "cp -r AdventOfCode2023.FSharp/AdventOfCode2023.FSharp/obj /tmp/obj"
- "nix develop --command sh -c 'ls -la $LINKER_PATH'"
- "nix develop --command sh -c 'strace /tmp/dotnet-home/.nuget/packages/runtime.linux-x64.microsoft.dotnet.ilcompiler/8.0.0/tools/ilc /tmp/obj/Release/net8.0/linux-x64/native/AdventOfCode2023.FSharp.ilc.rsp'"
- "nix develop --command dotnet publish AdventOfCode2023.FSharp/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.fsproj --configuration Release -p:PublishAot=true"
# Lint
- "nix flake check"
# Test
@@ -11,8 +22,9 @@ steps:
- nix develop --command alejandra --check .
- nix develop --command dotnet tool restore
- nix develop --command dotnet fantomas --check .
# - nix develop --command dotnet publish AdventOfCode2023.FSharp/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.fsproj --configuration Release
# - nix develop --command sh -c "$(find . -type f -name AdventOfCode2023.FSharp | grep Release | grep publish) AdventOfCode2023.FSharp/Test/samples"
# TODO: if https://github.com/dotnet/sdk/issues/37295 ever gets fixed, remove the PublishAot=false
- "nix develop --command dotnet publish AdventOfCode2023.FSharp/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.fsproj --configuration Release -p:PublishAot=false -p:SelfContained=true"
- '$(find . -type f -name AdventOfCode2023.FSharp | grep Release | grep publish) "$(pwd)/AdventOfCode2023.FSharp/Test/samples"'
when:
- event: "push"

View File

@@ -7,13 +7,17 @@
<ItemGroup>
<Compile Include="Arr2D.fs" />
<Compile Include="ResizeArray.fs" />
<Compile Include="EfficientString.fs" />
<Compile Include="Arithmetic.fs" />
<Compile Include="Day1.fs" />
<Compile Include="Day2.fs" />
<Compile Include="Day3.fs" />
<Compile Include="Day4.fs" />
<Compile Include="Day5.fs" />
<Compile Include="Day6.fs" />
<Compile Include="Day7.fs" />
<Compile Include="Day8.fs" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,67 @@
namespace AdventOfCode2023
[<RequireQualifiedAccess>]
module Arithmetic =
[<Struct>]
type EuclidResult<'a> =
{
Hcf : 'a
A : 'a
B : 'a
}
/// Compute floor(sqrt(i)).
let inline sqrt (i : ^a) =
if i <= LanguagePrimitives.GenericOne then
i
else
let rec go start =
let next = start + LanguagePrimitives.GenericOne
let sqr = next * next
if sqr < LanguagePrimitives.GenericZero then
// Overflow attempted, so the sqrt is between start and next
start
elif i < sqr then
start
elif i = sqr then
next
else
go next
go LanguagePrimitives.GenericOne
/// Find Hcf, A, B s.t. A * a + B * b = Hcf, and Hcf is the highest common factor of a and b.
let inline euclideanAlgorithm (a : ^a) (b : ^a) : EuclidResult< ^a > =
let rec go rMin1 r sMin1 s tMin1 t =
if r = LanguagePrimitives.GenericZero then
{
Hcf = rMin1
A = sMin1
B = tMin1
}
else
let newQ = rMin1 / r
go r (rMin1 - newQ * r) s (sMin1 - newQ * s) t (tMin1 - newQ * t)
let maxA = max a b
let minB = min a b
let result =
go
maxA
minB
LanguagePrimitives.GenericOne
LanguagePrimitives.GenericZero
LanguagePrimitives.GenericZero
LanguagePrimitives.GenericOne
if a = maxA then
result
else
{
Hcf = result.Hcf
A = result.B
B = result.A
}

View File

@@ -5,21 +5,13 @@ open System
[<RequireQualifiedAccess>]
module Day1 =
let firstDigit (s : ReadOnlySpan<char>) =
let mutable pos = 0
while '0' > s.[pos] || s.[pos] > '9' do
pos <- pos + 1
let inline firstDigit (s : ReadOnlySpan<char>) =
let pos = s.IndexOfAnyInRange ('0', '9')
byte s.[pos] - byte '0'
// No surrogate pairs please!
let lastDigit (s : ReadOnlySpan<char>) =
let mutable pos = s.Length - 1
while '0' > s.[pos] || s.[pos] > '9' do
pos <- pos - 1
let inline lastDigit (s : ReadOnlySpan<char>) =
let pos = s.LastIndexOfAnyInRange ('0', '9')
byte s.[pos] - byte '0'
let part1 (s : string) =
@@ -35,35 +27,66 @@ module Day1 =
total
let table =
[|
"one", 1uy
"two", 2uy
"three", 3uy
"four", 4uy
"five", 5uy
"six", 6uy
"seven", 7uy
"eight", 8uy
"nine", 9uy
|]
let isDigitSpelled (s : ReadOnlySpan<char>) (pos : int) (answer : byref<byte>) =
// Can't be bothered to write a jump-table compiler
if s.[pos] >= '0' && s.[pos] <= '9' then
answer <- byte s.[pos] - byte '0'
else if s.[pos] = 'o' then
if pos + 2 < s.Length && s.[pos + 1] = 'n' && s.[pos + 2] = 'e' then
answer <- 1uy
elif s.[pos] = 't' then
if pos + 2 < s.Length && s.[pos + 1] = 'w' && s.[pos + 2] = 'o' then
answer <- 2uy
elif
pos + 4 < s.Length
&& s.[pos + 1] = 'h'
&& s.[pos + 2] = 'r'
&& s.[pos + 3] = 'e'
&& s.[pos + 4] = 'e'
then
answer <- 3uy
elif s.[pos] = 'f' then
if pos + 3 < s.Length then
if s.[pos + 1] = 'o' && s.[pos + 2] = 'u' && s.[pos + 3] = 'r' then
answer <- 4uy
elif s.[pos + 1] = 'i' && s.[pos + 2] = 'v' && s.[pos + 3] = 'e' then
answer <- 5uy
elif s.[pos] = 's' then
if pos + 2 < s.Length && s.[pos + 1] = 'i' && s.[pos + 2] = 'x' then
answer <- 6uy
elif
pos + 4 < s.Length
&& s.[pos + 1] = 'e'
&& s.[pos + 2] = 'v'
&& s.[pos + 3] = 'e'
&& s.[pos + 4] = 'n'
then
answer <- 7uy
elif s.[pos] = 'e' then
if
pos + 4 < s.Length
&& s.[pos + 1] = 'i'
&& s.[pos + 2] = 'g'
&& s.[pos + 3] = 'h'
&& s.[pos + 4] = 't'
then
answer <- 8uy
elif s.[pos] = 'n' then
if
pos + 3 < s.Length
&& s.[pos + 1] = 'i'
&& s.[pos + 2] = 'n'
&& s.[pos + 3] = 'e'
then
answer <- 9uy
let firstDigitIncSpelled (s : ReadOnlySpan<char>) =
let mutable pos = 0
let mutable answer = 255uy
while answer = 255uy do
if s.[pos] >= '0' && s.[pos] <= '9' then
answer <- byte s.[pos] - byte '0'
else
for i, value in table do
if
pos + i.Length < s.Length
&& MemoryExtensions.SequenceEqual (s.Slice (pos, i.Length), i)
then
answer <- value
pos <- pos + 1
isDigitSpelled s pos &answer
pos <- pos + 1
answer
@@ -72,17 +95,8 @@ module Day1 =
let mutable answer = 255uy
while answer = 255uy do
if s.[pos] >= '0' && s.[pos] <= '9' then
answer <- byte s.[pos] - byte '0'
else
for i, value in table do
if
pos - i.Length + 1 >= 0
&& MemoryExtensions.SequenceEqual (s.Slice (pos - i.Length + 1, i.Length), i)
then
answer <- value
pos <- pos - 1
isDigitSpelled s pos &answer
pos <- pos - 1
answer

View File

@@ -1,9 +1,12 @@
namespace AdventOfCode2023
open System
open System.Globalization
[<RequireQualifiedAccess>]
module Day2 =
let inline parseInt (s : ReadOnlySpan<char>) : int =
Int32.Parse (s, NumberStyles.None, CultureInfo.InvariantCulture)
let part1 (s : string) =
use lines = StringSplitEnumerator.make '\n' s
@@ -18,20 +21,20 @@ module Day2 =
while isOk && words.MoveNext () do
match words.Current.[0] with
| 'b' ->
if Int32.Parse prevWord > 14 then
if parseInt prevWord > 14 then
isOk <- false
| 'r' ->
if Int32.Parse prevWord > 12 then
if parseInt prevWord > 12 then
isOk <- false
| 'g' ->
if Int32.Parse prevWord > 13 then
if parseInt prevWord > 13 then
isOk <- false
| _ -> ()
prevWord <- words.Current
if isOk then
answer <- answer + Int32.Parse (line.Slice (5, line.IndexOf ':' - 5))
answer <- answer + parseInt (line.Slice (5, line.IndexOf ':' - 5))
answer
@@ -49,9 +52,9 @@ module Day2 =
while words.MoveNext () do
match words.Current.[0] with
| 'b' -> blues <- max blues (Int32.Parse prevWord)
| 'r' -> reds <- max reds (Int32.Parse prevWord)
| 'g' -> greens <- max greens (Int32.Parse prevWord)
| 'b' -> blues <- max blues (parseInt prevWord)
| 'r' -> reds <- max reds (parseInt prevWord)
| 'g' -> greens <- max greens (parseInt prevWord)
| _ -> ()
prevWord <- words.Current

View File

@@ -146,7 +146,7 @@ module Day3 =
let mutable answer = 0
for KeyValue (_gearPos, gears) in gears do
for gears in gears.Values do
if gears.Count = 2 then
answer <- answer + gears.[0] * gears.[1]

View File

@@ -54,8 +54,6 @@ module Day4 =
total
let part2 (s : string) =
use lines = StringSplitEnumerator.make '\n' s
let winningNumbers = ResizeArray<byte> ()

View File

@@ -0,0 +1,215 @@
namespace AdventOfCode2023
open System
open System.Globalization
open AdventOfCode2023.ResizeArray
type Hand =
| Five = 6
| Four = 5
| FullHouse = 4
| Three = 3
| TwoPairs = 2
| Pair = 1
| High = 0
type HandContents =
{
First : byte
Second : byte
Third : byte
Fourth : byte
Fifth : byte
}
[<RequireQualifiedAccess>]
module Day7 =
[<Literal>]
let joker = 0uy
let inline toByte (adjustJoker : bool) (c : char) : byte =
if c <= '9' then byte c - byte '1'
elif c = 'T' then 9uy
elif c = 'J' then (if adjustJoker then joker else 10uy)
elif c = 'Q' then 11uy
elif c = 'K' then 12uy
elif c = 'A' then 13uy
else failwithf "could not parse: %c" c
let inline private updateState (tallies : ResizeArray<_>) newNum =
let mutable isAdded = false
for i = 0 to tallies.Count - 1 do
if fst tallies.[i] = newNum then
tallies.[i] <- (fst tallies.[i], snd tallies.[i] + 1)
isAdded <- true
if not isAdded then
tallies.Add (newNum, 1)
type RankedHand = uint32
[<Literal>]
let fourteen = 14ul
[<Literal>]
let fourteenFive = fourteen * fourteen * fourteen * fourteen * fourteen
[<Literal>]
let fourteenFour = fourteen * fourteen * fourteen * fourteen
[<Literal>]
let fourteenThree = fourteen * fourteen * fourteen
[<Literal>]
let fourteenTwo = fourteen * fourteen
let toInt (hand : Hand) (contents : HandContents) : RankedHand =
uint32 hand * fourteenFive
+ uint32 contents.First * fourteenFour
+ uint32 contents.Second * fourteenThree
+ uint32 contents.Third * fourteenTwo
+ uint32 contents.Fourth * fourteen
+ uint32 contents.Fifth
let parseHand (tallyBuffer : ResizeArray<_>) (adjustJoker : bool) (s : ReadOnlySpan<char>) : RankedHand =
let contents =
{
First = toByte adjustJoker s.[0]
Second = toByte adjustJoker s.[1]
Third = toByte adjustJoker s.[2]
Fourth = toByte adjustJoker s.[3]
Fifth = toByte adjustJoker s.[4]
}
tallyBuffer.Clear ()
tallyBuffer.Add (contents.First, 1)
updateState tallyBuffer contents.Second
updateState tallyBuffer contents.Third
updateState tallyBuffer contents.Fourth
updateState tallyBuffer contents.Fifth
let jokerCount, jokerPos =
if not adjustJoker then
0, -1
else
let mutable jokerCount = 0
let mutable jokerPos = 0
while jokerPos < tallyBuffer.Count && jokerCount = 0 do
let card, tally = tallyBuffer.[jokerPos]
if card = joker then
jokerCount <- tally
else
jokerPos <- jokerPos + 1
jokerCount, jokerPos
let hand =
if jokerCount > 0 then
match tallyBuffer.Count with
| 1 ->
// Five jokers
Hand.Five
| 2 ->
// Jokers plus one other card type
Hand.Five
| 3 ->
// Jokers plus two other card types. Either full house, or four of a kind
if jokerCount >= 2 then
// JJABB or JJJAB
Hand.Four
else if
// JAABB or JAAAB
jokerPos <> 0
then
if snd tallyBuffer.[0] = 2 then
Hand.FullHouse
else
Hand.Four
else if snd tallyBuffer.[1] = 2 then
Hand.FullHouse
else
Hand.Four
| 4 ->
// Jokers plus three other card types, exactly one of which therefore is a two-of.
Hand.Three
| 5 ->
// Five different cards, one of which is a joker.
Hand.Pair
| _ -> failwith "bad tallyBuffer"
elif tallyBuffer.Count = 1 then
Hand.Five
elif tallyBuffer.Count = 2 then
// AAAAB or AAABB
if snd tallyBuffer.[0] = 3 || snd tallyBuffer.[0] = 2 then
Hand.FullHouse
else
Hand.Four
elif tallyBuffer.Count = 3 then
// AAABC or AABBC
if snd tallyBuffer.[0] = 3 then Hand.Three
elif snd tallyBuffer.[0] = 2 then Hand.TwoPairs
elif snd tallyBuffer.[1] = 3 then Hand.Three
elif snd tallyBuffer.[1] = 2 then Hand.TwoPairs
else Hand.Three
elif tallyBuffer.Count = 4 then
Hand.Pair
else
Hand.High
toInt hand contents
type RankedHandAndBid = uint32
[<Literal>]
let bidSeparator = 1001ul
let inline toRankedHandAndBid (r : RankedHand) (bid : uint32) : RankedHandAndBid = bidSeparator * r + bid
let inline getBid (r : RankedHandAndBid) : uint32 = uint32 (r % bidSeparator)
let parse (adjustJoker : bool) (s : string) : ResizeArray<RankedHandAndBid> =
use mutable lines = StringSplitEnumerator.make '\n' s
let result = ResizeArray.create 4
let tallies = ResizeArray.create 5
while lines.MoveNext () do
if not lines.Current.IsEmpty then
use mutable line = StringSplitEnumerator.make' ' ' lines.Current
line.MoveNext () |> ignore
let rankedHand = parseHand tallies adjustJoker line.Current
line.MoveNext () |> ignore
let bid =
UInt32.Parse (line.Current, NumberStyles.Integer, CultureInfo.InvariantCulture)
result.Add (toRankedHandAndBid rankedHand bid)
result
let part1 (s : string) =
let arr = parse false s
arr.Sort ()
let mutable answer = 0ul
for i = 0 to arr.Count - 1 do
answer <- answer + getBid arr.[i] * (uint32 i + 1ul)
answer
let part2 (s : string) =
let arr = parse true s
arr.Sort ()
let mutable answer = 0ul
for i = 0 to arr.Count - 1 do
answer <- answer + getBid arr.[i] * (uint32 i + 1ul)
answer

View File

@@ -0,0 +1,110 @@
namespace AdventOfCode2023
open System
open System.Collections.Generic
[<RequireQualifiedAccess>]
module Day8 =
type Instructions =
{
/// "true" is 'R'
Steps : bool array
Nodes : Dictionary<string, string * string>
}
let parse (s : string) =
use mutable lines = StringSplitEnumerator.make '\n' s
lines.MoveNext () |> ignore
let stepsLine = lines.Current.TrimEnd ()
let steps = Array.zeroCreate stepsLine.Length
for i = 0 to stepsLine.Length - 1 do
steps.[i] <- (stepsLine.[i] = 'R')
let dict = Dictionary ()
while lines.MoveNext () do
if not lines.Current.IsEmpty then
use mutable line = StringSplitEnumerator.make' ' ' lines.Current
line.MoveNext () |> ignore
let key = line.Current.ToString ()
line.MoveNext () |> ignore
line.MoveNext () |> ignore
let v1 = line.Current.Slice(1, line.Current.Length - 2).ToString ()
line.MoveNext () |> ignore
let v2 = line.Current.Slice(0, line.Current.Length - 1).ToString ()
dict.[key] <- (v1, v2)
{
Steps = steps
Nodes = dict
}
let part1 (s : string) =
let data = parse s
let mutable i = 0
let mutable currentNode = "AAA"
let mutable answer = 0
while currentNode <> "ZZZ" do
let instruction = data.Nodes.[currentNode]
if data.Steps.[i] then
// "true" is R
currentNode <- snd instruction
else
currentNode <- fst instruction
i <- (i + 1) % data.Steps.Length
answer <- answer + 1
answer
let inline lcm (periods : ^T[]) =
let mutable lcm = periods.[0]
let mutable i = 1
while i < periods.Length do
let euclid = Arithmetic.euclideanAlgorithm lcm periods.[i]
lcm <- (lcm * periods.[i]) / euclid.Hcf
i <- i + 1
lcm
let part2 (s : string) =
let data = parse s
let startingNodes = ResizeArray ()
for key in data.Nodes.Keys do
if key.[key.Length - 1] = 'A' then
startingNodes.Add key
let periods =
Array.init
startingNodes.Count
(fun startNode ->
let mutable i = 0
let mutable currentNode = startingNodes.[startNode]
let mutable answer = 0ul
while currentNode.[currentNode.Length - 1] <> 'Z' do
let instruction = data.Nodes.[currentNode]
if data.Steps.[i] then
// "true" is R
currentNode <- snd instruction
else
currentNode <- fst instruction
i <- (i + 1) % data.Steps.Length
answer <- answer + 1ul
uint64 answer
)
lcm periods

View File

@@ -0,0 +1,40 @@
namespace AdventOfCode2023.ResizeArray
open System
type ResizeArray<'T> =
private
{
mutable Array : 'T array
mutable Length : int
}
member this.Count = this.Length
member this.Clear () = this.Length <- 0
member this.Add (t : 'T) =
if this.Length < this.Array.Length then
this.Array.[this.Length] <- t
else
let newLength = this.Length * 2
let newArray = Array.zeroCreate<'T> newLength
Array.blit this.Array 0 newArray 0 this.Length
newArray.[this.Length] <- t
this.Array <- newArray
this.Length <- this.Length + 1
member this.Item
with get (i : int) = this.Array.[i]
and set (i : int) (t : 'T) = this.Array.[i] <- t
member this.Sort () =
Span(this.Array).Slice(0, this.Count).Sort ()
[<RequireQualifiedAccess>]
module ResizeArray =
let create<'T> (capacity : int) =
{
Array = Array.zeroCreate<'T> capacity
Length = 0
}

View File

@@ -23,22 +23,29 @@ module Program =
let sw = Stopwatch.StartNew ()
printfn "=====Day 1====="
Console.WriteLine "=====Day 1====="
do
sw.Restart ()
let input = Path.Combine (dir.FullName, "day1.txt") |> File.ReadAllText
let input =
try
Path.Combine (dir.FullName, "day1part1.txt") |> File.ReadAllText
with :? FileNotFoundException ->
Path.Combine (dir.FullName, "day1.txt") |> File.ReadAllText
let part1 = Day1.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 input = Path.Combine (dir.FullName, "day1.txt") |> File.ReadAllText
let part2 = Day1.part2 input
sw.Stop ()
Console.WriteLine (part2.ToString ())
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
printfn "=====Day 2====="
Console.WriteLine "=====Day 2====="
do
let input = Path.Combine (dir.FullName, "day2.txt") |> File.ReadAllText
@@ -53,7 +60,7 @@ module Program =
Console.WriteLine (part2.ToString ())
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
printfn "=====Day 3====="
Console.WriteLine "=====Day 3====="
do
let input = Path.Combine (dir.FullName, "day3.txt") |> File.ReadAllBytes
@@ -91,7 +98,7 @@ module Program =
Console.WriteLine (part2.ToString ())
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
printfn "=====Day 4====="
Console.WriteLine "=====Day 4====="
do
let input = Path.Combine (dir.FullName, "day4.txt") |> File.ReadAllText
@@ -106,7 +113,7 @@ module Program =
Console.WriteLine (part2.ToString ())
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
printfn "=====Day 5====="
Console.WriteLine "=====Day 5====="
do
let input = Path.Combine (dir.FullName, "day5.txt") |> File.ReadAllText
@@ -121,7 +128,7 @@ module Program =
Console.WriteLine (part2.ToString ())
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
printfn "=====Day 6====="
Console.WriteLine "=====Day 6====="
do
let input = Path.Combine (dir.FullName, "day6.txt") |> File.ReadAllText
@@ -136,6 +143,42 @@ module Program =
Console.WriteLine (part2.ToString ())
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
Console.WriteLine "=====Day 7====="
do
let input = Path.Combine (dir.FullName, "day7.txt") |> File.ReadAllText
sw.Restart ()
let part1 = Day7.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 = Day7.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 8====="
do
let input =
try
Path.Combine (dir.FullName, "day8part1.txt") |> File.ReadAllText
with :? FileNotFoundException ->
Path.Combine (dir.FullName, "day8.txt") |> File.ReadAllText
sw.Restart ()
let part1 = Day8.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 input = Path.Combine (dir.FullName, "day8.txt") |> File.ReadAllText
let part2 = Day8.part2 input
sw.Stop ()
Console.WriteLine (part2.ToString ())
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
endToEnd.Stop ()
Console.Error.WriteLine (

View File

@@ -15,6 +15,8 @@
<Compile Include="TestDay4.fs" />
<Compile Include="TestDay5.fs" />
<Compile Include="TestDay6.fs" />
<Compile Include="TestDay7.fs" />
<Compile Include="TestDay8.fs" />
<EmbeddedResource Include="samples\day1.txt" />
<EmbeddedResource Include="samples\day1part1.txt" />
<EmbeddedResource Include="samples\day2.txt" />
@@ -22,6 +24,9 @@
<EmbeddedResource Include="samples\day4.txt" />
<EmbeddedResource Include="samples\day5.txt" />
<EmbeddedResource Include="samples\day6.txt" />
<EmbeddedResource Include="samples\day7.txt" />
<EmbeddedResource Include="samples\day8part1.txt" />
<EmbeddedResource Include="samples\day8.txt" />
</ItemGroup>
<ItemGroup>

View File

@@ -0,0 +1,46 @@
namespace AdventOfCode2023.Test
open System
open AdventOfCode2023
open NUnit.Framework
open FsUnitTyped
open System.IO
[<TestFixture>]
module TestDay7 =
let sample = Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day7.txt"
[<Test>]
let part1Sample () =
sample |> Day7.part1 |> shouldEqual 6440ul
[<Test>]
let part2Sample () =
sample |> Day7.part2 |> shouldEqual 5905ul
[<Test>]
let part1Actual () =
let s =
try
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day7.txt"))
with
| :? DirectoryNotFoundException
| :? FileNotFoundException ->
Assert.Inconclusive ()
failwith "unreachable"
Day7.part1 s |> shouldEqual 250058342ul
[<Test>]
let part2Actual () =
let s =
try
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day7.txt"))
with
| :? DirectoryNotFoundException
| :? FileNotFoundException ->
Assert.Inconclusive ()
failwith "unreachable"
Day7.part2 s |> shouldEqual 250506580ul

View File

@@ -0,0 +1,57 @@
namespace AdventOfCode2023.Test
open AdventOfCode2023
open NUnit.Framework
open FsUnitTyped
open System.IO
[<TestFixture>]
module TestDay8 =
[<Test>]
let part1Sample () =
Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day8part1.txt"
|> Day8.part1
|> shouldEqual 2
[<Test>]
let part1Sample2 () =
"""LLR
AAA = (BBB, BBB)
BBB = (AAA, ZZZ)
ZZZ = (ZZZ, ZZZ)"""
|> Day8.part1
|> shouldEqual 6
[<Test>]
let part2Sample () =
Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day8.txt"
|> Day8.part2
|> shouldEqual 6uL
[<Test>]
let part1Actual () =
let s =
try
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day8.txt"))
with
| :? DirectoryNotFoundException
| :? FileNotFoundException ->
Assert.Inconclusive ()
failwith "unreachable"
Day8.part1 s |> shouldEqual 19199
[<Test>]
let part2Actual () =
let s =
try
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day8.txt"))
with
| :? DirectoryNotFoundException
| :? FileNotFoundException ->
Assert.Inconclusive ()
failwith "unreachable"
Day8.part2 s |> shouldEqual 13663968099527uL

View File

@@ -0,0 +1,5 @@
32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483

View File

@@ -0,0 +1,10 @@
LR
11A = (11B, XXX)
11B = (XXX, 11Z)
11Z = (11B, XXX)
22A = (22B, XXX)
22B = (22C, 22C)
22C = (22Z, 22Z)
22Z = (22B, 22B)
XXX = (XXX, XXX)

View File

@@ -0,0 +1,9 @@
RL
AAA = (BBB, CCC)
BBB = (DDD, EEE)
CCC = (ZZZ, GGG)
DDD = (DDD, DDD)
EEE = (EEE, EEE)
GGG = (GGG, GGG)
ZZZ = (ZZZ, ZZZ)

21
flake.lock generated
View File

@@ -18,6 +18,26 @@
"type": "github"
}
},
"nix-ld": {
"inputs": {
"nixpkgs": [
"nixpkgs"
]
},
"locked": {
"lastModified": 1701153607,
"narHash": "sha256-h+odOVyiGmEERMECoFOj5P7FPiMR8IPRzroFA4sKivg=",
"owner": "Mic92",
"repo": "nix-ld",
"rev": "bf5aa84a713c31d95b4307e442e966d6c7fd7ae7",
"type": "github"
},
"original": {
"owner": "Mic92",
"repo": "nix-ld",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1701253981,
@@ -37,6 +57,7 @@
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nix-ld": "nix-ld",
"nixpkgs": "nixpkgs"
}
},

View File

@@ -3,12 +3,17 @@
inputs = {
flake-utils.url = "github:numtide/flake-utils";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
nix-ld = {
url = "github:Mic92/nix-ld";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = {
self,
nixpkgs,
flake-utils,
nix-ld,
}:
flake-utils.lib.eachDefaultSystem (
system: let
@@ -25,9 +30,14 @@
pkgs.darwin.apple_sdk.frameworks.GSS
]
else [];
in let
deps = darwinDeps ++ [pkgs.zlib pkgs.zlib.dev pkgs.openssl pkgs.icu];
in {
devShells = {
default = pkgs.mkShell {
HOME = "/tmp/dotnet-home";
NUGET_PACKAGES = "/tmp/dotnet-home/.nuget/packages";
LINKER_PATH = "${pkgs.stdenv.cc}/nix-support/dynamic-linker";
buildInputs = with pkgs;
[
(with dotnetCorePackages;
@@ -36,8 +46,7 @@
dotnetPackages.Nuget
])
]
++ darwinDeps
++ [pkgs.zlib pkgs.zlib.dev pkgs.openssl pkgs.icu pkgs.alejandra];
++ [pkgs.alejandra pkgs.patchelf pkgs.strace];
};
};
}