summary refs log tree commit diff stats
path: root/solutions
diff options
context:
space:
mode:
authorBrian Chu <brianmchu42@gmail.com>2022-12-13 15:58:13 -0800
committerBrian Chu <brianmchu42@gmail.com>2022-12-13 15:58:13 -0800
commit2b99148232e659d10d4e5d77df7956366d28f00d (patch)
tree2b4a0539a194fae8b8c653ee78089d135ebd22c9 /solutions
parent6eb34aa6152415e6db96b45456bfaefd242c1b0d (diff)
downloadAdventOfCode2022-2b99148232e659d10d4e5d77df7956366d28f00d.tar.gz
solution for day 10
Diffstat (limited to 'solutions')
-rw-r--r--solutions/day10.fs34
1 files changed, 34 insertions, 0 deletions
diff --git a/solutions/day10.fs b/solutions/day10.fs
new file mode 100644
index 0000000..799850d
--- /dev/null
+++ b/solutions/day10.fs
@@ -0,0 +1,34 @@
+namespace Solutions
+
+module Day10 =
+    open System.IO
+    open System.Text.RegularExpressions
+
+    let (|InstRegex|_|) pattern line =
+        let matched = Regex.Match(line, pattern)
+        if matched.Success then
+            matched.Groups |> Seq.tail |> Seq.map (fun x -> x.Value) |> List.ofSeq |> Some
+        else None
+
+    let parseInst line =
+        match line with
+        | InstRegex "noop" [] -> [0]
+        | InstRegex "addx (-?\d+)" x -> [0; (int x[0])]
+        | _ -> failwith "invalid input"
+
+    let changes = File.ReadLines("inputs/day10.txt") |> Seq.map parseInst |> List.concat
+
+    let executeInsts changes =
+        List.scan (+) 1 changes
+
+    let getStateSum cycles (states: int list) =
+        cycles |> List.map (fun x -> x * states[x-1]) |> List.sum
+
+    let part1 () = executeInsts changes |> getStateSum [20; 60; 100; 140; 180; 220]
+
+    let part2 () = executeInsts changes 
+                    |> List.mapi (fun pixel signal -> abs (signal - (pixel % 40)) <= 1) 
+                    |> List.map (fun x -> if x then "#" else " ")
+                    |> List.chunkBySize 40
+                    |> List.map (String.concat "")
+                    |> List.iter (printf "%s\n")
\ No newline at end of file