Day 12 #13

Merged
patrick merged 8 commits from day12 into main 2023-12-12 19:23:41 +00:00
3 changed files with 106 additions and 35 deletions
Showing only changes of commit 20a45caf8f - Show all commits

View File

@@ -7,61 +7,125 @@ open System.Globalization
[<RequireQualifiedAccess>] [<RequireQualifiedAccess>]
module Day12 = module Day12 =
let rec solve (line : ReadOnlySpan<char>) (groups : IReadOnlyList<int>) (currentGroupIndex : int) = let rec solve
(dict : Dictionary<int * int, 'a>)
(line : ReadOnlySpan<char>)
(groups : IReadOnlyList<int>)
(currentGroupIndex : int)
: 'a
=
if line.Length = 0 then if line.Length = 0 then
if currentGroupIndex = groups.Count then LanguagePrimitives.GenericOne else LanguagePrimitives.GenericZero 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 else
match dict.TryGetValue ((line.Length, currentGroupIndex)) with
| true, v -> v
| false, _ ->
let remaining =
let mutable remaining = -1
for i = currentGroupIndex to groups.Count - 1 do
remaining <- remaining + groups.[i] + 1
remaining
if remaining > line.Length then
dict.Add ((line.Length, currentGroupIndex), LanguagePrimitives.GenericZero)
LanguagePrimitives.GenericZero
else
match line.[0] with match line.[0] with
| '#' -> | '#' ->
if currentGroupIndex >= groups.Count then LanguagePrimitives.GenericZero else if currentGroupIndex >= groups.Count then
let mutable isOk = true LanguagePrimitives.GenericZero
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 (line.Slice (groups.[currentGroupIndex] + 1)) groups (currentGroupIndex + 1)
else else
solve ReadOnlySpan<_>.Empty groups (currentGroupIndex + 1)
| '.' ->
solve (line.Slice 1) groups currentGroupIndex
| '?' ->
let ifDot = solve (line.Slice 1) groups currentGroupIndex
let ifHash =
if currentGroupIndex >= groups.Count then LanguagePrimitives.GenericZero else
let mutable isOk = true let mutable isOk = true
for i = 1 to groups.[currentGroupIndex] - 1 do for i = 1 to groups.[currentGroupIndex] - 1 do
if isOk && (i >= line.Length || (line.[i] <> '#' && line.[i] <> '?')) then if isOk && (i >= line.Length || (line.[i] <> '#' && line.[i] <> '?')) then
isOk <- false isOk <- false
if not isOk then LanguagePrimitives.GenericZero else
if groups.[currentGroupIndex] < line.Length then if not isOk then
if groups.[currentGroupIndex] < line.Length && line.[groups.[currentGroupIndex]] = '#' then LanguagePrimitives.GenericZero
else if groups.[currentGroupIndex] < line.Length then
if line.[groups.[currentGroupIndex]] = '#' then
LanguagePrimitives.GenericZero LanguagePrimitives.GenericZero
else else
solve (line.Slice (groups.[currentGroupIndex] + 1)) groups (currentGroupIndex + 1) solve dict (line.Slice (groups.[currentGroupIndex] + 1)) groups (currentGroupIndex + 1)
else else
solve ReadOnlySpan<_>.Empty groups (currentGroupIndex + 1) solve dict ReadOnlySpan<_>.Empty groups (currentGroupIndex + 1)
| '.' -> solve dict (line.Slice 1) groups 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 currentGroupIndex
else
let ifDot = solve dict (line.Slice 1) groups 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 (currentGroupIndex + 1)
else
solve dict ReadOnlySpan<_>.Empty groups (currentGroupIndex + 1)
ifDot + ifHash ifDot + ifHash
| _ -> | _ ->
if currentGroupIndex = groups.Count then LanguagePrimitives.GenericOne else LanguagePrimitives.GenericZero if currentGroupIndex = groups.Count then
LanguagePrimitives.GenericOne
else
LanguagePrimitives.GenericZero
let part1 (s : string) = let part1 (s : string) =
use mutable lines = StringSplitEnumerator.make '\n' s use mutable lines = StringSplitEnumerator.make '\n' s
let mutable answer = 0uL let mutable answer = 0uL
let arr = ResizeArray () let arr = ResizeArray ()
for line in lines do for line in lines do
if not line.IsEmpty then if not line.IsEmpty then
arr.Clear () arr.Clear ()
use ints = StringSplitEnumerator.make' ',' (line.Slice (line.IndexOf ' ' + 1)) use ints = StringSplitEnumerator.make' ',' (line.Slice (line.IndexOf ' ' + 1))
for int in ints do for int in ints do
arr.Add (Int32.Parse (int, NumberStyles.None, CultureInfo.InvariantCulture)) arr.Add (Int32.Parse (int, NumberStyles.None, CultureInfo.InvariantCulture))
let solved = solve line arr 0 let solved = solve (Dictionary ()) line arr 0
answer <- answer + solved answer <- answer + solved
answer answer
@@ -71,19 +135,24 @@ module Day12 =
let mutable answer = 0uL let mutable answer = 0uL
let arr = ResizeArray () let arr = ResizeArray ()
for line in lines do for line in lines do
if not line.IsEmpty then if not line.IsEmpty then
arr.Clear () arr.Clear ()
let spaceIndex =line.IndexOf ' ' let spaceIndex = line.IndexOf ' '
for _ = 0 to 4 do for _ = 0 to 4 do
use ints = StringSplitEnumerator.make' ',' (line.Slice (spaceIndex + 1)) use ints = StringSplitEnumerator.make' ',' (line.Slice (spaceIndex + 1))
for int in ints do for int in ints do
arr.Add (Int32.Parse (int, NumberStyles.None, CultureInfo.InvariantCulture)) arr.Add (Int32.Parse (int, NumberStyles.None, CultureInfo.InvariantCulture))
let sliced = line.Slice(0, spaceIndex).ToString () let sliced = line.Slice(0, spaceIndex).ToString ()
let line = String.Concat (sliced, '?', sliced, '?', sliced, '?', sliced, '?', sliced)
let solved = solve (line.AsSpan()) arr 0 let line =
String.Concat (sliced, '?', sliced, '?', sliced, '?', sliced, '?', sliced)
let solved = solve (Dictionary ()) (line.AsSpan ()) arr 0
printfn $"%s{line} : %i{solved}" printfn $"%s{line} : %i{solved}"
answer <- answer + solved answer <- answer + solved

View File

@@ -219,7 +219,11 @@ module Program =
sw.Restart () sw.Restart ()
let data = Day11.parse input let data = Day11.parse input
sw.Stop () sw.Stop ()
Console.Error.WriteLine ((1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString () + "ms parse")
Console.Error.WriteLine (
(1_000.0 * float sw.ElapsedTicks / float Stopwatch.Frequency).ToString ()
+ "ms parse"
)
sw.Restart () sw.Restart ()
let part1 = Day11.solve data 2uL let part1 = Day11.solve data 2uL

View File

@@ -16,9 +16,7 @@ module TestDay12 =
[<Test>] [<Test>]
let part2Sample () = let part2Sample () =
sample sample |> Day12.part2 |> shouldEqual 525152uL
|> Day12.part2
|> shouldEqual 525152uL
[<Test>] [<Test>]
let part1Actual () = let part1Actual () =