37 lines
1.4 KiB
Forth
37 lines
1.4 KiB
Forth
module advent_of_code.tasks.day_7.Star2
|
|
|
|
open System
|
|
open System.IO
|
|
|
|
let stream = new StreamReader("tasks/day-7/input.txt")
|
|
|
|
let containsSplit (s: string) =
|
|
s.Contains("^")
|
|
|
|
let split (oldTimelines: int64 array) (newTimelines: int64 array) (t: int * char) =
|
|
let index, item = t.Deconstruct()
|
|
let mutable timelines = newTimelines
|
|
if item = '^' then
|
|
timelines[index-1] <- oldTimelines[index] + newTimelines[index-1]
|
|
timelines[index+1] <- oldTimelines[index] + newTimelines[index+1]
|
|
else
|
|
timelines[index] <- oldTimelines[index] + newTimelines[index]
|
|
timelines
|
|
|
|
let rec getTimelines (manifold: string array) (prevTimelines: int64 array) =
|
|
if manifold.Length = 0 then
|
|
prevTimelines
|
|
else
|
|
let row, tail = manifold[0], manifold[1..]
|
|
let emptyTimeline = [1 .. row.Length] |> Seq.map (fun _ -> 0 |> int64) |> Array.ofSeq
|
|
let newTimeline = row |> seq |> Seq.mapi (fun index item -> index, item) |> Seq.fold (split prevTimelines) emptyTimeline
|
|
getTimelines tail newTimeline
|
|
|
|
|
|
let main =
|
|
let allLines = stream.ReadToEnd().Split("\n")
|
|
let startLine, manifold = allLines[0], (allLines[1..] |> Array.filter containsSplit)
|
|
let startPos = startLine.IndexOf("S")
|
|
let initialTimeline = [1..startLine.Length] |> Seq.map (fun _ -> 0 |> int64) |> Array.ofSeq
|
|
initialTimeline[startPos] <- 1
|
|
Array.sum (getTimelines manifold initialTimeline) |