bastien.monsarrat 6 жил өмнө
parent
commit
7e3ef079f3

+ 6 - 0
Adv2015.sln

@@ -83,6 +83,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D22.1", "D22.1\D22.1.csproj
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D22.2", "D22.2\D22.2.csproj", "{63AEC02B-5420-4D45-B48E-BBBEA9F6FA5F}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D23.1", "D23.1\D23.1.csproj", "{777A3D19-DEFC-4E94-9E7B-7D8F1E87027B}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -249,6 +251,10 @@ Global
 		{63AEC02B-5420-4D45-B48E-BBBEA9F6FA5F}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{63AEC02B-5420-4D45-B48E-BBBEA9F6FA5F}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{63AEC02B-5420-4D45-B48E-BBBEA9F6FA5F}.Release|Any CPU.Build.0 = Release|Any CPU
+		{777A3D19-DEFC-4E94-9E7B-7D8F1E87027B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{777A3D19-DEFC-4E94-9E7B-7D8F1E87027B}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{777A3D19-DEFC-4E94-9E7B-7D8F1E87027B}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{777A3D19-DEFC-4E94-9E7B-7D8F1E87027B}.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>

+ 84 - 0
D23.1/Program.cs

@@ -0,0 +1,84 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+
+namespace D23._1
+{
+    public class Machine
+    {
+        public static uint[] Registers { get; set; }
+
+        public Machine() => Registers = new uint[2] { 0, 0 };
+
+        static public int I { get; set; }
+
+        public readonly Dictionary<string, Action<int, int>> OpCodes = new Dictionary<string, Action<int, int>>
+        {
+            { "hlf", (r, o) => Registers[r] /= 2 },
+            { "tpl", (r, o) => Registers[r] *= 3 },
+            { "inc", (r, o) => Registers[r] += 1 },
+            { "jmp", (o, _) => I += o - 1 },
+            { "jie", (r, o) => { if (Registers[r] % 2 == 0) I += o - 1; } },
+            { "jio", (r, o) => { if (Registers[r] == 1) I += o - 1; } },
+        };
+
+        public List<(string, int, int)> Code = new List<(string, int, int)>();
+
+        public void AddLine(string op, int p1, int p2) => Code.Add((op, p1, p2));
+
+        public bool Exec()
+        {
+            (string op, int p1, int p2) = Code[I];
+            OpCodes[op](p1, p2);
+            I++;
+
+            return I < Code.Count;
+        }
+    }
+
+    class Program
+    {
+        static void Main(string[] args)
+        {
+            if (args.Length < 1) throw new ArgumentException();
+            if (File.Exists(args[0]) == false) throw new FileNotFoundException();
+
+            var pc = ParseFile(args);
+
+            while (pc.Exec()) ;
+
+            Console.WriteLine($"Part 1 answer is {Machine.Registers[1]}");
+
+            Machine.Registers = new uint[] { 1, 0 };
+            Machine.I = 0;
+
+            while (pc.Exec()) ;
+
+            Console.WriteLine($"Part 2 answer is {Machine.Registers[1]}");
+        }
+
+        private static Machine ParseFile(string[] args)
+        {
+            var pc = new Machine();
+            using (var file = File.OpenText(args[0]))
+            {
+                while (true)
+                {
+                    var line = file.ReadLine();
+                    if (line == null) break;
+
+                    var lr = line.Split(" ", 2);
+                    var op = lr[0];
+                    var vlr = lr[1].Split(", ");
+
+                    int p1 = vlr[0] == "a" ? 0 : (vlr[0] == "b" ? 1 : int.Parse(vlr[0]));
+                    int p2 = vlr.Length > 1 ? int.Parse(vlr[1]) : 0;
+
+                    pc.AddLine(op, p1, p2);
+                }
+            }
+
+            return pc;
+        }
+    }
+}

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

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

+ 47 - 0
D23.1/input.txt

@@ -0,0 +1,47 @@
+jio a, +18
+inc a
+tpl a
+inc a
+tpl a
+tpl a
+tpl a
+inc a
+tpl a
+inc a
+tpl a
+inc a
+inc a
+tpl a
+tpl a
+tpl a
+inc a
+jmp +22
+tpl a
+inc a
+tpl a
+inc a
+inc a
+tpl a
+inc a
+tpl a
+inc a
+inc a
+tpl a
+tpl a
+inc a
+inc a
+tpl a
+inc a
+inc a
+tpl a
+inc a
+inc a
+tpl a
+jio a, +8
+inc b
+jie a, +4
+tpl a
+inc a
+jmp +2
+hlf a
+jmp -7