Day 11 part 2
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

This commit is contained in:
Smaug123
2023-12-11 08:49:36 +00:00
parent 4757ca4753
commit 16053d1a51
2 changed files with 42 additions and 25 deletions

View File

@@ -5,17 +5,17 @@ open System
[<RequireQualifiedAccess>]
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<int>
ColsWithoutGalaxies : ResizeArray<int>
/// row * col
Galaxies : ResizeArray<int * int>
}
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

View File

@@ -12,10 +12,13 @@ module TestDay11 =
[<Test>]
let part1Sample () =
sample |> Day11.part1 |> shouldEqual 374
sample |> Day11.part1 |> shouldEqual 374uL
[<Test>]
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
[<Test>]
let part1Actual () =
@@ -28,7 +31,7 @@ module TestDay11 =
Assert.Inconclusive ()
failwith "unreachable"
Day11.part1 s |> shouldEqual 9947476
Day11.part1 s |> shouldEqual 9947476uL
[<Test>]
let part2Actual () =
@@ -41,4 +44,4 @@ module TestDay11 =
Assert.Inconclusive ()
failwith "unreachable"
Day11.part2 s |> shouldEqual -1
Day11.part2 s |> shouldEqual 519939907614uL