bastien.monsarrat 6 年之前
父节点
当前提交
0ab3159cdd
共有 7 个文件被更改,包括 175 次插入0 次删除
  1. 12 0
      Adv2016.sln
  2. 9 0
      D23.1/D23.1.csproj
  3. 90 0
      D23.1/Program.cs
  4. 8 0
      D23.1/Properties/launchSettings.json
  5. 26 0
      D23.1/input.txt
  6. 13 0
      D23.2/D23.2.csproj
  7. 17 0
      D23.2/Program.cs

+ 12 - 0
Adv2016.sln

@@ -77,6 +77,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D21.2", "D21.2\D21.2.csproj
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D22", "D22.1\D22.csproj", "{82C72A7C-701D-4BC9-BE5B-75B9A9586700}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D23.1", "D23.1\D23.1.csproj", "{D59CC08C-A2CA-497A-AF77-5B81CA305179}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D23.2", "D23.2\D23.2.csproj", "{FAA8466D-15B7-46FC-A6CD-ECE8EDC8EBD1}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -231,6 +235,14 @@ Global
 		{82C72A7C-701D-4BC9-BE5B-75B9A9586700}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{82C72A7C-701D-4BC9-BE5B-75B9A9586700}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{82C72A7C-701D-4BC9-BE5B-75B9A9586700}.Release|Any CPU.Build.0 = Release|Any CPU
+		{D59CC08C-A2CA-497A-AF77-5B81CA305179}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{D59CC08C-A2CA-497A-AF77-5B81CA305179}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{D59CC08C-A2CA-497A-AF77-5B81CA305179}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{D59CC08C-A2CA-497A-AF77-5B81CA305179}.Release|Any CPU.Build.0 = Release|Any CPU
+		{FAA8466D-15B7-46FC-A6CD-ECE8EDC8EBD1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{FAA8466D-15B7-46FC-A6CD-ECE8EDC8EBD1}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{FAA8466D-15B7-46FC-A6CD-ECE8EDC8EBD1}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{FAA8466D-15B7-46FC-A6CD-ECE8EDC8EBD1}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 9 - 0
D23.1/D23.1.csproj

@@ -0,0 +1,9 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>netcoreapp2.1</TargetFramework>
+    <RootNamespace>D23._1</RootNamespace>
+  </PropertyGroup>
+
+</Project>

+ 90 - 0
D23.1/Program.cs

@@ -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]);
+            }
+        }
+    }
+}

+ 8 - 0
D23.1/Properties/launchSettings.json

@@ -0,0 +1,8 @@
+{
+  "profiles": {
+    "D23.1": {
+      "commandName": "Project",
+      "commandLineArgs": "\"..\\..\\..\\..\\D23.1\\input.txt\""
+    }
+  }
+}

+ 26 - 0
D23.1/input.txt

@@ -0,0 +1,26 @@
+cpy a b
+dec b
+cpy a d
+cpy 0 a
+cpy b c
+inc a
+dec c
+jnz c -2
+dec d
+jnz d -5
+dec b
+cpy b c
+cpy c d
+dec d
+inc c
+jnz d -2
+tgl c
+cpy -16 c
+jnz 1 c
+cpy 79 c
+jnz 74 d
+inc a
+inc d
+jnz d -2
+inc c
+jnz c -5

+ 13 - 0
D23.2/D23.2.csproj

@@ -0,0 +1,13 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <OutputType>Exe</OutputType>
+    <TargetFramework>netcoreapp2.1</TargetFramework>
+    <RootNamespace>D23._2</RootNamespace>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="..\D23.1\D23.1.csproj" />
+  </ItemGroup>
+
+</Project>

+ 17 - 0
D23.2/Program.cs

@@ -0,0 +1,17 @@
+using System;
+using static D23._1.Program;
+
+namespace D23._2
+{
+    class Program
+    {
+        static void Main(string[] args)
+        {
+            int[] Registers = new int[5] { 12, 0, 0, 0, 0 };
+
+            Work(args, Registers);
+
+            Console.WriteLine($"The answer is : {Registers[0]}");
+        }
+    }
+}