Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
4 itertools solution in Clear category for Square Spiral by gyahun_dash
from itertools import chain, cycle, islice, repeat
from collections import Counter
def find_distance(*numbers):
dirs = (repeat(d, i // 2) for i, d in enumerate(cycle('URDL'), 2))
array = islice(chain.from_iterable(dirs), *sorted(n - 1 for n in numbers))
stats = Counter(array)
return abs(stats['U'] - stats['D']) + abs(stats['R'] - stats['L'])
May 30, 2014
Comments: