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