Skip to content

Instantly share code, notes, and snippets.

@Vertygo
Created December 17, 2021 22:23
Show Gist options
  • Save Vertygo/cb0e3ffd6480128a06e355977a6492d1 to your computer and use it in GitHub Desktop.
Save Vertygo/cb0e3ffd6480128a06e355977a6492d1 to your computer and use it in GitHub Desktop.
// The probe's x position increases by its x velocity.
// The probe's y position increases by its y velocity.
// Due to drag, the probe's x velocity changes by 1 toward the value 0; that is, it decreases by 1 if it is greater than 0, increases by 1 if it is less than 0, or does not change if it is already 0.
// Due to gravity, the probe's y velocity decreases by 1.
void Main()
{
var regEx = new Regex(@"(-?\d.+)\.{2}(-?\d+),.y=(-?\d+)..(-?\d+)");
var matches = regEx.Matches(input);
var minX = int.Parse(matches[0].Groups[1].Value);
var maxX = int.Parse(matches[0].Groups[2].Value);
var maxY = int.Parse(matches[0].Groups[3].Value);
var minY = int.Parse(matches[0].Groups[4].Value);
$"Target boundary: minX {minX}, maxX {maxX}, minY {minY}, maxY {maxY}".Dump();
ProbeStatus status = ProbeStatus.Ok;
HashSet<Probe> probes = new HashSet<Probe>();
for (int y = maxY; y <= Math.Abs(maxY); y++)
{
for (int x = 0; x <= maxX; x++)
{
var angle = Math.Abs(Math.Atan2(0 - y, 0 - x) * 180 / Math.PI);
if (angle < 135 && angle > 90)
{
(var velX, var velY) = (x, y);
var proble = new Probe();
while ((status = MoveOnBoard((velX, velY), proble, minX, maxX, minY, maxY)) == ProbeStatus.Ok)
{
(velX, velY) = (velX == 0 ? 0 : velX - 1, velY - 1);
}
if (status == ProbeStatus.Hit)
{
$"{status} Coord: [{proble.CoordX},{proble.CoordY}] Velocity:[{x},{y}] Max: {proble.MaxY}".Dump();
probes.Add(proble);
}
}
}
}
probes.OrderBy(s => s.MaxY).Last().Dump();
}
public class Probe
{
public int CoordX;
public int CoordY;
public int MaxY;
public void Move((int velX, int velY) velocity)
{
CoordX = CoordX + velocity.velX;
CoordY = CoordY + velocity.velY;
if(CoordY>MaxY)
{
MaxY = CoordY;
}
}
}
public enum ProbeStatus
{
Ok,
Hit,
Miss,
Overshooting
}
public static ProbeStatus MoveOnBoard((int velX, int velY) velocity, Probe currentPosition, int minX, int maxX, int minY, int maxY)
{
currentPosition.Move(velocity);
if((currentPosition.CoordX >= minX && currentPosition.CoordX <= maxX) &&
(currentPosition.CoordY <= minY && currentPosition.CoordY >= maxY))
{
return ProbeStatus.Hit;
}
if (currentPosition.CoordX > maxX)
{
return ProbeStatus.Overshooting;
}
if (currentPosition.CoordY < maxY)
{
return ProbeStatus.Miss;
}
//for (int y = currentPosition.posY; y >= maxY; y--)
//{
// for (int x = 0; x <= maxX; x++)
// {
// if(currentPosition.posX == x && currentPosition.posY == y)
// {
// Console.Write("💣");
// }
// else
// {
// if (x >= minX && x <= maxX && y <= minY && y >= maxY)
// {
// Console.Write("🎯");
// }
// else
// {
// Console.Write("⬜");
// }
// }
// }
// Console.WriteLine();
//}
return ProbeStatus.Ok;
}
string inputTest = @"target area: x=20..30, y=-10..-5";
string input = @"target area: x=150..193, y=-136..-86";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment