From 92c46cda5be412c80dadfcda43d4814a4d3b070e Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Thu, 7 Dec 2023 23:26:56 +0000 Subject: [PATCH] Stop going insane --- .../AdventOfCode2023.FSharp.Lib.fsproj | 1 + .../AdventOfCode2023.FSharp.Lib/Day7.fs | 18 +++++----- .../ResizeArray.fs | 33 +++++++++++++++++++ 3 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/ResizeArray.fs diff --git a/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/AdventOfCode2023.FSharp.Lib.fsproj b/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/AdventOfCode2023.FSharp.Lib.fsproj index 0c23d7a..f00e16b 100644 --- a/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/AdventOfCode2023.FSharp.Lib.fsproj +++ b/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/AdventOfCode2023.FSharp.Lib.fsproj @@ -7,6 +7,7 @@ + diff --git a/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day7.fs b/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day7.fs index 230ad03..5611048 100644 --- a/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day7.fs +++ b/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day7.fs @@ -2,6 +2,7 @@ namespace AdventOfCode2023 open System open System.Globalization +open AdventOfCode2023.ResizeArray type Hand = | Five = 6 @@ -72,7 +73,7 @@ module Day7 = + uint32 contents.Fourth * fourteen + uint32 contents.Fifth - let inline parseHand (tallyBuffer : ResizeArray<_>) (adjustJoker : bool) (s : ReadOnlySpan) : RankedHand = + let parseHand (tallyBuffer : ResizeArray<_>) (adjustJoker : bool) (s : ReadOnlySpan) : RankedHand = let contents = { First = toByte adjustJoker s.[0] @@ -94,14 +95,13 @@ module Day7 = 0, -1 else let mutable jokerCount = 0 - let mutable jokerPos = -1 - - for i = 0 to tallyBuffer.Count - 1 do - let card, tally = tallyBuffer.[i] - + let mutable jokerPos = 0 + while jokerPos < tallyBuffer.Count && jokerCount = 0 do + let card, tally = tallyBuffer.[jokerPos] if card = joker then jokerCount <- tally - jokerPos <- i + else + jokerPos <- jokerPos + 1 jokerCount, jokerPos @@ -171,8 +171,8 @@ module Day7 = let parse (adjustJoker : bool) (s : string) : ResizeArray = use mutable lines = StringSplitEnumerator.make '\n' s - let result = ResizeArray () - let tallies = ResizeArray 5 + let result = ResizeArray.create 4 + let tallies = ResizeArray.create 5 while lines.MoveNext () do if not lines.Current.IsEmpty then diff --git a/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/ResizeArray.fs b/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/ResizeArray.fs new file mode 100644 index 0000000..5b69e9c --- /dev/null +++ b/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/ResizeArray.fs @@ -0,0 +1,33 @@ +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 () + +[] +module ResizeArray = + let create<'T> (capacity : int) = { Array = Array.zeroCreate<'T> capacity; Length = 0 }