From 16053d1a513d1993fc492aa2458b2e511ce155a4 Mon Sep 17 00:00:00 2001 From: Smaug123 Date: Mon, 11 Dec 2023 08:49:36 +0000 Subject: [PATCH] Day 11 part 2 --- .../AdventOfCode2023.FSharp.Lib/Day11.fs | 56 ++++++++++++------- AdventOfCode2023.FSharp/Test/TestDay11.fs | 11 ++-- 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day11.fs b/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day11.fs index 6c8c4d6..4fde534 100644 --- a/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day11.fs +++ b/AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day11.fs @@ -5,17 +5,17 @@ open System [] module Day11 = - let inline private toRowAndCol (lineLength : int) (pos : int) : struct (int * int) = - let lineNum = pos / lineLength - let withinLine = pos % lineLength - struct (lineNum, withinLine) + type Data = + { + RowsWithoutGalaxies : ResizeArray + ColsWithoutGalaxies : ResizeArray + /// row * col + Galaxies : ResizeArray + } - let inline private ofRowAndCol (lineLength : int) (lineNum : int) (col : int) : int = lineNum * lineLength + col - - let part1 (s : string) = + let parse (s : string) : Data = let galaxies = ResizeArray () let rowsWithoutGalaxies = ResizeArray () - let rowLength = s.IndexOf '\n' let mutable hasAnyGalaxy = false let mutable currRowIndex = 0 let mutable currColIndex = 0 @@ -50,25 +50,32 @@ module Day11 = result - let mutable answer = 0 + { + RowsWithoutGalaxies = rowsWithoutGalaxies + ColsWithoutGalaxies = colsWithoutGalaxies + Galaxies = galaxies + } - for galaxy1 = 0 to galaxies.Count - 1 do - let row1, col1 = galaxies.[galaxy1] + let solve (data : Data) (expansion : uint64) = + let mutable answer = 0uL - for galaxy2 = galaxy1 + 1 to galaxies.Count - 1 do - let row2, col2 = galaxies.[galaxy2] - let baseDistance = abs (row1 - row2) + abs (col1 - col2) + for galaxy1 = 0 to data.Galaxies.Count - 1 do + let row1, col1 = data.Galaxies.[galaxy1] + + for galaxy2 = galaxy1 + 1 to data.Galaxies.Count - 1 do + let row2, col2 = data.Galaxies.[galaxy2] + let baseDistance = uint64 (abs (row1 - row2) + abs (col1 - col2)) let extraDistance = - let mutable extraDistance = 0 + let mutable extraDistance = 0uL for i = 1 + min row1 row2 to max row1 row2 - 1 do - if rowsWithoutGalaxies.Contains i then - extraDistance <- extraDistance + 1 + if data.RowsWithoutGalaxies.Contains i then + extraDistance <- extraDistance + expansion - 1uL for i = 1 + min col1 col2 to max col1 col2 - 1 do - if colsWithoutGalaxies.Contains i then - extraDistance <- extraDistance + 1 + if data.ColsWithoutGalaxies.Contains i then + extraDistance <- extraDistance + expansion - 1uL extraDistance @@ -76,6 +83,13 @@ module Day11 = answer + + let part1 (s : string) = + let data = parse s + + solve data 2uL + let part2 (s : string) = - use s = StringSplitEnumerator.make '\n' s - 0 + let data = parse s + + solve data 1_000_000uL diff --git a/AdventOfCode2023.FSharp/Test/TestDay11.fs b/AdventOfCode2023.FSharp/Test/TestDay11.fs index ffc65c6..3fad587 100644 --- a/AdventOfCode2023.FSharp/Test/TestDay11.fs +++ b/AdventOfCode2023.FSharp/Test/TestDay11.fs @@ -12,10 +12,13 @@ module TestDay11 = [] let part1Sample () = - sample |> Day11.part1 |> shouldEqual 374 + sample |> Day11.part1 |> shouldEqual 374uL [] - let part2Sample () = sample |> Day11.part2 |> shouldEqual -1 + let part2Sample () = + let data = sample |> Day11.parse + Day11.solve data 10uL |> shouldEqual 1030uL + Day11.solve data 100uL |> shouldEqual 8410uL [] let part1Actual () = @@ -28,7 +31,7 @@ module TestDay11 = Assert.Inconclusive () failwith "unreachable" - Day11.part1 s |> shouldEqual 9947476 + Day11.part1 s |> shouldEqual 9947476uL [] let part2Actual () = @@ -41,4 +44,4 @@ module TestDay11 = Assert.Inconclusive () failwith "unreachable" - Day11.part2 s |> shouldEqual -1 + Day11.part2 s |> shouldEqual 519939907614uL