Parcourir la source

Day 12 part 2

Lukas Angerer il y a 2 ans
Parent
commit
b9deb43b89
2 fichiers modifiés avec 25 ajouts et 12 suppressions
  1. 18 2
      Day12/Program.cs
  2. 7 10
      Day12/WellRecord.cs

+ 18 - 2
Day12/Program.cs

@@ -21,8 +21,7 @@ var sum = 0L;
 
 foreach (var r in records)
 {
-    r.Print();
-    var count = r.Count();
+    var count = r.Count(1);
     Console.WriteLine($"Count: {count}");
     sum += count;
 }
@@ -30,4 +29,21 @@ foreach (var r in records)
 Console.WriteLine();
 Console.WriteLine($"Sum Total: {sum}");
 
+Console.WriteLine();
+Console.WriteLine();
+
+sum = 0;
+foreach (var r in records)
+{
+    var count = r.Count(5);
+    Console.WriteLine($"Count: {count}");
+    sum += count;
+}
+
+Console.WriteLine();
+Console.WriteLine($"Sum Total: {sum}");
+
+Console.WriteLine();
+Console.WriteLine($"Cached Counts: {WellRecord.MemoMap.Count}");
+
 return 0;

+ 7 - 10
Day12/WellRecord.cs

@@ -7,7 +7,7 @@ public class WellRecord
     private readonly string _record;
     private readonly int[] _checksum;
     
-    private static readonly Dictionary<string, long> MemoMap = new Dictionary<string, long>();
+    public static readonly Dictionary<string, long> MemoMap = new Dictionary<string, long>();
 
     public WellRecord(string record, int[] checksum)
     {
@@ -15,14 +15,13 @@ public class WellRecord
         _checksum = checksum;
     }
 
-    public void Print()
+    public long Count(int multiply)
     {
-        Console.WriteLine($"{_record} => {string.Join(", ", _checksum)}");
-    }
-
-    public long Count()
-    {
-        return CountPermuations(_record, _checksum);
+        var input = string.Join("?", Enumerable.Repeat(_record, multiply));
+        var values = Enumerable.Repeat(_checksum, multiply).SelectMany(x => x).ToArray();
+        
+        Console.WriteLine($"{input} => {string.Join(", ", values)}");
+        return CountPermuations(input, values);
     }
 
     private long CountPermuations(string tail, int[] values)
@@ -30,7 +29,6 @@ public class WellRecord
         var key = Key(tail, values);
         if (MemoMap.TryGetValue(key, out long count))
         {
-            Console.WriteLine($"Memoized {key}: {count}");
             return count;
         }
 
@@ -67,7 +65,6 @@ public class WellRecord
         }
 
         var result = dotCount + hashCount;
-        Console.WriteLine($"Computed {key}: {result}");
         MemoMap[key] = result;
         return result;
     }