bastien.monsarrat 6 年之前
父節點
當前提交
0ecd6c85ae
共有 8 個文件被更改,包括 380 次插入0 次删除
  1. 12 0
      Adv2016.sln
  2. 9 0
      D21.1/D21.1.csproj
  3. 162 0
      D21.1/Program.cs
  4. 8 0
      D21.1/Properties/launchSettings.json
  5. 100 0
      D21.1/input.txt
  6. 13 0
      D21.2/D21.2.csproj
  7. 68 0
      D21.2/Program.cs
  8. 8 0
      D21.2/Properties/launchSettings.json

+ 12 - 0
Adv2016.sln

@@ -71,6 +71,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D20.1", "D20.1\D20.1.csproj
 EndProject
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D20.2", "D20.2\D20.2.csproj", "{0DF60CDC-1037-4AB7-8A5C-3B1198050D52}"
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D21.1", "D21.1\D21.1.csproj", "{B8F3FA32-46B0-4791-8014-09C8AC10595E}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D21.2", "D21.2\D21.2.csproj", "{BC4F812B-8D17-4E2F-A66F-5954CC49C347}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -213,6 +217,14 @@ Global
 		{0DF60CDC-1037-4AB7-8A5C-3B1198050D52}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{0DF60CDC-1037-4AB7-8A5C-3B1198050D52}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{0DF60CDC-1037-4AB7-8A5C-3B1198050D52}.Release|Any CPU.Build.0 = Release|Any CPU
+		{B8F3FA32-46B0-4791-8014-09C8AC10595E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{B8F3FA32-46B0-4791-8014-09C8AC10595E}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{B8F3FA32-46B0-4791-8014-09C8AC10595E}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{B8F3FA32-46B0-4791-8014-09C8AC10595E}.Release|Any CPU.Build.0 = Release|Any CPU
+		{BC4F812B-8D17-4E2F-A66F-5954CC49C347}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{BC4F812B-8D17-4E2F-A66F-5954CC49C347}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{BC4F812B-8D17-4E2F-A66F-5954CC49C347}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{BC4F812B-8D17-4E2F-A66F-5954CC49C347}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

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

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

+ 162 - 0
D21.1/Program.cs

@@ -0,0 +1,162 @@
+using System;
+using System.Collections;
+using System.IO;
+using System.Linq;
+
+namespace D21._1
+{
+    public class R : StreamReader, IEnumerable
+    {
+        public R(Stream stream) : base(stream) { }
+        public IEnumerator GetEnumerator()
+        {
+            while (true)
+            {
+                var line = ReadLine();
+                if (line == null) break;
+                yield return line;
+            }
+        }
+    }
+    public class Program
+    {
+        public static char[] Swap(char[] input, int x, int y)
+        {
+            var result = input.Clone() as char[];
+            var tmp = result[x];
+            result[x] = result[y];
+            result[y] = tmp;
+            return result;
+        }
+
+        public static char[] Swap(char[] input, char x, char y)
+        {
+            var result = new char[input.Length];
+
+            for (var i = 0; i < input.Length; i++)
+            {
+                if (input[i] == x) result[i] = y;
+                else if (input[i] == y) result[i] = x;
+                else result[i] = input[i];
+            }
+
+            return result;
+        }
+
+        public static char[] RotateLeft(char[] input, int x) => Rotate(input, 1, x);
+
+        public static char[] RotateRight(char[] input, int x) => Rotate(input, -1, x);
+
+        static char[] Rotate(char[] input, int dir, int x)
+        {
+            var result = new char[input.Length];
+
+            for (var i = 0; i < input.Length; i++)
+            {
+                int source = i + (dir * x);
+                while (source < 0) source += input.Length;
+                while (source >= input.Length) source -= input.Length;
+                result[i] = input[source];
+            }
+
+            return result;
+        }
+
+        public static int IndexOf(char[] input, char l)
+        {
+            int index = -1;
+            for (int i = 0; i < input.Length; i++)
+            {
+                char c = input[i];
+                if (c == l)
+                {
+                    index = i;
+                    break;
+                }
+            }
+            return index;
+        }
+
+        public static char[] Rotate(char[] input, char x)
+        {
+            var index = IndexOf(input, x);
+            if (index == -1) throw new Exception();
+            return RotateRight(input, (index >= 4 ? index + 2 : index + 1));
+        }
+
+        public static char[] Reverse(char[] input, int x, int y)
+        {
+            var result = new char[input.Length];
+
+            for (var i = 0; i < x; i++) result[i] = input[i];
+            for (int i = x, j = y; i <= y; i++, j--) result[i] = input[j];
+            for (var i = y + 1; i < input.Length; i++) result[i] = input[i];
+
+            return result;
+        }
+
+        public static char[] Move(char[] input, int x, int y)
+        {
+            var result = input.Clone() as char[];
+
+            if (x < y) Array.Copy(result, x + 1, result, x, y - x);
+            else Array.Copy(result, y, result, y + 1, x - y);
+
+            result[y] = input[x];
+
+            return result;
+        }
+
+        static void Main(string[] args)
+        {
+            var scrambling = "abcdefgh".ToCharArray();
+
+            using (var file = new R(File.OpenText(args[0]).BaseStream))
+            {
+                foreach (string line in file)
+                {
+                    if (line.StartsWith("swap position "))
+                    {
+                        var i = int.Parse(line.Split(" ")[2]);
+                        var j = int.Parse(line.Split(" ")[5]);
+                        scrambling = Swap(scrambling, i, j);
+                    }
+                    else if (line.StartsWith("swap letter "))
+                    {
+                        var i = line.Split(" ")[2][0];
+                        var j = line.Split(" ")[5][0];
+                        scrambling = Swap(scrambling, i, j);
+                    }
+                    else if (line.StartsWith("rotate right "))
+                    {
+                        var i = int.Parse(line.Split(" ")[2]);
+                        scrambling = RotateRight(scrambling, i);
+                    }
+                    else if (line.StartsWith("rotate left "))
+                    {
+                        var i = int.Parse(line.Split(" ")[2]);
+                        scrambling = RotateLeft(scrambling, i);
+                    }
+                    else if (line.StartsWith("rotate based on position of letter "))
+                    {
+                        scrambling = Rotate(scrambling, line.Last());
+                    }
+                    else if (line.StartsWith("reverse positions "))
+                    {
+                        var i = int.Parse(line.Split(" ")[2]);
+                        var j = int.Parse(line.Split(" ")[4]);
+                        scrambling = Reverse(scrambling, i, j);
+                    }
+                    else if (line.StartsWith("move position "))
+                    {
+                        var i = int.Parse(line.Split(" ")[2]);
+                        var j = int.Parse(line.Split(" ")[5]);
+                        scrambling = Move(scrambling, i, j);
+                    }
+                }
+            }
+
+            Console.WriteLine(scrambling);
+        }
+    }
+}

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

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

+ 100 - 0
D21.1/input.txt

@@ -0,0 +1,100 @@
+rotate right 3 steps
+swap letter b with letter a
+move position 3 to position 4
+swap position 0 with position 7
+swap letter f with letter h
+rotate based on position of letter f
+rotate based on position of letter b
+swap position 3 with position 0
+swap position 6 with position 1
+move position 4 to position 0
+rotate based on position of letter d
+swap letter d with letter h
+reverse positions 5 through 6
+rotate based on position of letter h
+reverse positions 4 through 5
+move position 3 to position 6
+rotate based on position of letter e
+rotate based on position of letter c
+rotate right 2 steps
+reverse positions 5 through 6
+rotate right 3 steps
+rotate based on position of letter b
+rotate right 5 steps
+swap position 5 with position 6
+move position 6 to position 4
+rotate left 0 steps
+swap position 3 with position 5
+move position 4 to position 7
+reverse positions 0 through 7
+rotate left 4 steps
+rotate based on position of letter d
+rotate left 3 steps
+swap position 0 with position 7
+rotate based on position of letter e
+swap letter e with letter a
+rotate based on position of letter c
+swap position 3 with position 2
+rotate based on position of letter d
+reverse positions 2 through 4
+rotate based on position of letter g
+move position 3 to position 0
+move position 3 to position 5
+swap letter b with letter d
+reverse positions 1 through 5
+reverse positions 0 through 1
+rotate based on position of letter a
+reverse positions 2 through 5
+swap position 1 with position 6
+swap letter f with letter e
+swap position 5 with position 1
+rotate based on position of letter a
+move position 1 to position 6
+swap letter e with letter d
+reverse positions 4 through 7
+swap position 7 with position 5
+swap letter c with letter g
+swap letter e with letter g
+rotate left 4 steps
+swap letter c with letter a
+rotate left 0 steps
+swap position 0 with position 1
+reverse positions 1 through 4
+rotate based on position of letter d
+swap position 4 with position 2
+rotate right 0 steps
+swap position 1 with position 0
+swap letter c with letter a
+swap position 7 with position 3
+swap letter a with letter f
+reverse positions 3 through 7
+rotate right 1 step
+swap letter h with letter c
+move position 1 to position 3
+swap position 4 with position 2
+rotate based on position of letter b
+reverse positions 5 through 6
+move position 5 to position 3
+swap letter b with letter g
+rotate right 6 steps
+reverse positions 6 through 7
+swap position 2 with position 5
+rotate based on position of letter e
+swap position 1 with position 7
+swap position 1 with position 5
+reverse positions 2 through 7
+reverse positions 5 through 7
+rotate left 3 steps
+rotate based on position of letter b
+rotate left 3 steps
+swap letter e with letter c
+rotate based on position of letter a
+swap letter f with letter a
+swap position 0 with position 6
+swap position 4 with position 7
+reverse positions 0 through 5
+reverse positions 3 through 5
+swap letter d with letter e
+move position 0 to position 7
+move position 1 to position 3
+reverse positions 4 through 7

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

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

+ 68 - 0
D21.2/Program.cs

@@ -0,0 +1,68 @@
+using D21._1;
+using static D21._1.Program;
+using System;
+using System.IO;
+using System.Linq;
+
+namespace D21._2
+{
+    class Program
+    {
+        static void Main(string[] args)
+        {
+            var scrambling = "fbgdceah".ToCharArray();
+
+            using (var f = new R(File.OpenText(args[0]).BaseStream))
+            {
+                var file = f.ReadToEnd().Split(new [] { "\n", "\r\n" }, StringSplitOptions.RemoveEmptyEntries).Reverse();
+                foreach (string line in file)
+                {
+                    if (line.StartsWith("swap position "))
+                    {
+                        var i = int.Parse(line.Split(" ")[2]);
+                        var j = int.Parse(line.Split(" ")[5]);
+                        scrambling = Swap(scrambling, i, j);
+                    }
+                    else if (line.StartsWith("swap letter "))
+                    {
+                        var i = line.Split(" ")[2][0];
+                        var j = line.Split(" ")[5][0];
+                        scrambling = Swap(scrambling, i, j);
+                    }
+                    else if (line.StartsWith("rotate right "))
+                    {
+                        var i = int.Parse(line.Split(" ")[2]);
+                        scrambling = RotateLeft(scrambling, i);
+                    }
+                    else if (line.StartsWith("rotate left "))
+                    {
+                        var i = int.Parse(line.Split(" ")[2]);
+                        scrambling = RotateRight(scrambling, i);
+                    }
+                    else if (line.StartsWith("rotate based on position of letter "))
+                    {
+                        var index = IndexOf(scrambling, line.Last());
+                        if (index == -1) throw new Exception();
+                        var scramblingS = scrambling.Clone() as char[];
+                        while (Rotate(scrambling, line.Last()).SequenceEqual(scramblingS) == false)
+                            scrambling = RotateLeft(scrambling, 1);
+                    }
+                    else if (line.StartsWith("reverse positions "))
+                    {
+                        var i = int.Parse(line.Split(" ")[2]);
+                        var j = int.Parse(line.Split(" ")[4]);
+                        scrambling = Reverse(scrambling, i, j);
+                    }
+                    else if (line.StartsWith("move position "))
+                    {
+                        var i = int.Parse(line.Split(" ")[2]);
+                        var j = int.Parse(line.Split(" ")[5]);
+                        scrambling = Move(scrambling, j, i);
+                    }
+                }
+            }
+
+            Console.WriteLine(scrambling);
+        }
+    }
+}

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

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