Program.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text.RegularExpressions;
  6. namespace D13._1
  7. {
  8. public class Table
  9. {
  10. Person First = null;
  11. Person Last = null;
  12. public void Sit(Person p)
  13. {
  14. p.Left = null;
  15. p.Right = null;
  16. if (Last == null)
  17. {
  18. p.Right = p;
  19. p.Left = p;
  20. Last = p;
  21. First = p;
  22. return;
  23. }
  24. p.Left = Last;
  25. p.Right = First;
  26. p.Left.Right= p;
  27. Last.Right = p;
  28. p.Right.Left= p;
  29. First.Left = p;
  30. Last = p;
  31. }
  32. public bool Contains(Person p)
  33. {
  34. bool found = false;
  35. var cur = First;
  36. while (cur != null && found == false)
  37. {
  38. if (cur == p) found = true;
  39. cur = cur.Right;
  40. if (cur == First) break;
  41. }
  42. return found;
  43. }
  44. public int Count(Dictionary<(string, string), int> preferences)
  45. {
  46. int count = 0;
  47. var cur = First;
  48. while (cur != null)
  49. {
  50. count += preferences[(cur.Name, cur.Left.Name)] + preferences[(cur.Name, cur.Right.Name)];
  51. cur = cur.Right;
  52. if (cur == First) break;
  53. }
  54. return count;
  55. }
  56. }
  57. public class Person
  58. {
  59. public string Name { get; }
  60. public Person(string name) => Name = name;
  61. public Person Left;
  62. public Person Right;
  63. public override bool Equals(object obj)
  64. {
  65. var person = obj as Person;
  66. return person != null &&
  67. Name == person.Name;
  68. }
  69. public override int GetHashCode()
  70. {
  71. return HashCode.Combine(Name);
  72. }
  73. }
  74. public class Program
  75. {
  76. static void Main(string[] args)
  77. {
  78. if (args.Length < 1) throw new ArgumentException();
  79. if (File.Exists(args[0]) == false) throw new FileNotFoundException();
  80. var persons = new HashSet<Person>();
  81. var preferences = new Dictionary<(string, string), int>();
  82. ParseFile(args[0], persons, preferences);
  83. int best = SearchBestTablePlan(persons, preferences);
  84. Console.WriteLine($"The answer is : {best}");
  85. }
  86. public static int SearchBestTablePlan(HashSet<Person> persons, Dictionary<(string, string), int> preferences)
  87. {
  88. var permList = GetPermutations(persons, persons.Count);
  89. var best = 0;
  90. foreach (var perm in permList)
  91. {
  92. var table = new Table();
  93. foreach (var p in perm)
  94. table.Sit(p);
  95. var count = table.Count(preferences);
  96. if (count > best) best = count;
  97. }
  98. return best;
  99. }
  100. static IEnumerable<IEnumerable<T>> GetPermutations<T>(IEnumerable<T> list, int length)
  101. {
  102. if (length == 1) return list.Select(t => new T[] { t });
  103. return GetPermutations(list, length - 1)
  104. .SelectMany(t => list.Where(o => !t.Contains(o)),
  105. (t1, t2) => t1.Concat(new T[] { t2 }));
  106. }
  107. public static void ParseFile(string f, HashSet<Person> persons, Dictionary<(string, string), int> preferences)
  108. {
  109. var regex = new Regex(@"(?<l>\w+) would (?<v>\w+) (?<h>\d+) happiness units by sitting next to (?<r>\w+).");
  110. using (var file = File.OpenText(f))
  111. {
  112. while (true)
  113. {
  114. var line = file.ReadLine();
  115. if (line == null) break;
  116. var result = regex.Match(line);
  117. int multiplier = 0;
  118. if (result.Groups["v"].Value == "lose") multiplier = -1;
  119. if (result.Groups["v"].Value == "gain") multiplier = 1;
  120. var happiness = int.Parse(result.Groups["h"].Value);
  121. var left = result.Groups["l"].Value;
  122. var right = result.Groups["r"].Value;
  123. persons.Add(new Person(left));
  124. persons.Add(new Person(right));
  125. preferences.Add((left, right), happiness * multiplier);
  126. }
  127. }
  128. }
  129. }
  130. }