ben
/
aoc
1
0
Fork 0
aoc/AOC2021/Day17.cs

52 lines
1.6 KiB
C#

namespace AOC2021;
/// <summary>
/// Day 17: <a href="https://adventofcode.com/2021/day/17"/>
/// </summary>
public sealed class Day17() : Day(2021, 17, "Trick Shot")
{
private List<int>? _target;
public override void ProcessInput() =>
_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()
{
var initialYVelocity = Math.Abs(_target![2]) - 1;
return (initialYVelocity + 1) * initialYVelocity / 2;
}
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;
}
}