31 lines
941 B
Forth
31 lines
941 B
Forth
module advent_of_code.tasks.day_7.Star1
|
|
|
|
open System.IO
|
|
|
|
let stream = new StreamReader("tasks/day-7/input.txt")
|
|
|
|
let containsSplit (s: string) =
|
|
s.Contains("^")
|
|
|
|
let split (row: string) (pos: int) =
|
|
if row[pos] = '^' then
|
|
[|pos-1; pos+1|] |> Set.ofArray, 1
|
|
else
|
|
[|pos|] |> Set.ofArray, 0
|
|
|
|
let rec getBeams (manifold: string array) (pos: Set<int>) =
|
|
if manifold.Length = 0 then
|
|
0
|
|
else
|
|
let row, tail = manifold[0], manifold[1..]
|
|
let mapRes = (pos |> Seq.map (split row))
|
|
let newPos = (mapRes |> Seq.map fst) |> Set.ofSeq |> Set.unionMany
|
|
let splits = mapRes |> Seq.map snd |> Seq.sum
|
|
getBeams tail newPos + splits
|
|
|
|
let main =
|
|
let allLines = stream.ReadToEnd().Split("\n")
|
|
let startLine, manifold = allLines[0], (allLines[1..] |> Array.filter containsSplit)
|
|
let startPos = startLine.IndexOf("S")
|
|
getBeams manifold ([|startPos|] |> Set.ofArray)
|