-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10_part02.fs
40 lines (33 loc) · 1.48 KB
/
day10_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
39
40
module day10_part02
open AdventOfCode_2022.Modules.LocalHelper
let path = "day10/day10_input.txt"
let rec runOp (op: int * int) (currentValue: int) (listOfCycles: int list) =
match fst op > 1 with
| false ->
let lastValue = currentValue + snd op
(lastValue, listOfCycles @ [lastValue])
| true ->
let newListOfCycles = listOfCycles @ [currentValue]
runOp ((fst op) - 1, snd op) currentValue newListOfCycles
let rec performInstructions (ins: (int * int) list) (currentValue: int) (listOfCycles: int list)=
match ins with
| head :: tail ->
let (newCurrentValue, newListOfCycles) = runOp head currentValue listOfCycles
performInstructions tail newCurrentValue newListOfCycles
| [] -> listOfCycles
let getLitPixels (values: int list) =
let chunks = values |> List.chunkBySize 40
seq {
for chunk in chunks do
yield (chunk |> List.mapi(fun idx v -> if v = idx || v = (idx - 1) || v = (idx + 1) then "#" else "."))
}
let displayCRT (output: string list seq) =
for line in output do
printfn "%s" (System.String.Concat(line))
let execute =
let inputLines = GetLinesFromFile(path)
let instructions = inputLines |> Array.map(fun l -> if l.Split(' ').[0] = "noop" then (1, 0) else (2, (int)(l.Split(' ').[1]))) |> Array.toList
let initValue = 1
let initlistOfCycles = [1]
let result = performInstructions instructions initValue initlistOfCycles
displayCRT (getLitPixels result)