Fit in uint32
This commit is contained in:
@@ -24,13 +24,16 @@ type HandContents =
|
||||
[<RequireQualifiedAccess>]
|
||||
module Day7 =
|
||||
|
||||
[<Literal>]
|
||||
let joker = 0uy
|
||||
|
||||
let inline toByte (adjustJoker : bool) (c : char) : byte =
|
||||
if c <= '9' then byte c - byte '0'
|
||||
elif c = 'T' then 10uy
|
||||
elif c = 'J' then (if adjustJoker then 1uy else 11uy)
|
||||
elif c = 'Q' then 12uy
|
||||
elif c = 'K' then 13uy
|
||||
elif c = 'A' then 14uy
|
||||
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 =
|
||||
@@ -44,10 +47,10 @@ module Day7 =
|
||||
if not isAdded then
|
||||
tallies.Add (newNum, 1)
|
||||
|
||||
type RankedHand = uint64
|
||||
type RankedHand = uint32
|
||||
|
||||
[<Literal>]
|
||||
let fifteen = 15uL
|
||||
let fifteen = 14ul
|
||||
|
||||
[<Literal>]
|
||||
let fifteenFive = fifteen * fifteen * fifteen * fifteen * fifteen
|
||||
@@ -61,16 +64,13 @@ module Day7 =
|
||||
[<Literal>]
|
||||
let fifteenTwo = fifteen * fifteen
|
||||
|
||||
let inline toInt (hand : Hand) (contents : HandContents) : RankedHand =
|
||||
// match hand with
|
||||
// | Hand.Five -> UInt32.MaxValue - 15ul + uint32 contents.First
|
||||
// | _ ->
|
||||
uint64 hand * fifteenFive
|
||||
+ uint64 contents.First * fifteenFour
|
||||
+ uint64 contents.Second * fifteenThree
|
||||
+ uint64 contents.Third * fifteenTwo
|
||||
+ uint64 contents.Fourth * fifteen
|
||||
+ uint64 contents.Fifth
|
||||
let toInt (hand : Hand) (contents : HandContents) : RankedHand =
|
||||
uint32 hand * fifteenFive
|
||||
+ uint32 contents.First * fifteenFour
|
||||
+ uint32 contents.Second * fifteenThree
|
||||
+ uint32 contents.Third * fifteenTwo
|
||||
+ uint32 contents.Fourth * fifteen
|
||||
+ uint32 contents.Fifth
|
||||
|
||||
let inline parseHand (tallyBuffer : ResizeArray<_>) (adjustJoker : bool) (s : ReadOnlySpan<char>) : RankedHand =
|
||||
let contents =
|
||||
@@ -93,17 +93,17 @@ module Day7 =
|
||||
if not adjustJoker then
|
||||
0, -1
|
||||
else
|
||||
let mutable count = 0
|
||||
let mutable jokerCount = 0
|
||||
let mutable jokerPos = -1
|
||||
|
||||
for i = 0 to tallyBuffer.Count - 1 do
|
||||
let card, tally = tallyBuffer.[i]
|
||||
|
||||
if card = 1uy then
|
||||
count <- tally
|
||||
if card = joker then
|
||||
jokerCount <- tally
|
||||
jokerPos <- i
|
||||
|
||||
count, jokerPos
|
||||
jokerCount, jokerPos
|
||||
|
||||
let hand =
|
||||
if jokerCount > 0 then
|
||||
@@ -160,11 +160,14 @@ module Day7 =
|
||||
|
||||
toInt hand contents
|
||||
|
||||
type RankedHandAndBid = uint64
|
||||
type RankedHandAndBid = uint32
|
||||
|
||||
let inline toRankedHandAndBid (r : RankedHand) (bid : uint64) : RankedHandAndBid = 1001uL * r + bid
|
||||
[<Literal>]
|
||||
let bidSeparator = 1001ul
|
||||
|
||||
let inline getBid (r : RankedHandAndBid) : uint64 = uint64 (r % 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
|
||||
@@ -179,7 +182,7 @@ module Day7 =
|
||||
line.MoveNext () |> ignore
|
||||
|
||||
let bid =
|
||||
UInt64.Parse (line.Current, NumberStyles.Integer, CultureInfo.InvariantCulture)
|
||||
UInt32.Parse (line.Current, NumberStyles.Integer, CultureInfo.InvariantCulture)
|
||||
|
||||
result.Add (toRankedHandAndBid rankedHand bid)
|
||||
|
||||
@@ -190,10 +193,10 @@ module Day7 =
|
||||
|
||||
arr.Sort ()
|
||||
|
||||
let mutable answer = 0uL
|
||||
let mutable answer = 0ul
|
||||
|
||||
for i = 0 to arr.Count - 1 do
|
||||
answer <- answer + getBid arr.[i] * (uint64 i + 1uL)
|
||||
answer <- answer + getBid arr.[i] * (uint32 i + 1ul)
|
||||
|
||||
answer
|
||||
|
||||
@@ -202,9 +205,9 @@ module Day7 =
|
||||
|
||||
arr.Sort ()
|
||||
|
||||
let mutable answer = 0uL
|
||||
let mutable answer = 0ul
|
||||
|
||||
for i = 0 to arr.Count - 1 do
|
||||
answer <- answer + getBid arr.[i] * (uint64 i + 1uL)
|
||||
answer <- answer + getBid arr.[i] * (uint32 i + 1ul)
|
||||
|
||||
answer
|
||||
|
@@ -13,11 +13,11 @@ module TestDay7 =
|
||||
|
||||
[<Test>]
|
||||
let part1Sample () =
|
||||
sample |> Day7.part1 |> shouldEqual 6440uL
|
||||
sample |> Day7.part1 |> shouldEqual 6440ul
|
||||
|
||||
[<Test>]
|
||||
let part2Sample () =
|
||||
sample |> Day7.part2 |> shouldEqual 5905uL
|
||||
sample |> Day7.part2 |> shouldEqual 5905ul
|
||||
|
||||
[<Test>]
|
||||
let part1Actual () =
|
||||
@@ -30,7 +30,7 @@ module TestDay7 =
|
||||
Assert.Inconclusive ()
|
||||
failwith "unreachable"
|
||||
|
||||
Day7.part1 s |> shouldEqual 250058342uL
|
||||
Day7.part1 s |> shouldEqual 250058342ul
|
||||
|
||||
[<Test>]
|
||||
let part2Actual () =
|
||||
@@ -43,4 +43,4 @@ module TestDay7 =
|
||||
Assert.Inconclusive ()
|
||||
failwith "unreachable"
|
||||
|
||||
Day7.part2 s |> shouldEqual 250506580uL
|
||||
Day7.part2 s |> shouldEqual 250506580ul
|
||||
|
Reference in New Issue
Block a user