bastien.monsarrat 6 年之前
父节点
当前提交
5695a3a920
共有 8 个文件被更改,包括 309 次插入7 次删除
  1. 19 7
      Adv2015.sln
  2. 9 0
      D13.1/D13.1.csproj
  3. 162 0
      D13.1/Program.cs
  4. 8 0
      D13.1/Properties/launchSettings.json
  5. 56 0
      D13.1/input.txt
  6. 13 0
      D13.2/D13.2.csproj
  7. 34 0
      D13.2/Program.cs
  8. 8 0
      D13.2/Properties/launchSettings.json

+ 19 - 7
Adv2015.sln

@@ -33,19 +33,23 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D07.2", "D07.2\D07.2.csproj
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D08.1", "D08.1\D08.1.csproj", "{2FC7996C-DBC8-40B6-9870-8996179A3AAC}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D08.2", "D08.2\D08.2.csproj", "{FBDA595A-9E1A-40DF-BC1D-C2393DF69F51}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D08.2", "D08.2\D08.2.csproj", "{FBDA595A-9E1A-40DF-BC1D-C2393DF69F51}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D09", "D09.1\D09.csproj", "{8680B3A9-FBC7-4432-857A-9FE81ED4590B}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D09", "D09.1\D09.csproj", "{8680B3A9-FBC7-4432-857A-9FE81ED4590B}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D10.1", "D10.1\D10.1.csproj", "{D720AFF3-4AE8-4083-8AC9-FE620A260D2A}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D10.1", "D10.1\D10.1.csproj", "{D720AFF3-4AE8-4083-8AC9-FE620A260D2A}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D10.2", "D10.2\D10.2.csproj", "{F7E0673F-04A5-414D-9DD6-7E442DEAAE24}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D10.2", "D10.2\D10.2.csproj", "{F7E0673F-04A5-414D-9DD6-7E442DEAAE24}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D11", "D11.1\D11.csproj", "{D4001022-526D-4548-BB09-6F9DFD03B743}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D11", "D11.1\D11.csproj", "{D4001022-526D-4548-BB09-6F9DFD03B743}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D12.1", "D12.1\D12.1.csproj", "{D6E6034D-6F56-4845-A9C1-D18092C4A8AC}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D12.1", "D12.1\D12.1.csproj", "{D6E6034D-6F56-4845-A9C1-D18092C4A8AC}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D12.2", "D12.2\D12.2.csproj", "{DFA340DE-DDF5-4E99-949C-4680B3E8F2F6}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D12.2", "D12.2\D12.2.csproj", "{DFA340DE-DDF5-4E99-949C-4680B3E8F2F6}"
+EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "D13.1", "D13.1\D13.1.csproj", "{38FBCA6A-29F0-4FC8-B0DB-616DDC27E459}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "D13.2", "D13.2\D13.2.csproj", "{46445805-4831-436D-A531-9D973306DD2E}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -141,6 +145,14 @@ Global
 		{DFA340DE-DDF5-4E99-949C-4680B3E8F2F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{DFA340DE-DDF5-4E99-949C-4680B3E8F2F6}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{DFA340DE-DDF5-4E99-949C-4680B3E8F2F6}.Release|Any CPU.Build.0 = Release|Any CPU
+		{38FBCA6A-29F0-4FC8-B0DB-616DDC27E459}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{38FBCA6A-29F0-4FC8-B0DB-616DDC27E459}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{38FBCA6A-29F0-4FC8-B0DB-616DDC27E459}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{38FBCA6A-29F0-4FC8-B0DB-616DDC27E459}.Release|Any CPU.Build.0 = Release|Any CPU
+		{46445805-4831-436D-A531-9D973306DD2E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{46445805-4831-436D-A531-9D973306DD2E}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{46445805-4831-436D-A531-9D973306DD2E}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{46445805-4831-436D-A531-9D973306DD2E}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

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

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

+ 162 - 0
D13.1/Program.cs

@@ -0,0 +1,162 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text.RegularExpressions;
+
+namespace D13._1
+{
+    public class Table
+    {
+        Person First = null;
+        Person Last = null;
+
+        public void Sit(Person p)
+        {
+            p.Left = null;
+            p.Right = null;
+
+            if (Last == null)
+            {
+                p.Right = p;
+                p.Left = p;
+
+                Last = p;
+                First = p;
+                return;
+            }
+
+            p.Left = Last;
+            p.Right = First;
+
+            p.Left.Right= p;
+            Last.Right = p;
+
+            p.Right.Left= p;
+            First.Left = p;
+
+            Last = p;
+        }
+
+        public bool Contains(Person p)
+        {
+            bool found = false;
+            var cur = First;
+            while (cur != null && found == false)
+            {
+                if (cur == p) found = true;
+                cur = cur.Right;
+                if (cur == First) break;
+            }
+            return found;
+        }
+
+        public int Count(Dictionary<(string, string), int> preferences)
+        {
+            int count = 0;
+            var cur = First;
+            while (cur != null)
+            {
+                count += preferences[(cur.Name, cur.Left.Name)] + preferences[(cur.Name, cur.Right.Name)];
+
+                cur = cur.Right;
+                if (cur == First) break;
+            }
+            return count;
+        }
+    }
+
+    public class Person
+    {
+        public string Name { get; }
+        public Person(string name) => Name = name;
+        public Person Left;
+        public Person Right;
+
+        public override bool Equals(object obj)
+        {
+            var person = obj as Person;
+            return person != null &&
+                   Name == person.Name;
+        }
+
+        public override int GetHashCode()
+        {
+            return HashCode.Combine(Name);
+        }
+    }
+
+
+    public class Program
+    {
+        static void Main(string[] args)
+        {
+            if (args.Length < 1) throw new ArgumentException();
+            if (File.Exists(args[0]) == false) throw new FileNotFoundException();
+
+            var persons = new HashSet<Person>();
+            var preferences = new Dictionary<(string, string), int>();
+
+            ParseFile(args[0], persons, preferences);
+
+            int best = SearchBestTablePlan(persons, preferences);
+
+            Console.WriteLine($"The answer is : {best}");
+        }
+
+        public static int SearchBestTablePlan(HashSet<Person> persons, Dictionary<(string, string), int> preferences)
+        {
+            var permList = GetPermutations(persons, persons.Count);
+
+            var best = 0;
+            foreach (var perm in permList)
+            {
+                var table = new Table();
+                foreach (var p in perm)
+                    table.Sit(p);
+
+                var count = table.Count(preferences);
+                if (count > best) best = count;
+            }
+
+            return best;
+        }
+
+        static IEnumerable<IEnumerable<T>> GetPermutations<T>(IEnumerable<T> list, int length)
+        {
+            if (length == 1) return list.Select(t => new T[] { t });
+            return GetPermutations(list, length - 1)
+                .SelectMany(t => list.Where(o => !t.Contains(o)),
+                    (t1, t2) => t1.Concat(new T[] { t2 }));
+        }
+
+        public static void ParseFile(string f, HashSet<Person> persons, Dictionary<(string, string), int> preferences)
+        {
+            var regex = new Regex(@"(?<l>\w+) would (?<v>\w+) (?<h>\d+) happiness units by sitting next to (?<r>\w+).");
+            using (var file = File.OpenText(f))
+            {
+                while (true)
+                {
+                    var line = file.ReadLine();
+                    if (line == null) break;
+
+                    var result = regex.Match(line);
+
+                    int multiplier = 0;
+                    if (result.Groups["v"].Value == "lose") multiplier = -1;
+                    if (result.Groups["v"].Value == "gain") multiplier = 1;
+
+                    var happiness = int.Parse(result.Groups["h"].Value);
+
+                    var left = result.Groups["l"].Value;
+                    var right = result.Groups["r"].Value;
+
+                    persons.Add(new Person(left));
+                    persons.Add(new Person(right));
+
+                    preferences.Add((left, right), happiness * multiplier);
+                }
+            }
+        }
+    }
+}

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

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

+ 56 - 0
D13.1/input.txt

@@ -0,0 +1,56 @@
+Alice would gain 54 happiness units by sitting next to Bob.
+Alice would lose 81 happiness units by sitting next to Carol.
+Alice would lose 42 happiness units by sitting next to David.
+Alice would gain 89 happiness units by sitting next to Eric.
+Alice would lose 89 happiness units by sitting next to Frank.
+Alice would gain 97 happiness units by sitting next to George.
+Alice would lose 94 happiness units by sitting next to Mallory.
+Bob would gain 3 happiness units by sitting next to Alice.
+Bob would lose 70 happiness units by sitting next to Carol.
+Bob would lose 31 happiness units by sitting next to David.
+Bob would gain 72 happiness units by sitting next to Eric.
+Bob would lose 25 happiness units by sitting next to Frank.
+Bob would lose 95 happiness units by sitting next to George.
+Bob would gain 11 happiness units by sitting next to Mallory.
+Carol would lose 83 happiness units by sitting next to Alice.
+Carol would gain 8 happiness units by sitting next to Bob.
+Carol would gain 35 happiness units by sitting next to David.
+Carol would gain 10 happiness units by sitting next to Eric.
+Carol would gain 61 happiness units by sitting next to Frank.
+Carol would gain 10 happiness units by sitting next to George.
+Carol would gain 29 happiness units by sitting next to Mallory.
+David would gain 67 happiness units by sitting next to Alice.
+David would gain 25 happiness units by sitting next to Bob.
+David would gain 48 happiness units by sitting next to Carol.
+David would lose 65 happiness units by sitting next to Eric.
+David would gain 8 happiness units by sitting next to Frank.
+David would gain 84 happiness units by sitting next to George.
+David would gain 9 happiness units by sitting next to Mallory.
+Eric would lose 51 happiness units by sitting next to Alice.
+Eric would lose 39 happiness units by sitting next to Bob.
+Eric would gain 84 happiness units by sitting next to Carol.
+Eric would lose 98 happiness units by sitting next to David.
+Eric would lose 20 happiness units by sitting next to Frank.
+Eric would lose 6 happiness units by sitting next to George.
+Eric would gain 60 happiness units by sitting next to Mallory.
+Frank would gain 51 happiness units by sitting next to Alice.
+Frank would gain 79 happiness units by sitting next to Bob.
+Frank would gain 88 happiness units by sitting next to Carol.
+Frank would gain 33 happiness units by sitting next to David.
+Frank would gain 43 happiness units by sitting next to Eric.
+Frank would gain 77 happiness units by sitting next to George.
+Frank would lose 3 happiness units by sitting next to Mallory.
+George would lose 14 happiness units by sitting next to Alice.
+George would lose 12 happiness units by sitting next to Bob.
+George would lose 52 happiness units by sitting next to Carol.
+George would gain 14 happiness units by sitting next to David.
+George would lose 62 happiness units by sitting next to Eric.
+George would lose 18 happiness units by sitting next to Frank.
+George would lose 17 happiness units by sitting next to Mallory.
+Mallory would lose 36 happiness units by sitting next to Alice.
+Mallory would gain 76 happiness units by sitting next to Bob.
+Mallory would lose 34 happiness units by sitting next to Carol.
+Mallory would gain 37 happiness units by sitting next to David.
+Mallory would gain 40 happiness units by sitting next to Eric.
+Mallory would gain 18 happiness units by sitting next to Frank.
+Mallory would gain 7 happiness units by sitting next to George.

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

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

+ 34 - 0
D13.2/Program.cs

@@ -0,0 +1,34 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+using static D13._1.Program;
+using D13._1;
+
+namespace D13._2
+{
+    class Program
+    {
+        static void Main(string[] args)
+        {
+            if (args.Length < 1) throw new ArgumentException();
+            if (File.Exists(args[0]) == false) throw new FileNotFoundException();
+
+            var persons = new HashSet<Person>();
+            var preferences = new Dictionary<(string, string), int>();
+
+            ParseFile(args[0], persons, preferences);
+
+            var me = new Person("Bastien");
+            foreach (var p in persons)
+            {
+                preferences.Add((me.Name, p.Name), 0);
+                preferences.Add((p.Name, me.Name), 0);
+            }
+            persons.Add(me);
+
+            int best = SearchBestTablePlan(persons, preferences);
+
+            Console.WriteLine($"The answer is : {best}");
+        }
+    }
+}

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

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