summary refs log tree commit diff stats
path: root/day8.fsx
diff options
context:
space:
mode:
Diffstat (limited to 'day8.fsx')
-rw-r--r--day8.fsx60
1 files changed, 60 insertions, 0 deletions
diff --git a/day8.fsx b/day8.fsx
new file mode 100644
index 0000000..5a4f423
--- /dev/null
+++ b/day8.fsx
@@ -0,0 +1,60 @@
+open System.IO
+open System.Text.RegularExpressions
+open System.Collections.Generic
+
+let (|InstRegex|_|) input =
+    let m = Regex.Match(input, "(\w+) (dec|inc) (-?\d+) if (\w+) (>|<|>=|<=|==|!=) (-?\d+)")
+    if m.Success then
+        let op = if m.Groups[2].Value = "dec" then (-) else (+)
+        let comparator: int->int->bool = 
+            match m.Groups[5].Value with
+            | ">" -> (>)
+            | "<" -> (<)
+            | ">=" -> (>=)
+            | "<=" -> (<=)
+            | "==" -> (=)
+            | "!=" -> (<>)
+            | _ -> (fun x y -> false)
+        Some((m.Groups[1].Value, op, int m.Groups[3].Value, m.Groups[4].Value, comparator, int m.Groups[6].Value))
+    else None
+
+type Instruction =
+    struct
+        val opReg: string
+        val opFun: int -> int -> int
+        val opVal: int
+        val compReg: string
+        val compFun: int -> int -> bool
+        val compVal: int
+        new(inst: string) =
+            match inst with
+            | InstRegex (target, operator, value, compared, comparator, compValue) ->
+                {
+                    opReg = target;
+                    opFun = operator;
+                    opVal = value;
+                    compReg = compared;
+                    compFun = comparator;
+                    compVal = compValue
+                }
+            | _ -> {opReg = ""; opFun = (+); opVal = 0; compReg = ""; compFun = (>); compVal = 0}
+    end
+
+let registers = new Dictionary<string, int>()
+let mutable maxValue = 0
+let executeInstruction (inst: Instruction) =
+    let mutable regVal = 0
+    registers.TryGetValue(inst.compReg, &regVal) |> ignore
+    if inst.compFun regVal inst.compVal then
+        registers.TryGetValue(inst.opReg, &regVal) |> ignore
+        registers[inst.opReg] <- inst.opFun regVal inst.opVal
+        if registers[inst.opReg] > maxValue then
+            maxValue <- registers[inst.opReg]
+        else ()
+    else ()
+
+File.ReadAllLines "day8.txt" |> Array.map (fun x -> new Instruction(x)) |> Array.iter executeInstruction
+// part 1
+registers.Values |> Seq.max |> printfn "%A"
+// part 2
+printfn "%d" maxValue
\ No newline at end of file