This commit is contained in:
Smaug123
2023-12-12 08:55:29 +00:00
parent 73a017862a
commit 511814f241
3 changed files with 54 additions and 3 deletions

View File

@@ -1,12 +1,57 @@
namespace AdventOfCode2023 namespace AdventOfCode2023
open System
open System.Collections.Generic
open System.Globalization
[<RequireQualifiedAccess>] [<RequireQualifiedAccess>]
module Day12 = module Day12 =
let rec solve (line : ReadOnlySpan<char>) (groups : IReadOnlyList<int>) (currentGroupIndex : int) =
match line.[0] with
| '#' ->
if currentGroupIndex >= groups.Count then 0 else
let mutable isOk = true
for i = 1 to groups.[currentGroupIndex] - 1 do
if isOk && line.[i] <> '#' && line.[i] <> '?' then
isOk <- false
if not isOk then 0 else
if line.[groups.[currentGroupIndex]] = '#' then 0 else
solve (line.Slice (groups.[currentGroupIndex] + 1)) groups (currentGroupIndex + 1)
| '.' ->
solve (line.Slice 1) groups currentGroupIndex
| '?' ->
let ifDot = solve (line.Slice 1) groups currentGroupIndex
let ifHash =
if currentGroupIndex >= groups.Count then 0 else
let mutable isOk = true
for i = 1 to groups.[currentGroupIndex] - 1 do
if isOk && line.[i] <> '#' && line.[i] <> '?' then
isOk <- false
if not isOk then 0 else
if line.[groups.[currentGroupIndex]] = '#' then 0 else
solve (line.Slice (groups.[currentGroupIndex] + 1)) groups (currentGroupIndex + 1)
ifDot + ifHash
| _ ->
if currentGroupIndex = groups.Count then 1 else 0
let part1 (s : string) = let part1 (s : string) =
use mutable lines = StringSplitEnumerator.make '\n' s use mutable lines = StringSplitEnumerator.make '\n' s
-1 let mutable answer = 0
let arr = ResizeArray ()
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 solved = solve line arr 0
answer <- answer + solved
answer
let part2 (s : string) = let part2 (s : string) =
use mutable lines = StringSplitEnumerator.make '\n' s use mutable lines = StringSplitEnumerator.make '\n' s

View File

@@ -12,7 +12,7 @@ module TestDay12 =
[<Test>] [<Test>]
let part1Sample () = let part1Sample () =
sample |> Day12.part1 |> shouldEqual 0 sample |> Day12.part1 |> shouldEqual 21
[<Test>] [<Test>]
let part2Sample () = let part2Sample () =
@@ -31,7 +31,7 @@ module TestDay12 =
Assert.Inconclusive () Assert.Inconclusive ()
failwith "unreachable" failwith "unreachable"
Day12.part1 s |> shouldEqual 0 Day12.part1 s |> shouldEqual 7402
[<Test>] [<Test>]
let part2Actual () = let part2Actual () =

View File

@@ -0,0 +1,6 @@
???.### 1,1,3
.??..??...?##. 1,1,3
?#?#?#?#?#?#?#? 1,3,1,6
????.#...#... 4,1,1
????.######..#####. 1,6,5
?###???????? 3,2,1