瀏覽代碼

Day 15 part 2

Lukas Angerer 2 年之前
父節點
當前提交
e3f0615bfe
共有 3 個文件被更改,包括 83 次插入5 次删除
  1. 51 3
      Day15/InitializationStep.cs
  2. 9 0
      Day15/Lens.cs
  3. 23 2
      Day15/Program.cs

+ 51 - 3
Day15/InitializationStep.cs

@@ -1,9 +1,57 @@
-namespace Day15;
+using System.Text.RegularExpressions;
 
-public record InitializationStep(string Instruction)
+namespace Day15;
+
+public partial class InitializationStep
 {
+    [GeneratedRegex(@"^(\w+)([=-])(\d+)?$")]
+    private partial Regex InstructionMatcher();
+    
+    private readonly string _instruction;
+    
+    public string Label { get; }
+    public char Operation { get; }
+    public int? FocalLength { get; }
+
+    public InitializationStep(string instruction)
+    {
+        _instruction = instruction;
+
+        var match = InstructionMatcher().Match(_instruction);
+        if (!match.Success)
+        {
+            throw new InvalidOperationException($"Invalid instruction {_instruction}");
+        }
+
+        Label = match.Groups[1].Value;
+        Operation = match.Groups[2].Value[0];
+        FocalLength = match.Groups[3].Success ? int.Parse(match.Groups[3].Value) : null;
+    }
+
+    public void Execute(List<List<Lens>> boxes)
+    {
+        var box = boxes[AsciiHash.Hash(Label)];
+        switch (Operation)
+        {
+            case '=':
+                var index = box.FindIndex(lens => lens.Label == Label);
+                if (index >= 0)
+                {
+                    box[index] = new Lens(Label, FocalLength!.Value);
+                }
+                else
+                {
+                    box.Add(new Lens(Label, FocalLength!.Value));
+                }
+                break;
+            case '-':
+                box.RemoveAll(lens => lens.Label == Label);
+                break;
+        }
+    }
+    
     public byte GetHash()
     {
-        return AsciiHash.Hash(Instruction);
+        return AsciiHash.Hash(_instruction);
     }
 }

+ 9 - 0
Day15/Lens.cs

@@ -0,0 +1,9 @@
+namespace Day15;
+
+public record Lens(string Label, int FocalLength)
+{
+    public string Visual()
+    {
+        return $"[{Label} {FocalLength}]";
+    }
+}

+ 23 - 2
Day15/Program.cs

@@ -16,17 +16,38 @@ if (args.Length < 1)
 
 var inputFile = args[0];
 var parser = new Parser();
-var steps = parser.Parse(inputFile);
+var steps = parser.Parse(inputFile).ToList();
 
 var sum = 0;
 foreach (var s in steps)
 {
     var hash = s.GetHash();
-    Console.WriteLine(hash);
     sum += hash;
 }
 
 Console.WriteLine();
 Console.WriteLine($"Total: {sum}");
 
+var boxes = Enumerable.Range(0, 256).Select(_ => new List<Lens>()).ToList();
+foreach (var s in steps)
+{
+    s.Execute(boxes);
+    foreach (var pair in boxes.Select((box, index) => (Box: box, Index: index)))
+    {
+        if (pair.Box.Count > 0)
+        {
+            var contents = string.Join(" ", pair.Box.Select(lens => lens.Visual()));
+            Console.WriteLine($"Box {pair.Index}: {contents}");
+        }
+    }
+    Console.WriteLine("-----");
+}
+
+var power = boxes
+    .SelectMany((box, boxIndex) => box.Select((lens, lensIndex) => (boxIndex + 1) * (lensIndex + 1) * lens.FocalLength))
+    .Sum();
+Console.WriteLine();
+Console.WriteLine($"Power: {power}");
+
+
 return 0;