Compare commits

..

2 Commits

Author SHA1 Message Date
Smaug123
6cdfc0d33c Faster
All checks were successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/all-checks-complete Pipeline was successful
ci/woodpecker/pr/build Pipeline was successful
ci/woodpecker/pr/all-checks-complete Pipeline was successful
2023-12-15 12:37:31 +00:00
Smaug123
9e2bfb8325 Faster 2023-12-15 12:32:15 +00:00
2 changed files with 43 additions and 17 deletions

View File

@@ -25,7 +25,7 @@ module Day15 =
answer
let removeFirst<'a> (toRemove : 'a -> bool) (arr : ResizeArray<'a>) : unit =
let inline removeFirst<'a> ([<InlineIfLambda>] toRemove : 'a -> bool) (arr : ResizeArray<'a>) : unit =
let mutable i = 0
while i < arr.Count do
@@ -38,7 +38,13 @@ module Day15 =
i <- i + 1
let replace (withKey : 'a -> 'key) (key : 'key) (value : 'a) (arr : ResizeArray<'a>) : unit =
let inline replace
([<InlineIfLambda>] withKey : 'a -> 'key)
(key : 'key)
(value : 'a)
(arr : ResizeArray<'a>)
: unit
=
let mutable i = 0
while i < arr.Count do
@@ -52,35 +58,57 @@ module Day15 =
// no replacement was made
arr.Add value
let inline focusingPower (boxNumber : int) (arr : ResizeArray<_ * _>) : int =
let mutable answer = 0
let inline getLength (labelAndLength : uint64) : uint32 =
(labelAndLength % uint64 UInt32.MaxValue) |> uint32
let inline getLabel (labelAndLength : uint64) : uint32 =
(labelAndLength / uint64 UInt32.MaxValue) |> uint32
let inline focusingPower (boxNumber : uint32) (arr : ResizeArray<_>) =
let mutable answer = 0ul
for i = 0 to arr.Count - 1 do
answer <- answer + (boxNumber + 1) * (i + 1) * snd arr.[i]
answer <- answer + (boxNumber + 1ul) * (uint32 i + 1ul) * getLength arr.[i]
answer
let inline toUint32 (s : ReadOnlySpan<char>) : uint32 =
let mutable answer = 0ul
for c in s do
answer <- answer * 26ul + uint32 (byte c - byte 'a')
answer
let inline pack (label : uint32) (focalLength : uint32) : uint64 =
uint64 label * uint64 UInt32.MaxValue + uint64 focalLength
let part2 (s : string) =
let s = s.AsSpan().TrimEnd ()
use chunks = StringSplitEnumerator.make' ',' s
let lenses = Array.init 256 (fun _ -> ResizeArray<string * _> ())
// The max length of a label turns out to be 6, which means we need 26^6 < 2^32 entries.
// So we'll use a uint32 instead of our string, to save hopping around memory.
// We'll also pack the focal length into the elements, to save tupling.
let lenses = Array.init 256 (fun _ -> ResizeArray<uint64> ())
for chunk in chunks do
if chunk.[chunk.Length - 1] = '-' then
let label = chunk.Slice(0, chunk.Length - 1).ToString ()
removeFirst (fun (label2, _focalLength) -> label2 = label) lenses.[hash (label.AsSpan ())]
let label = chunk.Slice (0, chunk.Length - 1)
let labelShrunk = toUint32 label
removeFirst (fun labelAndLength -> getLabel labelAndLength = labelShrunk) lenses.[hash label]
else
let equalsPos = chunk.IndexOf '='
let focalLength =
Int32.Parse (chunk.Slice (equalsPos + 1), NumberStyles.None, CultureInfo.InvariantCulture)
UInt32.Parse (chunk.Slice (equalsPos + 1), NumberStyles.None, CultureInfo.InvariantCulture)
let label = chunk.Slice(0, equalsPos).ToString ()
replace fst label (label, focalLength) lenses.[hash (label.AsSpan ())]
let label = chunk.Slice (0, equalsPos)
let labelShrunk = toUint32 label
replace getLabel labelShrunk (pack labelShrunk focalLength) lenses.[hash label]
let mutable answer = 0
let mutable answer = 0ul
for i = 0 to 255 do
answer <- answer + focusingPower i lenses.[i]
answer <- answer + focusingPower (uint32 i) lenses.[i]
answer

View File

@@ -1,7 +1,5 @@
namespace AdventOfCode2023.Test
open System
open AdventOfCode2023
open NUnit.Framework
open FsUnitTyped
@@ -19,7 +17,7 @@ module TestDay15 =
[<Test>]
let part2Sample () =
sample |> Day15.part2 |> shouldEqual 145
sample |> Day15.part2 |> shouldEqual 145ul
[<Test>]
let part1Actual () =
@@ -45,4 +43,4 @@ module TestDay15 =
Assert.Inconclusive ()
failwith "unreachable"
Day15.part2 s |> shouldEqual 248279
Day15.part2 s |> shouldEqual 248279ul