Add day 7
This commit is contained in:
parent
7389931fbf
commit
acaef8ee35
@ -28,4 +28,8 @@ let main _ =
|
||||
printfn $"%A{day6Star1}"
|
||||
let day6Star2 = day_6.Star2.main
|
||||
printfn $"%A{day6Star2}"
|
||||
let day7Star1 = day_7.Star1.main
|
||||
printfn $"%A{day7Star1}"
|
||||
let day7Star2 = day_7.Star2.main
|
||||
printfn $"%A{day7Star2}"
|
||||
0
|
||||
|
||||
30
advent-of-code/tasks/day-7/Star1.fs
Normal file
30
advent-of-code/tasks/day-7/Star1.fs
Normal file
@ -0,0 +1,30 @@
|
||||
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)
|
||||
37
advent-of-code/tasks/day-7/Star2.fs
Normal file
37
advent-of-code/tasks/day-7/Star2.fs
Normal file
@ -0,0 +1,37 @@
|
||||
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)
|
||||
Loading…
x
Reference in New Issue
Block a user