Day 11 part 2
This commit is contained in:
@@ -5,17 +5,17 @@ open System
|
|||||||
[<RequireQualifiedAccess>]
|
[<RequireQualifiedAccess>]
|
||||||
module Day11 =
|
module Day11 =
|
||||||
|
|
||||||
let inline private toRowAndCol (lineLength : int) (pos : int) : struct (int * int) =
|
type Data =
|
||||||
let lineNum = pos / lineLength
|
{
|
||||||
let withinLine = pos % lineLength
|
RowsWithoutGalaxies : ResizeArray<int>
|
||||||
struct (lineNum, withinLine)
|
ColsWithoutGalaxies : ResizeArray<int>
|
||||||
|
/// row * col
|
||||||
|
Galaxies : ResizeArray<int * int>
|
||||||
|
}
|
||||||
|
|
||||||
let inline private ofRowAndCol (lineLength : int) (lineNum : int) (col : int) : int = lineNum * lineLength + col
|
let parse (s : string) : Data =
|
||||||
|
|
||||||
let part1 (s : string) =
|
|
||||||
let galaxies = ResizeArray ()
|
let galaxies = ResizeArray ()
|
||||||
let rowsWithoutGalaxies = ResizeArray ()
|
let rowsWithoutGalaxies = ResizeArray ()
|
||||||
let rowLength = s.IndexOf '\n'
|
|
||||||
let mutable hasAnyGalaxy = false
|
let mutable hasAnyGalaxy = false
|
||||||
let mutable currRowIndex = 0
|
let mutable currRowIndex = 0
|
||||||
let mutable currColIndex = 0
|
let mutable currColIndex = 0
|
||||||
@@ -50,25 +50,32 @@ module Day11 =
|
|||||||
|
|
||||||
result
|
result
|
||||||
|
|
||||||
let mutable answer = 0
|
{
|
||||||
|
RowsWithoutGalaxies = rowsWithoutGalaxies
|
||||||
|
ColsWithoutGalaxies = colsWithoutGalaxies
|
||||||
|
Galaxies = galaxies
|
||||||
|
}
|
||||||
|
|
||||||
for galaxy1 = 0 to galaxies.Count - 1 do
|
let solve (data : Data) (expansion : uint64) =
|
||||||
let row1, col1 = galaxies.[galaxy1]
|
let mutable answer = 0uL
|
||||||
|
|
||||||
for galaxy2 = galaxy1 + 1 to galaxies.Count - 1 do
|
for galaxy1 = 0 to data.Galaxies.Count - 1 do
|
||||||
let row2, col2 = galaxies.[galaxy2]
|
let row1, col1 = data.Galaxies.[galaxy1]
|
||||||
let baseDistance = abs (row1 - row2) + abs (col1 - col2)
|
|
||||||
|
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 extraDistance =
|
||||||
let mutable extraDistance = 0
|
let mutable extraDistance = 0uL
|
||||||
|
|
||||||
for i = 1 + min row1 row2 to max row1 row2 - 1 do
|
for i = 1 + min row1 row2 to max row1 row2 - 1 do
|
||||||
if rowsWithoutGalaxies.Contains i then
|
if data.RowsWithoutGalaxies.Contains i then
|
||||||
extraDistance <- extraDistance + 1
|
extraDistance <- extraDistance + expansion - 1uL
|
||||||
|
|
||||||
for i = 1 + min col1 col2 to max col1 col2 - 1 do
|
for i = 1 + min col1 col2 to max col1 col2 - 1 do
|
||||||
if colsWithoutGalaxies.Contains i then
|
if data.ColsWithoutGalaxies.Contains i then
|
||||||
extraDistance <- extraDistance + 1
|
extraDistance <- extraDistance + expansion - 1uL
|
||||||
|
|
||||||
extraDistance
|
extraDistance
|
||||||
|
|
||||||
@@ -76,6 +83,13 @@ module Day11 =
|
|||||||
|
|
||||||
answer
|
answer
|
||||||
|
|
||||||
|
|
||||||
|
let part1 (s : string) =
|
||||||
|
let data = parse s
|
||||||
|
|
||||||
|
solve data 2uL
|
||||||
|
|
||||||
let part2 (s : string) =
|
let part2 (s : string) =
|
||||||
use s = StringSplitEnumerator.make '\n' s
|
let data = parse s
|
||||||
0
|
|
||||||
|
solve data 1_000_000uL
|
||||||
|
@@ -12,10 +12,13 @@ module TestDay11 =
|
|||||||
|
|
||||||
[<Test>]
|
[<Test>]
|
||||||
let part1Sample () =
|
let part1Sample () =
|
||||||
sample |> Day11.part1 |> shouldEqual 374
|
sample |> Day11.part1 |> shouldEqual 374uL
|
||||||
|
|
||||||
[<Test>]
|
[<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>]
|
[<Test>]
|
||||||
let part1Actual () =
|
let part1Actual () =
|
||||||
@@ -28,7 +31,7 @@ module TestDay11 =
|
|||||||
Assert.Inconclusive ()
|
Assert.Inconclusive ()
|
||||||
failwith "unreachable"
|
failwith "unreachable"
|
||||||
|
|
||||||
Day11.part1 s |> shouldEqual 9947476
|
Day11.part1 s |> shouldEqual 9947476uL
|
||||||
|
|
||||||
[<Test>]
|
[<Test>]
|
||||||
let part2Actual () =
|
let part2Actual () =
|
||||||
@@ -41,4 +44,4 @@ module TestDay11 =
|
|||||||
Assert.Inconclusive ()
|
Assert.Inconclusive ()
|
||||||
failwith "unreachable"
|
failwith "unreachable"
|
||||||
|
|
||||||
Day11.part2 s |> shouldEqual -1
|
Day11.part2 s |> shouldEqual 519939907614uL
|
||||||
|
Reference in New Issue
Block a user