bastien.monsarrat 6 years ago
parent
commit
2056f8e2d1

+ 12 - 0
Adv2016.sln

@@ -43,6 +43,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D10.2", "D10.2\D10.2.csproj
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D11", "D11.1\D11.csproj", "{560638CF-1FEA-4962-B944-B079E89B1CF0}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D12.1", "D12.1\D12.1.csproj", "{CE31711D-F000-4A27-811A-4C5E5F92FE7F}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D12.2", "D12.2\D12.2.csproj", "{873596CF-84EA-4318-A43F-909284891A82}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -129,6 +133,14 @@ Global
 		{560638CF-1FEA-4962-B944-B079E89B1CF0}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{560638CF-1FEA-4962-B944-B079E89B1CF0}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{560638CF-1FEA-4962-B944-B079E89B1CF0}.Release|Any CPU.Build.0 = Release|Any CPU
+		{CE31711D-F000-4A27-811A-4C5E5F92FE7F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{CE31711D-F000-4A27-811A-4C5E5F92FE7F}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{CE31711D-F000-4A27-811A-4C5E5F92FE7F}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{CE31711D-F000-4A27-811A-4C5E5F92FE7F}.Release|Any CPU.Build.0 = Release|Any CPU
+		{873596CF-84EA-4318-A43F-909284891A82}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{873596CF-84EA-4318-A43F-909284891A82}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{873596CF-84EA-4318-A43F-909284891A82}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{873596CF-84EA-4318-A43F-909284891A82}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

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

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

+ 60 - 0
D12.1/Program.cs

@@ -0,0 +1,60 @@
+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]);
+            }
+        }
+    }
+}

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

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

+ 23 - 0
D12.1/input.txt

@@ -0,0 +1,23 @@
+cpy 1 a
+cpy 1 b
+cpy 26 d
+jnz c 2
+jnz 1 5
+cpy 7 c
+inc d
+dec c
+jnz c -2
+cpy a c
+inc a
+dec b
+jnz b -2
+cpy c b
+dec d
+jnz d -6
+cpy 16 c
+cpy 12 d
+inc a
+dec d
+jnz d -2
+dec c
+jnz c -5

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

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

+ 17 - 0
D12.2/Program.cs

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

+ 8 - 0
D12.2/Properties/launchSettings.json

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