about summary refs log tree commit diff
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
parent1938202174413562858506316bb9444c81a73852 (diff)
d17p2
-rw-r--r--aoc2021.test/DayTests.cs2
-rw-r--r--aoc2021/Day17.cs31
2 files changed, 29 insertions, 4 deletions
diff --git a/aoc2021.test/DayTests.cs b/aoc2021.test/DayTests.cs
index 46c0c98..7c7f7b7 100644
--- a/aoc2021.test/DayTests.cs
+++ b/aoc2021.test/DayTests.cs
@@ -37,7 +37,7 @@ public class DayTests
     [DataRow(typeof(Day14), "5656", "12271437788530")]
     [DataRow(typeof(Day15), "702", "2955")]
     [DataRow(typeof(Day16), "852", "19348959966392")]
-    [DataRow(typeof(Day17), "12090", "")]
+    [DataRow(typeof(Day17), "12090", "5059")]
     public void CheckAllDays(Type dayType, string part1, string part2)
     {
         var s = Stopwatch.StartNew();
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