about summary refs log tree commit diff
path: root/aoc2021
diff options
context:
space:
mode:
authorBen Harris <ben@tilde.team>2021-12-17 17:00:31 -0500
committerBen Harris <ben@tilde.team>2021-12-17 17:00:31 -0500
commit8459671c893180d09e69e5e67ff185df9e675ed5 (patch)
tree593e11d6e7fc5de7549e27b7d8acaca645059b3c /aoc2021
parent1938202174413562858506316bb9444c81a73852 (diff)
d17p2
Diffstat (limited to 'aoc2021')
-rw-r--r--aoc2021/Day17.cs31
1 files changed, 28 insertions, 3 deletions
diff --git a/aoc2021/Day17.cs b/aoc2021/Day17.cs
index a8256a9..225d1a4 100644
--- a/aoc2021/Day17.cs
+++ b/aoc2021/Day17.cs
@@ -6,7 +6,7 @@
 public sealed class Day17 : Day
 {
     private readonly List<int> _target;
-    
+
     public Day17() : base(17, "Trick Shot")
     {
         _target = Input.First()
@@ -24,5 +24,30 @@ public sealed class Day17 : Day
         return (initialYVelocity + 1) * initialYVelocity / 2;
     }
 
-    public override object Part2() => "";
-}
+    public override object Part2()
+    {
+        var successfulVelocities = new HashSet<(int x, int y)>();
+        var xMin = 1;
+        while (xMin * (xMin + 1) / 2 < _target[0]) xMin++;
+
+        for (var x = xMin; x <= _target[1]; x++)
+        for (var y = _target[2]; y < Math.Abs(_target[2]); y++)
+        {
+            int xVelocity = x, yVelocity = y, xPos = 0, yPos = 0;
+
+            while (xPos <= _target[1] && yPos >= _target[2])
+            {
+                xPos += xVelocity;
+                yPos += yVelocity;
+
+                if (xPos >= _target[0] && xPos <= _target[1] && yPos >= _target[2] && yPos <= _target[3])
+                    successfulVelocities.Add((x, y));
+
+                xVelocity = xVelocity == 0 ? 0 : xVelocity - 1;
+                yVelocity--;
+            }
+        }
+
+        return successfulVelocities.Count;
+    }
+}
\ No newline at end of file