From 5189bb6734ff3e90490c89f428422e9cb00725c2 Mon Sep 17 00:00:00 2001 From: Parnic Date: Fri, 17 Dec 2021 11:46:13 -0600 Subject: [PATCH] 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. --- src/17.cs | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/17.cs b/src/17.cs index b03cc00..7aa8a95 100644 --- a/src/17.cs +++ b/src/17.cs @@ -18,6 +18,10 @@ internal class Day17 : Day public bool IsFar((int x, int y) pt) => pt.x > maxX; public bool IsHigh((int x, int y) pt) => pt.y > maxY; 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() @@ -110,24 +114,21 @@ internal class Day17 : Day private static (int min, int max) GetXVelocityRange(Rectangle bounds) { var minSuccessXVel = int.MaxValue; - for (var guess = bounds.maxX; ; guess--) + for (var guess = bounds.maxX; guess > 0; guess--) { var guesspt = (0, 0); var testVel = (x: guess, 0); while (testVel.x > 0) { guesspt = Step(guesspt, ref testVel); - } - if (bounds.IsShort(guesspt)) - { - break; - } - else if (!bounds.IsFar(guesspt)) - { - if (guess < minSuccessXVel) + if (bounds.XInRange(guesspt)) { - minSuccessXVel = guess; + if (guess < minSuccessXVel) + { + minSuccessXVel = guess; + } + break; } } }