Compare commits
18 Commits
786a7eba8e
...
woodpekcer
Author | SHA1 | Date | |
---|---|---|---|
|
71e6d8479a | ||
|
f31a856838 | ||
|
10e2602b50 | ||
|
729a205dc8 | ||
|
3499326730 | ||
|
b67efa2198 | ||
|
5c445e8175 | ||
|
a47d3e635e | ||
|
53e247d222 | ||
|
78680ccaf8 | ||
|
a0b9e50f50 | ||
|
2510873e5f | ||
|
9d81279173 | ||
|
7d1ba3e6a1 | ||
|
e8fa607279 | ||
d94663ae0e | |||
bcd2bb6349 | |||
2fbdf2c362 |
@@ -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"
|
||||
|
@@ -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>
|
||||
|
@@ -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
|
||||
}
|
@@ -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
|
||||
|
||||
|
@@ -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
|
||||
|
@@ -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]
|
||||
|
||||
|
@@ -54,8 +54,6 @@ module Day4 =
|
||||
|
||||
total
|
||||
|
||||
|
||||
|
||||
let part2 (s : string) =
|
||||
use lines = StringSplitEnumerator.make '\n' s
|
||||
let winningNumbers = ResizeArray<byte> ()
|
||||
|
215
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day7.fs
Normal file
215
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day7.fs
Normal 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
|
110
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day8.fs
Normal file
110
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day8.fs
Normal 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
|
@@ -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
|
||||
}
|
@@ -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 (
|
||||
|
@@ -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>
|
||||
|
46
AdventOfCode2023.FSharp/Test/TestDay7.fs
Normal file
46
AdventOfCode2023.FSharp/Test/TestDay7.fs
Normal 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
|
57
AdventOfCode2023.FSharp/Test/TestDay8.fs
Normal file
57
AdventOfCode2023.FSharp/Test/TestDay8.fs
Normal 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
|
5
AdventOfCode2023.FSharp/Test/samples/day7.txt
Normal file
5
AdventOfCode2023.FSharp/Test/samples/day7.txt
Normal file
@@ -0,0 +1,5 @@
|
||||
32T3K 765
|
||||
T55J5 684
|
||||
KK677 28
|
||||
KTJJT 220
|
||||
QQQJA 483
|
10
AdventOfCode2023.FSharp/Test/samples/day8.txt
Normal file
10
AdventOfCode2023.FSharp/Test/samples/day8.txt
Normal 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)
|
9
AdventOfCode2023.FSharp/Test/samples/day8part1.txt
Normal file
9
AdventOfCode2023.FSharp/Test/samples/day8part1.txt
Normal 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
21
flake.lock
generated
@@ -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"
|
||||
}
|
||||
},
|
||||
|
13
flake.nix
13
flake.nix
@@ -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];
|
||||
};
|
||||
};
|
||||
}
|
||||
|
Reference in New Issue
Block a user