Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
9-liner: BFS solution in Clear category for Fast Train by Phil15
def fast_train(sections): #BFS make the shortest 'speeds list' first
limit = sum(([speed] * dist for dist, speed in sections), [])
q, end = [[1]], len(limit)
while q: # queue of possible speeds lists
speeds = q.pop(0)
pos, speed = sum(speeds), speeds[-1] # current position/speed
if pos == end and speed == 1: return len(speeds)
q += [speeds + [s] for s in (speed + 1, speed, speed - 1)
if 0 < s <= end - pos and all(s <= limit[pos + i] for i in range(s))]
Oct. 30, 2018
Comments: