Stop going insane
This commit is contained in:
@@ -7,6 +7,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="Arr2D.fs" />
|
||||
<Compile Include="ResizeArray.fs" />
|
||||
<Compile Include="EfficientString.fs" />
|
||||
<Compile Include="Day1.fs" />
|
||||
<Compile Include="Day2.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<char>) : RankedHand =
|
||||
let parseHand (tallyBuffer : ResizeArray<_>) (adjustJoker : bool) (s : ReadOnlySpan<char>) : 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<RankedHandAndBid> =
|
||||
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
|
||||
|
@@ -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 ()
|
||||
|
||||
[<RequireQualifiedAccess>]
|
||||
module ResizeArray =
|
||||
let create<'T> (capacity : int) = { Array = Array.zeroCreate<'T> capacity; Length = 0 }
|
Reference in New Issue
Block a user