|
|
@@ -0,0 +1,90 @@
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.IO;
|
|
|
+using System.Linq;
|
|
|
+
|
|
|
+namespace D23._1
|
|
|
+{
|
|
|
+ public class Program
|
|
|
+ {
|
|
|
+ static void Main(string[] args)
|
|
|
+ {
|
|
|
+ int[] Registers = new int[5] {7, 0, 0, 0, 0 };
|
|
|
+
|
|
|
+ Work(args, Registers);
|
|
|
+
|
|
|
+ Console.WriteLine($"The answer is : {Registers[0]}");
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void Work(string[] args, int[] Registers)
|
|
|
+ {
|
|
|
+ List<string[]> instr = File.ReadAllText(args[0]).Split(new string[] { "\r\n", "\n" }, StringSplitOptions.RemoveEmptyEntries)
|
|
|
+ .Select(n => n.Split(" ")).ToList();
|
|
|
+
|
|
|
+ var Ops = new Dictionary<string, Action<bool, bool, int, int>>()
|
|
|
+ {
|
|
|
+ { "cpy", (ia, ib, a, b) => {
|
|
|
+ if (ib == false) return;
|
|
|
+ Registers[b] = ia ? Registers[a] : a; }
|
|
|
+ },
|
|
|
+ { "inc", (ia, ib, a, b) => {
|
|
|
+ if (ia == false) return;
|
|
|
+ Registers[a]++; }
|
|
|
+ },
|
|
|
+ { "dec", (ia, ib, a, b) => {
|
|
|
+ if (ia == false) return;
|
|
|
+ Registers[a]--;
|
|
|
+ } },
|
|
|
+ { "jnz", (ia, ib, a, b) => { Registers[4] += (ia ? Registers[a] : a) != 0 ? (ib ? Registers[b] : b) - 1 : 0; } },
|
|
|
+ { "tgl", (ia, ib, a, b) => {
|
|
|
+ var ii = (ia ? Registers[a] : a) + Registers[4];
|
|
|
+ if (ii >= instr.Count) return;
|
|
|
+
|
|
|
+ var ins = instr[ii];
|
|
|
+
|
|
|
+ if (ins.Length == 2)
|
|
|
+ ins[0] = ins[0] == "inc" ? "dec" : "inc";
|
|
|
+ else
|
|
|
+ ins[0] = ins[0] == "jnz" ? "cpy" : "jnz";
|
|
|
+
|
|
|
+ instr[ii] = ins;
|
|
|
+ } }
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ for (; Registers[4] < instr.Count; Registers[4]++)
|
|
|
+ {
|
|
|
+ var i = instr[Registers[4]];
|
|
|
+
|
|
|
+ ParseLine(i, out int valuea, out int valueb, out bool isrega, out bool isregb);
|
|
|
+
|
|
|
+ Ops[i[0]](isrega, isregb, valuea, valueb);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static void ParseLine(string[] i, out int valuea, out int valueb, out bool isrega, out bool isregb)
|
|
|
+ {
|
|
|
+ valuea = 0;
|
|
|
+ valueb = 0;
|
|
|
+ isrega = false;
|
|
|
+ isregb = false;
|
|
|
+ if (i[1][0] >= 'a')
|
|
|
+ {
|
|
|
+ valuea = i[1][0] - 'a';
|
|
|
+ isrega = true;
|
|
|
+ }
|
|
|
+ else valuea = int.Parse(i[1]);
|
|
|
+
|
|
|
+ if (i.Length == 3)
|
|
|
+ {
|
|
|
+ if (i[2][0] >= 'a')
|
|
|
+ {
|
|
|
+ valueb = i[2][0] - 'a';
|
|
|
+ isregb = true;
|
|
|
+ }
|
|
|
+ else valueb = int.Parse(i[2]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|