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() let mutable maxValue = 0 let executeInstruction (inst: Instruction) = let mutable regVal = 0 registers.TryGetValue(inst.compReg, ®Val) |> ignore if inst.compFun regVal inst.compVal then registers.TryGetValue(inst.opReg, ®Val) |> 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