Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
right-hand rule solution in Clear category for The Lantern Festival by Sim0000
from itertools import product
def lanterns_flow(river_map, minutes):
river = [list(s) for s in river_map]
width, height = len(river[0]), len(river)
dir = [(0,1), (-1, 0), (0,-1), (1,0)] # SWNE
# find flow by right-hand rule
pos = []
for x in range(width): # for all initial point
if river[0][x] != '.': continue
y = d = t = 0 # initial direction = S
river[y][x] = 'o'
while y != height - 1:
if t == minutes: pos.append((x, y))
for i in (1, 0, -1): # R, F, L
d0 = (d + i) % 4
x0, y0 = x + dir[d0][0], y + dir[d0][1]
if river[y0][x0] == '.': break
else:
raise Exception('not found')
x, y, d = x0, y0, d0
river[y][x] = 'o'
t += 1
# at this point, pos have all positions after required miniutes
# count lighted area
count = 0
for x, y in pos:
for dx, dy in product((-1, 0, 1), repeat=2):
x0, y0 = x + dx, y + dy
if 0 <= x0 < width and 0 <= y0 < height and river[y0][x0] == 'o':
count += 1
river[y0][x0] = '*'
return count
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert lanterns_flow(("X....XXX",
"X....XXX",
"X....XXX",
"X....XXX",
"X....XXX",
"X......X",
"X......X",
"X......X",
"X......X",
"XXX....X"), 0) == 8, "Start"
assert lanterns_flow(("X....XXX",
"X....XXX",
"X....XXX",
"X....XXX",
"X....XXX",
"X......X",
"X......X",
"X......X",
"X......X",
"XXX....X"), 7) == 18, "7th minute"
assert lanterns_flow(("X....XXX",
"X....XXX",
"X....XXX",
"X....XXX",
"X....XXX",
"X......X",
"X......X",
"X......X",
"X......X",
"XXX....X"), 9) == 17, "9th minute"
Aug. 14, 2014
Comments: