Browse Source

Day 24 part 2 - had to give up on C# and resort to Python

Lukas Angerer 2 năm trước cách đây
mục cha
commit
5c18cdca60
3 tập tin đã thay đổi với 43 bổ sung3 xóa
  1. 2 2
      Day24/HailStorm.cs
  2. 34 0
      Day24/LuckyShot.cs
  3. 7 1
      Day24/Program.cs

+ 2 - 2
Day24/HailStorm.cs

@@ -17,8 +17,8 @@ public class HailStorm
             for (var j = i + 1; j < _hail.Count; j++)
             {
                 var intersection = _hail[i].IntersectionWith(_hail[j]);
-                Console.WriteLine($"{i} v {j} => {intersection.X} / {intersection.Y} @ {intersection.T1} & {intersection.T2}");
-                Console.WriteLine($"Within Limits: {intersection.IsValid(min, max)}");
+                //Console.WriteLine($"{i} v {j} => {intersection.X} / {intersection.Y} @ {intersection.T1} & {intersection.T2}");
+                //Console.WriteLine($"Within Limits: {intersection.IsValid(min, max)}");
                 if (intersection.IsValid(min, max))
                 {
                     count++;

+ 34 - 0
Day24/LuckyShot.cs

@@ -0,0 +1,34 @@
+namespace Day24;
+
+public class LuckyShot
+{
+    private readonly IList<Line3> _lines;
+
+    public LuckyShot(IList<Line3> lines)
+    {
+        _lines = lines;
+    }
+
+    public void Find()
+    {
+        var equations = new List<string>();
+        Console.WriteLine("import sympy");
+        Console.WriteLine();
+        Console.WriteLine("sx, sy, sz, vsx, vsy, vsz = sympy.symbols(\"sx, sy, sz, vsx, vsy, vsz\")");
+        Console.WriteLine();
+        Console.WriteLine("equations = []");
+        
+        for (var i = 0; i < 3; i++)
+        {
+            var h = _lines[i];
+            equations.Add($"equations.append(({h.Position.X} - sx) / (vsx - {h.Velocity.X}) - ({h.Position.Y} - sy) / (vsy - {h.Velocity.Y}))");
+            equations.Add($"equations.append(({h.Position.X} - sx) / (vsx - {h.Velocity.X}) - ({h.Position.Z} - sz) / (vsz - {h.Velocity.Z}))");
+        }
+        Console.WriteLine(string.Join("\n", equations));
+
+        Console.WriteLine();
+        Console.WriteLine("answers = sympy.solve(equations)");
+        Console.WriteLine("print(answers)");
+        Console.WriteLine("print(answers[0][sx] + answers[0][sy] + answers[0][sz])");
+    }
+}

+ 7 - 1
Day24/Program.cs

@@ -17,7 +17,7 @@ if (args.Length < 1)
 var inputFile = args[0];
 var isSample = inputFile.Contains("sample");
 var parser = new Parser();
-var lines = parser.Parse(inputFile);
+var lines = parser.Parse(inputFile).ToList();
 var hailStorm = new HailStorm(lines.Select(l => (Line)l).ToList());
 var min = isSample ? 7d : 200000000000000d;
 var max = isSample ? 27d : 400000000000000d;
@@ -26,4 +26,10 @@ var count = hailStorm.Count(min, max);
 Console.WriteLine();
 Console.WriteLine($"Total: {count}");
 
+Console.WriteLine();
+Console.WriteLine("Just put the following code into Python. It's much easier that way.");
+Console.WriteLine("-----");
+var lucky = new LuckyShot(lines);
+lucky.Find();
+
 return 0;