2022-12-03 05:55:49 +00:00
|
|
|
namespace AOC2021;
|
2022-10-27 15:42:05 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2022-12-03 05:41:38 +00:00
|
|
|
/// Day 17: <a href="https://adventofcode.com/2021/day/17"/>
|
2022-10-27 15:42:05 +00:00
|
|
|
/// </summary>
|
2023-09-20 18:38:58 +00:00
|
|
|
public sealed class Day17() : Day(2021, 17, "Trick Shot")
|
2022-10-27 15:42:05 +00:00
|
|
|
{
|
2022-12-08 19:40:40 +00:00
|
|
|
private List<int>? _target;
|
2022-10-27 15:42:05 +00:00
|
|
|
|
2023-12-01 07:30:47 +00:00
|
|
|
public override void ProcessInput() =>
|
2022-10-27 15:42:05 +00:00
|
|
|
_target = Input.First()
|
|
|
|
.Split(' ', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries)
|
|
|
|
.Skip(2)
|
|
|
|
.SelectMany(i => i.Split('=')[1].Split(".."))
|
|
|
|
.Select(i => i.TrimEnd(','))
|
|
|
|
.Select(int.Parse)
|
|
|
|
.ToList();
|
|
|
|
|
|
|
|
public override object Part1()
|
|
|
|
{
|
2022-12-08 19:40:40 +00:00
|
|
|
var initialYVelocity = Math.Abs(_target![2]) - 1;
|
2022-10-27 15:42:05 +00:00
|
|
|
return (initialYVelocity + 1) * initialYVelocity / 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
public override object Part2()
|
|
|
|
{
|
|
|
|
var successfulVelocities = new HashSet<(int x, int y)>();
|
|
|
|
var xMin = 1;
|
2022-12-08 19:40:40 +00:00
|
|
|
while (xMin * (xMin + 1) / 2 < _target![0]) xMin++;
|
2022-10-27 15:42:05 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2022-12-03 05:55:49 +00:00
|
|
|
}
|