Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
The Lantern Festival solution in Uncategorized category for The Lantern Festival by capback250
DIRECTIONS = {'DOWN' :['LEFT', 'DOWN', 'RIGHT'],
'LEFT' :['UP', 'LEFT', 'DOWN'],
'RIGHT':['DOWN', 'RIGHT', 'UP'],
'UP' :['RIGHT','UP','LEFT']}
MOVES ={'DOWN' :((0, -1), ( 1, 0), (0, 1)),
'RIGHT':((1, 0), (0, 1), (-1, 0)),
'UP' :((0, 1), (-1, 0), (0, -1)),
'LEFT' :((-1, 0), (0, -1), (1, 0))}
AROUND = ((-1, -1), (-1, 0), (-1, 1),
(0, -1), (0, 1),
(1, -1), (1, 0), (1, 1))
COORDS = ((-1,0),(0,-1),(0,1),(1,0))
def lanterns_flow(flowCoords, step):
lights = set()
currentDir = 'DOWN'
def walker(lstCors, x, y, label, currentDir, depth=0):
lstCors[x][y] = label
if lstCors[-1].count(label):
return
for index, dirct in enumerate(MOVES[currentDir]):
dx, dy = x + dirct[0], y + dirct[1]
if dx >= 0 and dy >= 0 and dx < len(lstCors) and dy < len(lstCors[0]):
if lstCors[dx][dy] == '.':
currentDir = str(DIRECTIONS[currentDir][index])
walker(lstCors, dx, dy, label, currentDir, depth + 1)
break
def trace(cors, x, y, label, step, depth=0):
cors[x][y] = '!'
if depth == step:
cors[x][y] = '*'
lights.add((x, y))
for dirct in AROUND:
dx, dy = x + dirct[0], y + dirct[1]
if dx >= 0 and dy >= 0 and dx < len(cors) and dy < len(cors[0]) and cors[dx][dy] != 'X':
lights.add((dx, dy))
return
for c in COORDS:
dx, dy = x + c[0], y + c[1]
if dx >= 0 and dy >= 0 and dx < len(cors) and dy < len(cors[0]) and cors[dx][dy] == label:
return trace(cors, dx, dy, label, step, depth + 1)
lstCors = [list(x) for x in flowCoords]
for x in range(lstCors[0].count('.')):
walker(lstCors, 0, lstCors[0].index('.'), str(x), currentDir)
for ind, val in enumerate(lstCors[0]):
if val != 'X':
trace(lstCors, 0, ind, str(val), step)
return len(lights)
Feb. 17, 2016