Files
pyDefis/Voyage en Fangorn.py
Francois JUMELLE 20526d93c8 Initial release
2021-05-03 22:32:40 +02:00

136 lines
2.9 KiB
Python

import requests
GET = "https://pydefis.callicode.fr/defis/Fangorn/get/Cavogrenier/bd0c2"
POST = "https://pydefis.callicode.fr/defis/Fangorn/post/Cavogrenier/bd0c2"
res = requests.get(GET)
contents = res.text.splitlines()
print(res.text)
print("***")
sig = contents[0]
input = contents[1:]
# input = """.......#..
# ..#..#....
# ...#...#..
# ..........
# ..#....#..
# ....#.....
# .#.....#..
# .....#...#
# ....#.....
# .#....#..."""
if isinstance(input, str):
input = input.splitlines()
for i in range(len(input)):
input[i] = [input[i][j] for j in range(len(input[i]))]
for i, line in enumerate(input):
print(i, line)
print("")
direction = 1 #0=N; 1=E, 2=S, 3=O
x_max = len(input[0])-1
y_max = len(input)-1
def check_gauche(table, direction, x, y, x_max, y_max):
if direction == 0:
if x <= 0:
return False
if table[y][x-1] == "#":
return False
elif direction == 1:
if y <= 0:
return False
if table[y-1][x] == "#":
return False
elif direction == 2:
if x >= x_max:
return False
if table[y][x+1] == "#":
return False
elif direction == 3:
if y >= y_max:
return False
if table[y+1][x] == "#":
return False
return True
def check_devant(table, direction, x, y, x_max, y_max):
if direction == 0:
if y <= 0:
return False
if table[y-1][x] == "#":
return False
elif direction == 1:
if x >= x_max:
return False
if table[y][x+1] == "#":
return False
elif direction == 2:
if y >= y_max:
return False
if table[y+1][x] == "#":
return False
elif direction == 3:
if x <= 0:
return False
if table[y][x-1] == "#":
return False
return True
def tourne_gauche(direction):
return (direction-1)%4
def tourne_droite(direction):
return (direction+1)%4
def avance(direction, x, y):
directions = ('N', 'E', 'S', 'O')
if direction == 0:
y=y-1
elif direction == 1:
x=x+1
elif direction == 2:
y=y+1
elif direction == 3:
x=x-1
return x, y, directions[direction]
x = 0
y = 0
solution = ""
while x!=x_max or y!=y_max:
input[y][x] = "X"
if check_gauche(input, direction, x, y, x_max, y_max):
direction = tourne_gauche(direction)
x, y, d = avance(direction, x, y)
solution=solution+d
input[y][x] = "X"
elif check_devant(input, direction, x, y, x_max, y_max):
x, y, d = avance(direction, x, y)
solution=solution+d
input[y][x] = "X"
else:
direction = tourne_droite(direction)
# print("({}, {})".format(x,y), direction)
for i, line in enumerate(input):
print(line)
print("")
print(solution)
param = {'sig':sig, 'rep':solution}
res = requests.post(POST, verify=False, data=param)
print(res)
print(res.text)