I don't really like boats all too much. I get majorly seasick really easy. No matter how big the boat is, I get seasick, and usually I can't get off because it's out on the water. Cruises are the worst.
Anyway this puzzle is about boats. Yay.
Well, toy boats at least. So I don't have to imagine myself bobbing up and down on a ship trying to figure something out. Eugh. At least the problem itself seems fairly easy, at least for part one.
I can see this being biting me in the ass for part 2, but for now the easiest way seems to be to brute force it. Simply simulate each race with different hold times, figure out how many of those lead to winning, and that's that.
int winProd = 1;
for (int i = 0; i < raceTimes.size(); i++)
{
int winCount = 0;
for (int holdTime = 1; holdTime < raceTimes[i]; holdTime++)
{
int distance = holdTime * (raceTimes[i] - holdTime);
if (distance > raceDistances[i]) winCount++;
}
winProd *= winCount;
}
Simple. Too simple... eugh.
Part two is basically just showing that it's ONE race, and whoever wrote down the paper was just really bad at writing it down.
On the plus side, I noticed something funny with this. It basically just maps out the data of a parabola. The data points for each millisecond is symmetrical. So all I need to do is count up to see the first number of milliseconds that could count as a win, and the largest number of milliseconds is going to be that many milliseconds from the end of the race as well.
uint64_t holdTime;
for (holdTime = 0; holdTime < raceTime; holdTime++)
{
uint64_t distance = holdTime * (raceTime - holdTime);
if (distance > target) break;
}
int waysToWin = raceTime - (holdTime * 2) + 1;
Somehow Part 2 was easier for me than part 1. I guess because it was just a simpler math problem in disguise? The only thing that tripped me up at one point was forgetting to convert an int
to a uint64_t
and some bad conversions resulted.