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 _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