Compare commits
2 Commits
main
...
3da77f08c3
Author | SHA1 | Date | |
---|---|---|---|
|
3da77f08c3 | ||
|
fd90f94653 |
@@ -31,6 +31,7 @@
|
|||||||
<Compile Include="Day14.fs" />
|
<Compile Include="Day14.fs" />
|
||||||
<Compile Include="Day15.fs" />
|
<Compile Include="Day15.fs" />
|
||||||
<Compile Include="Day16.fs" />
|
<Compile Include="Day16.fs" />
|
||||||
|
<Compile Include="Day18.fs" />
|
||||||
<Compile Include="Day19.fs" />
|
<Compile Include="Day19.fs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
103
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day18.fs
Normal file
103
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day18.fs
Normal file
@@ -0,0 +1,103 @@
|
|||||||
|
namespace AdventOfCode2023
|
||||||
|
|
||||||
|
#if DEBUG
|
||||||
|
#else
|
||||||
|
#nowarn "9"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
open System
|
||||||
|
open System.Collections.Generic
|
||||||
|
open System.Globalization
|
||||||
|
|
||||||
|
[<RequireQualifiedAccess>]
|
||||||
|
module Day18 =
|
||||||
|
|
||||||
|
let part1 (s : string) =
|
||||||
|
// Interleaved row and col
|
||||||
|
let mutable weHave = ResizeArray<int> ()
|
||||||
|
weHave.Add 0
|
||||||
|
weHave.Add 0
|
||||||
|
|
||||||
|
let mutable minCol = 0
|
||||||
|
let mutable maxCol = 0
|
||||||
|
let mutable minRow = 0
|
||||||
|
let mutable maxRow = 0
|
||||||
|
|
||||||
|
let mutable col = 0
|
||||||
|
let mutable row = 0
|
||||||
|
let mutable edgeCount = 0
|
||||||
|
use rows = StringSplitEnumerator.make '\n' s
|
||||||
|
|
||||||
|
for currRow in rows do
|
||||||
|
use mutable entries = StringSplitEnumerator.make' ' ' currRow
|
||||||
|
entries.MoveNext () |> ignore
|
||||||
|
let dir = Direction.ofChar entries.Current.[0]
|
||||||
|
entries.MoveNext () |> ignore
|
||||||
|
|
||||||
|
let distance =
|
||||||
|
Int32.Parse (entries.Current, NumberStyles.None, CultureInfo.InvariantCulture)
|
||||||
|
|
||||||
|
edgeCount <- edgeCount + distance
|
||||||
|
|
||||||
|
match dir with
|
||||||
|
| Direction.Down ->
|
||||||
|
for _ = 1 to distance do
|
||||||
|
row <- row + 1
|
||||||
|
weHave.Add row
|
||||||
|
weHave.Add col
|
||||||
|
|
||||||
|
maxRow <- max row maxRow
|
||||||
|
| Direction.Up ->
|
||||||
|
for _ = 1 to distance do
|
||||||
|
row <- row - 1
|
||||||
|
weHave.Add row
|
||||||
|
weHave.Add col
|
||||||
|
|
||||||
|
minRow <- min row minRow
|
||||||
|
| Direction.Left ->
|
||||||
|
for _ = 1 to distance do
|
||||||
|
col <- col - 1
|
||||||
|
weHave.Add row
|
||||||
|
weHave.Add col
|
||||||
|
|
||||||
|
minCol <- min col minCol
|
||||||
|
| Direction.Right ->
|
||||||
|
for _ = 1 to distance do
|
||||||
|
col <- col + 1
|
||||||
|
weHave.Add row
|
||||||
|
weHave.Add col
|
||||||
|
|
||||||
|
maxCol <- max col maxCol
|
||||||
|
| _ -> failwith "bad dir"
|
||||||
|
|
||||||
|
let buffer = Array.zeroCreate ((maxRow - minRow + 3) * (maxCol - minCol + 3))
|
||||||
|
#if DEBUG
|
||||||
|
let system : Arr2D<byte> =
|
||||||
|
{
|
||||||
|
Elements = buffer
|
||||||
|
Width = maxCol - minCol + 3
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
use ptr = fixed buffer
|
||||||
|
|
||||||
|
let system : Arr2D<byte> =
|
||||||
|
{
|
||||||
|
Elements = ptr
|
||||||
|
Length = buffer.Length
|
||||||
|
Width = maxCol - minCol + 3
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
let mutable pointIndex = 0
|
||||||
|
|
||||||
|
while pointIndex < weHave.Count do
|
||||||
|
let row = weHave.[pointIndex]
|
||||||
|
let col = weHave.[pointIndex + 1]
|
||||||
|
Arr2D.set system (col - minCol + 1) (row - minRow + 1) 1uy
|
||||||
|
pointIndex <- pointIndex + 2
|
||||||
|
|
||||||
|
Arr2D.floodFill (ResizeArray ()) system 0uy 2uy 0 0
|
||||||
|
|
||||||
|
Arr2D.count system 0uy + edgeCount
|
||||||
|
|
||||||
|
let part2 (s : string) = -1
|
@@ -316,6 +316,22 @@ module Program =
|
|||||||
Console.WriteLine (part2.ToString ())
|
Console.WriteLine (part2.ToString ())
|
||||||
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
|
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
|
||||||
|
|
||||||
|
Console.WriteLine "=====Day 18====="
|
||||||
|
|
||||||
|
do
|
||||||
|
let input = Path.Combine (dir.FullName, "day18.txt") |> File.ReadAllText
|
||||||
|
|
||||||
|
sw.Restart ()
|
||||||
|
let part1 = Day18.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 = Day18.part2 input
|
||||||
|
sw.Stop ()
|
||||||
|
Console.WriteLine (part2.ToString ())
|
||||||
|
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
|
||||||
|
|
||||||
Console.WriteLine "=====Day 19====="
|
Console.WriteLine "=====Day 19====="
|
||||||
|
|
||||||
do
|
do
|
||||||
|
@@ -30,6 +30,7 @@
|
|||||||
<Compile Include="TestDay14.fs" />
|
<Compile Include="TestDay14.fs" />
|
||||||
<Compile Include="TestDay15.fs" />
|
<Compile Include="TestDay15.fs" />
|
||||||
<Compile Include="TestDay16.fs" />
|
<Compile Include="TestDay16.fs" />
|
||||||
|
<Compile Include="TestDay18.fs" />
|
||||||
<Compile Include="TestDay19.fs" />
|
<Compile Include="TestDay19.fs" />
|
||||||
<EmbeddedResource Include="samples\day1.txt"/>
|
<EmbeddedResource Include="samples\day1.txt"/>
|
||||||
<EmbeddedResource Include="samples\day1part1.txt"/>
|
<EmbeddedResource Include="samples\day1part1.txt"/>
|
||||||
@@ -50,6 +51,7 @@
|
|||||||
<EmbeddedResource Include="samples\day14.txt" />
|
<EmbeddedResource Include="samples\day14.txt" />
|
||||||
<EmbeddedResource Include="samples\day15.txt" />
|
<EmbeddedResource Include="samples\day15.txt" />
|
||||||
<EmbeddedResource Include="samples\day16.txt" />
|
<EmbeddedResource Include="samples\day16.txt" />
|
||||||
|
<EmbeddedResource Include="samples\day18.txt" />
|
||||||
<EmbeddedResource Include="samples\day19.txt" />
|
<EmbeddedResource Include="samples\day19.txt" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
44
AdventOfCode2023.FSharp/Test/TestDay18.fs
Normal file
44
AdventOfCode2023.FSharp/Test/TestDay18.fs
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
namespace AdventOfCode2023.Test
|
||||||
|
|
||||||
|
open AdventOfCode2023
|
||||||
|
open NUnit.Framework
|
||||||
|
open FsUnitTyped
|
||||||
|
open System.IO
|
||||||
|
|
||||||
|
[<TestFixture>]
|
||||||
|
module TestDay18 =
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let sample = Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day18.txt"
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part1Sample () = sample |> Day18.part1 |> shouldEqual 62
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Sample () = sample |> Day18.part2 |> shouldEqual 0
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part1Actual () =
|
||||||
|
let s =
|
||||||
|
try
|
||||||
|
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day18.txt"))
|
||||||
|
with
|
||||||
|
| :? DirectoryNotFoundException
|
||||||
|
| :? FileNotFoundException ->
|
||||||
|
Assert.Inconclusive ()
|
||||||
|
failwith "unreachable"
|
||||||
|
|
||||||
|
Day18.part1 s |> shouldEqual 106459
|
||||||
|
|
||||||
|
[<Test>]
|
||||||
|
let part2Actual () =
|
||||||
|
let s =
|
||||||
|
try
|
||||||
|
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day18.txt"))
|
||||||
|
with
|
||||||
|
| :? DirectoryNotFoundException
|
||||||
|
| :? FileNotFoundException ->
|
||||||
|
Assert.Inconclusive ()
|
||||||
|
failwith "unreachable"
|
||||||
|
|
||||||
|
Day18.part2 s |> shouldEqual 0
|
14
AdventOfCode2023.FSharp/Test/samples/day18.txt
Normal file
14
AdventOfCode2023.FSharp/Test/samples/day18.txt
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
R 6 (#70c710)
|
||||||
|
D 5 (#0dc571)
|
||||||
|
L 2 (#5713f0)
|
||||||
|
D 2 (#d2c081)
|
||||||
|
R 2 (#59c680)
|
||||||
|
D 2 (#411b91)
|
||||||
|
L 5 (#8ceee2)
|
||||||
|
U 2 (#caa173)
|
||||||
|
L 1 (#1b58a2)
|
||||||
|
U 2 (#caa171)
|
||||||
|
R 2 (#7807d2)
|
||||||
|
U 3 (#a77fa3)
|
||||||
|
L 2 (#015232)
|
||||||
|
U 2 (#7a21e3)
|
Reference in New Issue
Block a user