bastien.monsarrat 6 年之前
父节点
当前提交
45a5cafe22
共有 6 个文件被更改,包括 127 次插入4 次删除
  1. 6 0
      Adv2018.sln
  2. 4 4
      D19.1/Program.cs
  3. 13 0
      D21.1/D21.1and2.csproj
  4. 64 0
      D21.1/Program.cs
  5. 8 0
      D21.1/Properties/launchSettings.json
  6. 32 0
      D21.1/input.txt

+ 6 - 0
Adv2018.sln

@@ -85,6 +85,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D19.2", "D19.2\D19.2.csproj
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D20.1and2", "D20.1\D20.1and2.csproj", "{BA7DC3A0-9406-46BD-8B2A-CAE906EC5F22}"
 EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D21.1and2", "D21.1\D21.1and2.csproj", "{47F415F2-1D92-4A6F-89D3-6B3A958A0BDC}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -255,6 +257,10 @@ Global
 		{BA7DC3A0-9406-46BD-8B2A-CAE906EC5F22}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{BA7DC3A0-9406-46BD-8B2A-CAE906EC5F22}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{BA7DC3A0-9406-46BD-8B2A-CAE906EC5F22}.Release|Any CPU.Build.0 = Release|Any CPU
+		{47F415F2-1D92-4A6F-89D3-6B3A958A0BDC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{47F415F2-1D92-4A6F-89D3-6B3A958A0BDC}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{47F415F2-1D92-4A6F-89D3-6B3A958A0BDC}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{47F415F2-1D92-4A6F-89D3-6B3A958A0BDC}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 4 - 4
D19.1/Program.cs

@@ -9,7 +9,7 @@ namespace D19._1
     {
         protected ProcessUnit CPU { get; private set; }
 
-        uint Ip = 0;
+        protected uint Ip = 0;
         List<(uint op, uint[] param)> Program;
 
         Parser InstrParser = new Parser();
@@ -42,7 +42,7 @@ namespace D19._1
 
         protected internal class Parser
         {
-            Dictionary<string, uint> OpCodes = new Dictionary<string, uint>
+            readonly Dictionary<string, uint> OpCodes = new Dictionary<string, uint>
             {
                 { "addr", 0 },
                 { "addi", 1 },
@@ -106,10 +106,10 @@ namespace D19._1
 
             public OpCode[] OpCodes;
 
-            public void SetRegisters(uint[] registers)
+            public void RefreshRegisters()
             {
                 for (var i = 0; i < Registers.Length; ++i)
-                    Registers[i] = registers[i];
+                    Registers[i] = 0;
             }
 
             public ProcessUnit()

+ 13 - 0
D21.1/D21.1and2.csproj

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

+ 64 - 0
D21.1/Program.cs

@@ -0,0 +1,64 @@
+using D19._1;
+using System;
+using System.Collections.Generic;
+using System.IO;
+
+namespace D21._1
+{
+    class ManagedComputer : Computer
+    {
+        public new uint Ip => base.Ip;
+        public uint GetRegister(int id) => CPU.Registers[id];
+        public void RefreshRegisters(uint value)
+        {
+            CPU.RefreshRegisters();
+            CPU.Registers[0] = value;
+        }
+    }
+
+    class Program
+    {
+        static void Main(string[] args)
+        {
+            if (args.Length < 1) return;
+            if (File.Exists(args[0]) == false) return;
+
+            var pc = new ManagedComputer();
+            var file = File.OpenText(args[0]);
+            pc.LoadProgram(file);
+            file.Close();
+
+            pc.DumpRegisters();
+
+            Console.WriteLine("==== EXECUTING PROGRAM ===");
+
+            bool continu = true;
+            uint first = 0, last = 0;
+            HashSet<uint> all = new HashSet<uint>();
+            for (uint i = 0; continu ; i++)
+            {
+                pc.RefreshRegisters(i);
+
+                do
+                {
+                    continu = pc.ExecInstruction();
+
+                    if (pc.Ip == 29 && first == 0)
+                        first = pc.GetRegister(1);
+                    if (pc.Ip == 29)
+                    {
+                        var l = pc.GetRegister(1);
+                        if (all.Add(l) == true && l < Int32.MaxValue) last = l;
+                        else continu = false;
+                    }
+
+                } while (continu);
+            }
+
+            Console.WriteLine("====== PROGRAM ENDED =====");
+            pc.DumpRegisters();
+            Console.WriteLine($"Part 1 answer is : {first}");
+            Console.WriteLine($"Part 2 answer is : {last}");
+        }
+    }
+}

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

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

+ 32 - 0
D21.1/input.txt

@@ -0,0 +1,32 @@
+#ip 4
+seti 123 0 1
+bani 1 456 1
+eqri 1 72 1
+addr 1 4 4
+seti 0 0 4
+seti 0 3 1
+bori 1 65536 2
+seti 7902108 7 1
+bani 2 255 5
+addr 1 5 1
+bani 1 16777215 1
+muli 1 65899 1
+bani 1 16777215 1
+gtir 256 2 5
+addr 5 4 4
+addi 4 1 4
+seti 27 0 4
+seti 0 0 5
+addi 5 1 3
+muli 3 256 3
+gtrr 3 2 3
+addr 3 4 4
+addi 4 1 4
+seti 25 2 4
+addi 5 1 5
+seti 17 2 4
+setr 5 1 2
+seti 7 2 4
+eqrr 1 0 5
+addr 5 4 4
+seti 5 9 4