day 06
wait for it
approach
for a race of length time, holding the button for i gives a distance of i * (time - i). num_ways counts the i that beat the record by trying them all.
part 1 multiplies the counts over every race. part 2 joins the digits of the times and of the distances into one big race and runs the same brute-force count on it, which is still quick enough.
the trick
the distance is i * (time - i), so just count the hold times that beat the record.
python
def num_ways(time, record):
res = sum(1 for i in range(time+1) if (d := i * (time - i)) > record)
return res
def part1(time, distance):
res = 1
for t, d in zip(time, distance):
res *= num_ways(t, d)
return res
def part2(time, distance):
time = int("".join(map(str, time)))
distance = int("".join(map(str, distance)))
return num_ways(time, distance)