-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday05_part02.fs
38 lines (32 loc) · 1.6 KB
/
day05_part02.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
module day05_part02
open AdventOfCode_Utilities
open AdventOfCode_2022.Modules.LocalHelper
let path = "day05/day05_input.txt"
let parseLines (lines: string list) =
let numberOfTowers = lines.Head.Length / 4 + 1
let indexes = [0..(numberOfTowers - 1)] |> List.map(fun i -> 1 + (i * 4))
let chunks = lines |> List.mapi(fun idx l -> (idx, (l.ToCharArray() |> Array.toList)))
let towers = Array.init numberOfTowers (fun v -> "")
for l in chunks do
for idx in indexes do
if ['A'..'Z'] |> List.contains((snd l).Item(idx)) then
towers.[idx / 4] <- towers.[idx / 4] + ((string)((snd l).Item(idx)))
towers
let performMove (towers: string []) (mov: int[]) =
let transfer = System.String.Concat((towers.[mov.[1] - 1].Substring(0, mov.[0])).ToCharArray())
towers.[mov.[2] - 1] <- transfer + towers.[mov.[2] - 1]
towers.[mov.[1] - 1] <- towers.[mov.[1] - 1].Substring(mov.[0])
towers
let rec runMovements (towers: string[]) (movs: int[] list) =
match movs.Length with
| 0 -> towers
| _ ->
let newTowers = performMove towers movs.Head
runMovements newTowers movs.Tail
let execute =
let inputLines = GetLinesFromFile(path) |> Seq.toList
let content = Utilities.getGroupsOnSeparator inputLines ""
let (initDrawing, movements) = (content.Head, content.Tail.Head)
let movDefinitions = movements |> List.map(fun m -> [|(int)(m.Split(' ').[1]); (int)(m.Split(' ').[3]); (int)(m.Split(' ').[5])|])
let towers = parseLines initDrawing
System.String.Concat(runMovements towers movDefinitions |> Array.map(fun w -> w.[0]))