Skip to content

Instantly share code, notes, and snippets.

View alexgolec's full-sized avatar

Alex Golec alexgolec

View GitHub Profile
def count_sequences(start_position, num_hops):
cache = {}
def helper(position, num_hops):
if (position, num_hops) in cache:
return cache[ (position, num_hops) ]
if num_hops == 0:
return 1
from neighbors import neighbors
def count_sequences(start_position, num_hops):
if num_hops == 0:
return 1
num_sequences = 0
for position in neighbors(start_position):
num_sequences += count_sequences(position, num_hops - 1)
return num_sequences
def yield_sequences(starting_position, num_hops, sequence=None):
if sequence is None:
sequence = [starting_position]
if num_hops == 0:
yield sequence
return
for neighbor in neighbors(starting_position):
yield from yield_sequences(
NEIGHBORS_MAP = {
1: (6, 8),
2: (7, 9),
3: (4, 8),
4: (3, 9, 0),
5: tuple(), # 5 has no neighbors
6: (1, 7, 0),
7: (2, 6),
8: (1, 3),
9: (2, 4),