Day 12 #13
@@ -22,6 +22,7 @@
|
||||
<Compile Include="Day9.fs"/>
|
||||
<Compile Include="Day10.fs" />
|
||||
<Compile Include="Day11.fs" />
|
||||
<Compile Include="Day12.fs" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@@ -1,7 +1,5 @@
|
||||
namespace AdventOfCode2023
|
||||
|
||||
open System
|
||||
|
||||
[<RequireQualifiedAccess>]
|
||||
module Day11 =
|
||||
|
||||
@@ -41,7 +39,7 @@ module Day11 =
|
||||
let result = ResizeArray ()
|
||||
let mutable prevCol = 0
|
||||
|
||||
for (_, c) in galaxies do
|
||||
for _, c in galaxies do
|
||||
if c > prevCol then
|
||||
for j = prevCol + 1 to c - 1 do
|
||||
result.Add j
|
||||
|
196
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day12.fs
Normal file
196
AdventOfCode2023.FSharp/AdventOfCode2023.FSharp.Lib/Day12.fs
Normal file
@@ -0,0 +1,196 @@
|
||||
namespace AdventOfCode2023
|
||||
|
||||
open System
|
||||
open System.Collections.Generic
|
||||
open System.Globalization
|
||||
|
||||
[<RequireQualifiedAccess>]
|
||||
module Day12 =
|
||||
|
||||
let rec solve
|
||||
(dict : Dictionary<int * int, uint64>)
|
||||
(line : ReadOnlySpan<char>)
|
||||
(groups : IReadOnlyList<int>)
|
||||
(remainingToFill : int)
|
||||
(currentGroupIndex : int)
|
||||
: uint64
|
||||
=
|
||||
if line.Length = 0 then
|
||||
if currentGroupIndex = groups.Count then
|
||||
LanguagePrimitives.GenericOne
|
||||
else
|
||||
LanguagePrimitives.GenericZero
|
||||
elif currentGroupIndex = groups.Count then
|
||||
if line.Contains '#' then
|
||||
LanguagePrimitives.GenericZero
|
||||
else
|
||||
LanguagePrimitives.GenericOne
|
||||
else
|
||||
|
||||
match dict.TryGetValue ((line.Length, currentGroupIndex)) with
|
||||
| true, v -> v
|
||||
| false, _ ->
|
||||
|
||||
if remainingToFill > line.Length then
|
||||
dict.Add ((line.Length, currentGroupIndex), LanguagePrimitives.GenericZero)
|
||||
LanguagePrimitives.GenericZero
|
||||
else
|
||||
|
||||
match line.[0] with
|
||||
| '#' ->
|
||||
if currentGroupIndex >= groups.Count then
|
||||
LanguagePrimitives.GenericZero
|
||||
else
|
||||
let mutable isOk = true
|
||||
|
||||
for i = 1 to groups.[currentGroupIndex] - 1 do
|
||||
if isOk && (i >= line.Length || (line.[i] <> '#' && line.[i] <> '?')) then
|
||||
isOk <- false
|
||||
|
||||
if not isOk then
|
||||
LanguagePrimitives.GenericZero
|
||||
else if groups.[currentGroupIndex] < line.Length then
|
||||
if line.[groups.[currentGroupIndex]] = '#' then
|
||||
LanguagePrimitives.GenericZero
|
||||
else
|
||||
solve
|
||||
dict
|
||||
(line.Slice (groups.[currentGroupIndex] + 1))
|
||||
groups
|
||||
(remainingToFill - groups.[currentGroupIndex] - 1)
|
||||
(currentGroupIndex + 1)
|
||||
else
|
||||
solve
|
||||
dict
|
||||
ReadOnlySpan<_>.Empty
|
||||
groups
|
||||
(remainingToFill - groups.[currentGroupIndex] - 1)
|
||||
(currentGroupIndex + 1)
|
||||
| '.' -> solve dict (line.Slice 1) groups remainingToFill currentGroupIndex
|
||||
| '?' ->
|
||||
let afterMark = line.IndexOfAnyExcept ('?', '#')
|
||||
|
||||
if afterMark >= 0 && groups.[currentGroupIndex] > afterMark then
|
||||
// this group would extend into a dot if this were filled in!
|
||||
let firstHash = line.IndexOf '#'
|
||||
|
||||
if firstHash >= 0 && firstHash < afterMark then
|
||||
// this group *is* filled in, contradiction
|
||||
LanguagePrimitives.GenericZero
|
||||
else
|
||||
solve dict (line.Slice afterMark) groups remainingToFill currentGroupIndex
|
||||
else
|
||||
|
||||
let ifDot = solve dict (line.Slice 1) groups remainingToFill currentGroupIndex
|
||||
dict.TryAdd ((line.Length - 1, currentGroupIndex), ifDot) |> ignore
|
||||
|
||||
let ifHash =
|
||||
if currentGroupIndex >= groups.Count then
|
||||
LanguagePrimitives.GenericZero
|
||||
else
|
||||
|
||||
let mutable isOk = true
|
||||
|
||||
for i = 1 to groups.[currentGroupIndex] - 1 do
|
||||
if isOk && (i >= line.Length || (line.[i] <> '#' && line.[i] <> '?')) then
|
||||
isOk <- false
|
||||
|
||||
if not isOk then
|
||||
LanguagePrimitives.GenericZero
|
||||
else if groups.[currentGroupIndex] < line.Length then
|
||||
if
|
||||
groups.[currentGroupIndex] < line.Length
|
||||
&& line.[groups.[currentGroupIndex]] = '#'
|
||||
then
|
||||
LanguagePrimitives.GenericZero
|
||||
else
|
||||
solve
|
||||
dict
|
||||
(line.Slice (groups.[currentGroupIndex] + 1))
|
||||
groups
|
||||
(remainingToFill - groups.[currentGroupIndex] - 1)
|
||||
(currentGroupIndex + 1)
|
||||
else
|
||||
solve
|
||||
dict
|
||||
ReadOnlySpan<_>.Empty
|
||||
groups
|
||||
(remainingToFill - groups.[currentGroupIndex] - 1)
|
||||
(currentGroupIndex + 1)
|
||||
|
||||
let ans = ifDot + ifHash
|
||||
dict.TryAdd ((line.Length, currentGroupIndex), ans) |> ignore
|
||||
ans
|
||||
| _ ->
|
||||
if currentGroupIndex = groups.Count then
|
||||
LanguagePrimitives.GenericOne
|
||||
else
|
||||
LanguagePrimitives.GenericZero
|
||||
|
||||
let part1 (s : string) =
|
||||
use mutable lines = StringSplitEnumerator.make '\n' s
|
||||
|
||||
let mutable answer = 0uL
|
||||
let arr = ResizeArray ()
|
||||
|
||||
let dict = Dictionary ()
|
||||
|
||||
for line in lines do
|
||||
if not line.IsEmpty then
|
||||
arr.Clear ()
|
||||
use ints = StringSplitEnumerator.make' ',' (line.Slice (line.IndexOf ' ' + 1))
|
||||
|
||||
for int in ints do
|
||||
arr.Add (Int32.Parse (int, NumberStyles.None, CultureInfo.InvariantCulture))
|
||||
|
||||
let remainingToFill =
|
||||
let mutable ans = -1
|
||||
|
||||
for i = 0 to arr.Count - 1 do
|
||||
ans <- ans + arr.[i] + 1
|
||||
|
||||
ans
|
||||
|
||||
dict.Clear ()
|
||||
let solved = solve dict line arr remainingToFill 0
|
||||
answer <- answer + solved
|
||||
|
||||
answer
|
||||
|
||||
let part2 (s : string) =
|
||||
use mutable lines = StringSplitEnumerator.make '\n' s
|
||||
|
||||
let mutable answer = 0uL
|
||||
let arr = ResizeArray ()
|
||||
|
||||
let dict = Dictionary ()
|
||||
|
||||
for line in lines do
|
||||
if not line.IsEmpty then
|
||||
arr.Clear ()
|
||||
let spaceIndex = line.IndexOf ' '
|
||||
|
||||
for _ = 0 to 4 do
|
||||
use ints = StringSplitEnumerator.make' ',' (line.Slice (spaceIndex + 1))
|
||||
|
||||
for int in ints do
|
||||
arr.Add (Int32.Parse (int, NumberStyles.None, CultureInfo.InvariantCulture))
|
||||
|
||||
let sliced = line.Slice(0, spaceIndex).ToString ()
|
||||
|
||||
let line =
|
||||
String.Concat (sliced, '?', sliced, '?', sliced, '?', sliced, '?', sliced)
|
||||
|
||||
let remainingToFill =
|
||||
let mutable ans = -1
|
||||
|
||||
for i = 0 to arr.Count - 1 do
|
||||
ans <- ans + arr.[i] + 1
|
||||
|
||||
ans
|
||||
|
||||
dict.Clear ()
|
||||
let solved = solve dict (line.AsSpan ()) arr remainingToFill 0
|
||||
answer <- answer + solved
|
||||
|
||||
answer
|
@@ -217,12 +217,37 @@ module Program =
|
||||
let input = Path.Combine (dir.FullName, "day11.txt") |> File.ReadAllText
|
||||
|
||||
sw.Restart ()
|
||||
let part1 = Day11.part1 input
|
||||
let data = Day11.parse input
|
||||
sw.Stop ()
|
||||
|
||||
Console.Error.WriteLine (
|
||||
(1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString ()
|
||||
+ "ms parse"
|
||||
)
|
||||
|
||||
sw.Restart ()
|
||||
let part1 = Day11.solve data 2uL
|
||||
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
|
||||
let part2 = Day11.solve data 1_000_000uL
|
||||
sw.Stop ()
|
||||
Console.WriteLine (part2.ToString ())
|
||||
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
|
||||
|
||||
Console.WriteLine "=====Day 12====="
|
||||
|
||||
do
|
||||
let input = Path.Combine (dir.FullName, "day12.txt") |> File.ReadAllText
|
||||
|
||||
sw.Restart ()
|
||||
let part1 = Day12.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 = Day12.part2 input
|
||||
sw.Stop ()
|
||||
Console.WriteLine (part2.ToString ())
|
||||
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms")
|
||||
|
@@ -20,6 +20,7 @@
|
||||
<Compile Include="TestDay9.fs"/>
|
||||
<Compile Include="TestDay10.fs" />
|
||||
<Compile Include="TestDay11.fs" />
|
||||
<Compile Include="TestDay12.fs" />
|
||||
<EmbeddedResource Include="samples\day1.txt"/>
|
||||
<EmbeddedResource Include="samples\day1part1.txt"/>
|
||||
<EmbeddedResource Include="samples\day2.txt"/>
|
||||
@@ -34,6 +35,7 @@
|
||||
<EmbeddedResource Include="samples\day10part1.txt" />
|
||||
<EmbeddedResource Include="samples\day10.txt" />
|
||||
<EmbeddedResource Include="samples\day11.txt" />
|
||||
<EmbeddedResource Include="samples\day12.txt" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
45
AdventOfCode2023.FSharp/Test/TestDay12.fs
Normal file
45
AdventOfCode2023.FSharp/Test/TestDay12.fs
Normal file
@@ -0,0 +1,45 @@
|
||||
namespace AdventOfCode2023.Test
|
||||
|
||||
open AdventOfCode2023
|
||||
open NUnit.Framework
|
||||
open FsUnitTyped
|
||||
open System.IO
|
||||
|
||||
[<TestFixture>]
|
||||
module TestDay12 =
|
||||
|
||||
let sample = Assembly.getEmbeddedResource typeof<Dummy>.Assembly "day12.txt"
|
||||
|
||||
[<Test>]
|
||||
let part1Sample () =
|
||||
sample |> Day12.part1 |> shouldEqual 21uL
|
||||
|
||||
[<Test>]
|
||||
let part2Sample () =
|
||||
sample |> Day12.part2 |> shouldEqual 525152uL
|
||||
|
||||
[<Test>]
|
||||
let part1Actual () =
|
||||
let s =
|
||||
try
|
||||
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day12.txt"))
|
||||
with
|
||||
| :? DirectoryNotFoundException
|
||||
| :? FileNotFoundException ->
|
||||
Assert.Inconclusive ()
|
||||
failwith "unreachable"
|
||||
|
||||
Day12.part1 s |> shouldEqual 7402uL
|
||||
|
||||
[<Test>]
|
||||
let part2Actual () =
|
||||
let s =
|
||||
try
|
||||
File.ReadAllText (Path.Combine (__SOURCE_DIRECTORY__, "../../inputs/day12.txt"))
|
||||
with
|
||||
| :? DirectoryNotFoundException
|
||||
| :? FileNotFoundException ->
|
||||
Assert.Inconclusive ()
|
||||
failwith "unreachable"
|
||||
|
||||
Day12.part2 s |> shouldEqual 3384337640277uL
|
6
AdventOfCode2023.FSharp/Test/samples/day12.txt
Normal file
6
AdventOfCode2023.FSharp/Test/samples/day12.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
???.### 1,1,3
|
||||
.??..??...?##. 1,1,3
|
||||
?#?#?#?#?#?#?#? 1,3,1,6
|
||||
????.#...#... 4,1,1
|
||||
????.######..#####. 1,6,5
|
||||
?###???????? 3,2,1
|
Reference in New Issue
Block a user