Day 11, super slow (#12)
Co-authored-by: Smaug123 <patrick+github@patrickstevens.co.uk> Reviewed-on: #12
This commit is contained in:
@@ -21,6 +21,7 @@
|
||||
<Compile Include="Day8.fs"/>
|
||||
<Compile Include="Day9.fs"/>
|
||||
<Compile Include="Day10.fs" />
|
||||
<Compile Include="Day11.fs" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
95
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day11.fs
Normal file
95
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day11.fs
Normal file
@@ -0,0 +1,95 @@
|
||||
namespace AdventOfCode2023
|
||||
|
||||
open System
|
||||
|
||||
[<RequireQualifiedAccess>]
|
||||
module Day11 =
|
||||
|
||||
type Data =
|
||||
{
|
||||
RowsWithoutGalaxies : ResizeArray<int>
|
||||
ColsWithoutGalaxies : ResizeArray<int>
|
||||
/// row * col
|
||||
Galaxies : ResizeArray<int * int>
|
||||
}
|
||||
|
||||
let parse (s : string) : Data =
|
||||
let galaxies = ResizeArray ()
|
||||
let rowsWithoutGalaxies = ResizeArray ()
|
||||
let mutable hasAnyGalaxy = false
|
||||
let mutable currRowIndex = 0
|
||||
let mutable currColIndex = 0
|
||||
|
||||
for c in s do
|
||||
if c = '\n' then
|
||||
if not hasAnyGalaxy then
|
||||
rowsWithoutGalaxies.Add currRowIndex
|
||||
|
||||
currRowIndex <- currRowIndex + 1
|
||||
currColIndex <- 0
|
||||
hasAnyGalaxy <- false
|
||||
elif c = '#' then
|
||||
hasAnyGalaxy <- true
|
||||
galaxies.Add (currRowIndex, currColIndex)
|
||||
currColIndex <- currColIndex + 1
|
||||
else
|
||||
currColIndex <- currColIndex + 1
|
||||
|
||||
galaxies.Sort (fun (_, c1) (_, c2) -> compare c1 c2)
|
||||
|
||||
let colsWithoutGalaxies =
|
||||
let result = ResizeArray ()
|
||||
let mutable prevCol = 0
|
||||
|
||||
for (_, c) in galaxies do
|
||||
if c > prevCol then
|
||||
for j = prevCol + 1 to c - 1 do
|
||||
result.Add j
|
||||
|
||||
prevCol <- c
|
||||
|
||||
result
|
||||
|
||||
{
|
||||
RowsWithoutGalaxies = rowsWithoutGalaxies
|
||||
ColsWithoutGalaxies = colsWithoutGalaxies
|
||||
Galaxies = galaxies
|
||||
}
|
||||
|
||||
let solve (data : Data) (expansion : uint64) =
|
||||
let mutable answer = 0uL
|
||||
|
||||
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 = 0uL
|
||||
|
||||
for i = 1 + min row1 row2 to max row1 row2 - 1 do
|
||||
if data.RowsWithoutGalaxies.Contains i then
|
||||
extraDistance <- extraDistance + expansion - 1uL
|
||||
|
||||
for i = 1 + min col1 col2 to max col1 col2 - 1 do
|
||||
if data.ColsWithoutGalaxies.Contains i then
|
||||
extraDistance <- extraDistance + expansion - 1uL
|
||||
|
||||
extraDistance
|
||||
|
||||
answer <- answer + extraDistance + baseDistance
|
||||
|
||||
answer
|
||||
|
||||
|
||||
let part1 (s : string) =
|
||||
let data = parse s
|
||||
|
||||
solve data 2uL
|
||||
|
||||
let part2 (s : string) =
|
||||
let data = parse s
|
||||
|
||||
solve data 1_000_000uL
|
@@ -211,6 +211,22 @@ module Program =
|
||||
Console.WriteLine (part2.ToString ())
|
||||
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
|
||||
|
||||
Console.WriteLine "=====Day 11====="
|
||||
|
||||
do
|
||||
let input = Path.Combine (dir.FullName, "day11.txt") |> File.ReadAllText
|
||||
|
||||
sw.Restart ()
|
||||
let part1 = Day11.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 = Day11.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 (
|
||||
|
@@ -19,6 +19,7 @@
|
||||
<Compile Include="TestDay8.fs"/>
|
||||
<Compile Include="TestDay9.fs"/>
|
||||
<Compile Include="TestDay10.fs" />
|
||||
<Compile Include="TestDay11.fs" />
|
||||
<EmbeddedResource Include="samples\day1.txt"/>
|
||||
<EmbeddedResource Include="samples\day1part1.txt"/>
|
||||
<EmbeddedResource Include="samples\day2.txt"/>
|
||||
@@ -32,6 +33,7 @@
|
||||
<EmbeddedResource Include="samples\day9.txt"/>
|
||||
<EmbeddedResource Include="samples\day10part1.txt" />
|
||||
<EmbeddedResource Include="samples\day10.txt" />
|
||||
<EmbeddedResource Include="samples\day11.txt" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
47
AdventOfCode2023.FSharp/Test/TestDay11.fs
Normal file
47
AdventOfCode2023.FSharp/Test/TestDay11.fs
Normal file
@@ -0,0 +1,47 @@
|
||||
namespace AdventOfCode2023.Test
|
||||
|
||||
open AdventOfCode2023
|
||||
open NUnit.Framework
|
||||
open FsUnitTyped
|
||||
open System.IO
|
||||
|
||||
[<TestFixture>]
|
||||
module TestDay11 =
|
||||
|
||||
let sample = Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day11.txt"
|
||||
|
||||
[<Test>]
|
||||
let part1Sample () =
|
||||
sample |> Day11.part1 |> shouldEqual 374uL
|
||||
|
||||
[<Test>]
|
||||
let part2Sample () =
|
||||
let data = sample |> Day11.parse
|
||||
Day11.solve data 10uL |> shouldEqual 1030uL
|
||||
Day11.solve data 100uL |> shouldEqual 8410uL
|
||||
|
||||
[<Test>]
|
||||
let part1Actual () =
|
||||
let s =
|
||||
try
|
||||
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day11.txt"))
|
||||
with
|
||||
| :? DirectoryNotFoundException
|
||||
| :? FileNotFoundException ->
|
||||
Assert.Inconclusive ()
|
||||
failwith "unreachable"
|
||||
|
||||
Day11.part1 s |> shouldEqual 9947476uL
|
||||
|
||||
[<Test>]
|
||||
let part2Actual () =
|
||||
let s =
|
||||
try
|
||||
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day11.txt"))
|
||||
with
|
||||
| :? DirectoryNotFoundException
|
||||
| :? FileNotFoundException ->
|
||||
Assert.Inconclusive ()
|
||||
failwith "unreachable"
|
||||
|
||||
Day11.part2 s |> shouldEqual 519939907614uL
|
@@ -15,10 +15,7 @@ module TestDay9 =
|
||||
sample |> Day9.part1 |> shouldEqual 114L
|
||||
|
||||
[<Test>]
|
||||
let part2Sample () =
|
||||
Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day9.txt"
|
||||
|> Day9.part2
|
||||
|> shouldEqual 2L
|
||||
let part2Sample () = sample |> Day9.part2 |> shouldEqual 2L
|
||||
|
||||
[<Test>]
|
||||
let part1Actual () =
|
||||
|
10
AdventOfCode2023.FSharp/Test/samples/day11.txt
Normal file
10
AdventOfCode2023.FSharp/Test/samples/day11.txt
Normal file
@@ -0,0 +1,10 @@
|
||||
...#......
|
||||
.......#..
|
||||
#.........
|
||||
..........
|
||||
......#...
|
||||
.#........
|
||||
.........#
|
||||
..........
|
||||
.......#..
|
||||
#...#.....
|
Reference in New Issue
Block a user