Day 15 (#17)
Co-authored-by: Smaug123 <patrick+github@patrickstevens.co.uk> Reviewed-on: #17
This commit is contained in:
@@ -14,6 +14,6 @@ module Inputs =
|
||||
if isNull dir then
|
||||
failwith "reached root of filesystem without finding inputs dir"
|
||||
|
||||
Array.init 14 (fun day -> Path.Combine (dir.FullName, "inputs", $"day%i{day + 1}.txt") |> File.ReadAllText)
|
||||
Array.init 15 (fun day -> Path.Combine (dir.FullName, "inputs", $"day%i{day + 1}.txt") |> File.ReadAllText)
|
||||
|
||||
let inline day (i : int) = days.[i - 1]
|
||||
|
@@ -44,7 +44,7 @@ module Benchmarks =
|
||||
[<GlobalSetup>]
|
||||
member _.Setup () = Run.shouldWrite <- false
|
||||
|
||||
[<Params(11, 12, 13, 14)>]
|
||||
[<Params(11, 12, 13, 14, 15)>]
|
||||
member val Day = 0 with get, set
|
||||
|
||||
[<Params(false, true)>]
|
||||
|
@@ -197,6 +197,18 @@ module Run =
|
||||
if shouldWrite then
|
||||
Console.WriteLine output
|
||||
|
||||
let day15 (partTwo : bool) (input : string) =
|
||||
if not partTwo then
|
||||
let output = Day15.part1 input
|
||||
|
||||
if shouldWrite then
|
||||
Console.WriteLine output
|
||||
else
|
||||
let output = Day15.part2 input
|
||||
|
||||
if shouldWrite then
|
||||
Console.WriteLine output
|
||||
|
||||
let allRuns =
|
||||
[|
|
||||
day1
|
||||
@@ -213,4 +225,5 @@ module Run =
|
||||
day12
|
||||
day13
|
||||
day14
|
||||
day15
|
||||
|]
|
||||
|
@@ -25,6 +25,7 @@
|
||||
<Compile Include="Day12.fs" />
|
||||
<Compile Include="Day13.fs" />
|
||||
<Compile Include="Day14.fs" />
|
||||
<Compile Include="Day15.fs" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
114
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day15.fs
Normal file
114
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day15.fs
Normal file
@@ -0,0 +1,114 @@
|
||||
namespace AdventOfCode2023
|
||||
|
||||
open System
|
||||
open System.Globalization
|
||||
|
||||
[<RequireQualifiedAccess>]
|
||||
module Day15 =
|
||||
|
||||
let hash (s : ReadOnlySpan<char>) : int =
|
||||
let mutable v = 0
|
||||
|
||||
for c in s do
|
||||
v <- v + int (byte c)
|
||||
v <- (17 * v) % 256
|
||||
|
||||
v
|
||||
|
||||
let part1 (s : string) =
|
||||
let s = s.AsSpan().TrimEnd ()
|
||||
use chunks = StringSplitEnumerator.make' ',' s
|
||||
let mutable answer = 0
|
||||
|
||||
for chunk in chunks do
|
||||
answer <- answer + hash chunk
|
||||
|
||||
answer
|
||||
|
||||
let inline removeFirst<'a> ([<InlineIfLambda>] toRemove : 'a -> bool) (arr : ResizeArray<'a>) : unit =
|
||||
let mutable i = 0
|
||||
|
||||
while i < arr.Count do
|
||||
if toRemove arr.[i] then
|
||||
for j = i to arr.Count - 2 do
|
||||
arr.[j] <- arr.[j + 1]
|
||||
|
||||
arr.RemoveAt (arr.Count - 1)
|
||||
i <- arr.Count
|
||||
|
||||
i <- i + 1
|
||||
|
||||
let inline replace
|
||||
([<InlineIfLambda>] withKey : 'a -> 'key)
|
||||
(key : 'key)
|
||||
(value : 'a)
|
||||
(arr : ResizeArray<'a>)
|
||||
: unit
|
||||
=
|
||||
let mutable i = 0
|
||||
|
||||
while i < arr.Count do
|
||||
if withKey arr.[i] = key then
|
||||
arr.[i] <- value
|
||||
i <- arr.Count
|
||||
|
||||
i <- i + 1
|
||||
|
||||
if i < arr.Count + 1 then
|
||||
// no replacement was made
|
||||
arr.Add value
|
||||
|
||||
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 + 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
|
||||
// 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)
|
||||
let labelShrunk = toUint32 label
|
||||
removeFirst (fun labelAndLength -> getLabel labelAndLength = labelShrunk) lenses.[hash label]
|
||||
else
|
||||
let equalsPos = chunk.IndexOf '='
|
||||
|
||||
let focalLength =
|
||||
UInt32.Parse (chunk.Slice (equalsPos + 1), NumberStyles.None, CultureInfo.InvariantCulture)
|
||||
|
||||
let label = chunk.Slice (0, equalsPos)
|
||||
let labelShrunk = toUint32 label
|
||||
replace getLabel labelShrunk (pack labelShrunk focalLength) lenses.[hash label]
|
||||
|
||||
let mutable answer = 0ul
|
||||
|
||||
for i = 0 to 255 do
|
||||
answer <- answer + focusingPower (uint32 i) lenses.[i]
|
||||
|
||||
answer
|
@@ -284,6 +284,22 @@ module Program =
|
||||
Console.WriteLine (part2.ToString ())
|
||||
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
|
||||
|
||||
Console.WriteLine "=====Day 15====="
|
||||
|
||||
do
|
||||
let input = Path.Combine (dir.FullName, "day15.txt") |> File.ReadAllText
|
||||
|
||||
sw.Restart ()
|
||||
let part1 = Day15.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 = Day15.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 (
|
||||
|
@@ -23,6 +23,7 @@
|
||||
<Compile Include="TestDay12.fs" />
|
||||
<Compile Include="TestDay13.fs" />
|
||||
<Compile Include="TestDay14.fs" />
|
||||
<Compile Include="TestDay15.fs" />
|
||||
<EmbeddedResource Include="samples\day1.txt"/>
|
||||
<EmbeddedResource Include="samples\day1part1.txt"/>
|
||||
<EmbeddedResource Include="samples\day2.txt"/>
|
||||
@@ -40,6 +41,7 @@
|
||||
<EmbeddedResource Include="samples\day12.txt" />
|
||||
<EmbeddedResource Include="samples\day13.txt" />
|
||||
<EmbeddedResource Include="samples\day14.txt" />
|
||||
<EmbeddedResource Include="samples\day15.txt" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
46
AdventOfCode2023.FSharp/Test/TestDay15.fs
Normal file
46
AdventOfCode2023.FSharp/Test/TestDay15.fs
Normal file
@@ -0,0 +1,46 @@
|
||||
namespace AdventOfCode2023.Test
|
||||
|
||||
open AdventOfCode2023
|
||||
open NUnit.Framework
|
||||
open FsUnitTyped
|
||||
open System.IO
|
||||
|
||||
[<TestFixture>]
|
||||
module TestDay15 =
|
||||
|
||||
[<Test>]
|
||||
let sample = Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day15.txt"
|
||||
|
||||
[<Test>]
|
||||
let part1Sample () =
|
||||
sample |> Day15.part1 |> shouldEqual 1320
|
||||
|
||||
[<Test>]
|
||||
let part2Sample () =
|
||||
sample |> Day15.part2 |> shouldEqual 145ul
|
||||
|
||||
[<Test>]
|
||||
let part1Actual () =
|
||||
let s =
|
||||
try
|
||||
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day15.txt"))
|
||||
with
|
||||
| :? DirectoryNotFoundException
|
||||
| :? FileNotFoundException ->
|
||||
Assert.Inconclusive ()
|
||||
failwith "unreachable"
|
||||
|
||||
Day15.part1 s |> shouldEqual 521434
|
||||
|
||||
[<Test>]
|
||||
let part2Actual () =
|
||||
let s =
|
||||
try
|
||||
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day15.txt"))
|
||||
with
|
||||
| :? DirectoryNotFoundException
|
||||
| :? FileNotFoundException ->
|
||||
Assert.Inconclusive ()
|
||||
failwith "unreachable"
|
||||
|
||||
Day15.part2 s |> shouldEqual 248279ul
|
1
AdventOfCode2023.FSharp/Test/samples/day15.txt
Normal file
1
AdventOfCode2023.FSharp/Test/samples/day15.txt
Normal file
@@ -0,0 +1 @@
|
||||
rn=1,cm-,qp=3,cm=2,qp-,pc=4,ot=9,ab=5,pc-,pc=6,ot=7
|
Reference in New Issue
Block a user