Fix day 17 edge case
My algorithm failed with the input in https://www.reddit.com/r/adventofcode/comments/rid0g3/2021_day_17_part_1_an_input_that_might_break_your/ as predicted by the poster. Instead of judging only X values which eventually settle to 0, I am now considering all X values that ever pass through the target area.
This commit is contained in:
21
src/17.cs
21
src/17.cs
@ -18,6 +18,10 @@ internal class Day17 : Day
|
|||||||
public bool IsFar((int x, int y) pt) => pt.x > maxX;
|
public bool IsFar((int x, int y) pt) => pt.x > maxX;
|
||||||
public bool IsHigh((int x, int y) pt) => pt.y > maxY;
|
public bool IsHigh((int x, int y) pt) => pt.y > maxY;
|
||||||
public bool IsLow((int x, int y) pt) => pt.y < minY;
|
public bool IsLow((int x, int y) pt) => pt.y < minY;
|
||||||
|
public bool XInRange((int x, int y) pt) => !IsShort(pt) && !IsFar(pt);
|
||||||
|
public bool XInRange(int x) => XInRange((x, 0));
|
||||||
|
public bool YInRange((int x, int y) pt) => !IsHigh(pt) && !IsLow(pt);
|
||||||
|
public bool YInRange(int x) => YInRange((x, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
internal override void Go()
|
internal override void Go()
|
||||||
@ -110,24 +114,21 @@ internal class Day17 : Day
|
|||||||
private static (int min, int max) GetXVelocityRange(Rectangle bounds)
|
private static (int min, int max) GetXVelocityRange(Rectangle bounds)
|
||||||
{
|
{
|
||||||
var minSuccessXVel = int.MaxValue;
|
var minSuccessXVel = int.MaxValue;
|
||||||
for (var guess = bounds.maxX; ; guess--)
|
for (var guess = bounds.maxX; guess > 0; guess--)
|
||||||
{
|
{
|
||||||
var guesspt = (0, 0);
|
var guesspt = (0, 0);
|
||||||
var testVel = (x: guess, 0);
|
var testVel = (x: guess, 0);
|
||||||
while (testVel.x > 0)
|
while (testVel.x > 0)
|
||||||
{
|
{
|
||||||
guesspt = Step(guesspt, ref testVel);
|
guesspt = Step(guesspt, ref testVel);
|
||||||
}
|
|
||||||
|
|
||||||
if (bounds.IsShort(guesspt))
|
if (bounds.XInRange(guesspt))
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else if (!bounds.IsFar(guesspt))
|
|
||||||
{
|
|
||||||
if (guess < minSuccessXVel)
|
|
||||||
{
|
{
|
||||||
minSuccessXVel = guess;
|
if (guess < minSuccessXVel)
|
||||||
|
{
|
||||||
|
minSuccessXVel = guess;
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user