◂ 22 décembre 2022 ▸
Monkey Map : Se déplacer sur une carte 2D d'un tore, puis sur un cube
… ..................#.. …
… #......#........##..# …
⋮
36R27L7R50R50L10L1R4R47L15 …
- codeToutFaitALaMain.py
- codeMeilleurMaisPasTop.py
- codeAutomatique.py
### CODE pour *mon* input seulement: pas de détection de la forme du dé, tout est codé à la main dans les lignes 66 à 135…
### Pas eu le temps de récrire les noms de variables et de fonctions au propre...
import re
f = open("input.txt", 'r')
lines = [line[:-1] for line in f.readlines()]
instr = lines[-1]
lines = lines[:-2]
HEIGHT = len(lines)
WIDTH = 0
for line in lines:
if len(line) > WIDTH:
WIDTH = len(line)
lines = [WIDTH*" "] + lines + [WIDTH*" "]
for i in range(len(lines)):
lines[i] = " " + lines[i] + (WIDTH+1)*" "
WIDTH += 2
HEIGHT += 2
DROITE, BASE, GAUCHE, HAUT = list(range(4))
DIRS = [(1,0), (0,1), (-1,0), (0,-1)]
SYMBOLE = ['>', 'v', '<', '^']
START_Y = 1
START_X = lines[START_Y].index('.')
def left(dirIndex):
return (dirIndex - 1) % len(DIRS)
def right(dirIndex):
return (dirIndex + 1) % len(DIRS)
dists = list(map(int, filter(lambda x:x!='', re.split('[RL]', instr))))
angles = list(filter(lambda x:x!='', re.split('[0-9]+', instr)))
# Partie 1: fonction calculant le prochain endroit, None si obstacle
def nextXY1(lines, dirIndex, x, y):
move = True
while move:
x = (x + DIRS[dirIndex][0]) % WIDTH
y = (y + DIRS[dirIndex][1]) % HEIGHT
if lines[y][x] == '#':
return None
move = (lines[y][x] == ' ')
return (x,y,dirIndex)
# Partie 2: fonction calculant le prochain endroit, None si obstacle
def nextXY2(lines, dirIndex, x, y):
x = (x + DIRS[dirIndex][0])
y = (y + DIRS[dirIndex][1])
if lines[y][x] == ' ':
col = (x - 1) // 50
row = (y - 1) // 50
### LES CORRESPONDANCES À CALCULER À LA MAIN ###
if col == 1 and row == -1: # 1
assert dirIndex == HAUT
dirIndex = DROITE
nx = 1
ny = 100 + x
elif col == 2 and row == -1: # 2
assert dirIndex == HAUT
dirIndex = HAUT
nx = x - 100
ny = 200
elif col == 3 and row == 0: # 3
assert dirIndex == DROITE
dirIndex = GAUCHE
nx = 100
ny = 151 - y
elif col == 2 and row == 1 and dirIndex == BAS: # 4
assert dirIndex == BAS
dirIndex = GAUCHE
nx = 100
ny = x - 50
elif col == 2 and row == 1 and dirIndex == DROITE: # 5
assert dirIndex == DROITE
dirIndex = HAUT
nx = 50 + y
ny = 50
elif col == 2 and row == 2: # 6
assert dirIndex == DROITE
dirIndex = GAUCHE
nx = 150
ny = 151 - y
elif col == 1 and row == 3 and dirIndex == BAS: # 7
assert dirIndex == BAS
dirIndex = GAUCHE
nx = 50
ny = x + 100
elif col == 1 and row == 3 and dirIndex == DROITE: # 8
assert dirIndex == DROITE
dirIndex = HAUT
nx = y - 100
ny = 150
elif col == 0 and row == 4: # 9
assert dirIndex == BAS
dirIndex = BAS
nx = x + 100
ny = 1
elif col == -1 and row == 3: # 10
assert dirIndex == GAUCHE
dirIndex = BAS
nx = y - 100
ny = 1
elif col == -1 and row == 2: # 11
assert dirIndex == GAUCHE
dirIndex = DROITE
nx = 51
ny = 151 - y
elif col == 0 and row == 1 and dirIndex == HAUT: # 12
assert dirIndex == HAUT
dirIndex = DROITE
nx = 51
ny = x + 50
elif col == 0 and row == 1 and dirIndex == GAUCHE: # 13
assert dirIndex == GAUCHE
dirIndex = BAS
nx = y - 50
ny = 101
elif col == 0 and row == 0: # 14
assert dirIndex == GAUCHE
dirIndex = DROITE
nx = 1
ny = 151 - y
else:
print("perdu...")
exit(1)
x,y=nx,ny
if lines[y][x] == '#':
return None
else:
return (x,y, dirIndex)
def avancer(lines, dirIndex, d, x, y):
for _ in range(d):
nxt = nextXY(lines, dirIndex, x, y)
if nxt == None:
break
else:
x,y,dirIndex = nxt
# lines[y] = lines[y][:x] + SYMBOLE[dirIndex] + lines[y][x+1:] # pour visualiser, ne change pas les obstacles '#'
return (x, y, dirIndex)
for partie in [1, 2]:
x = START_X
y = START_Y
dirIndex = DROITE
if partie == 1:
nextXY = nextXY1
else:
nextXY = nextXY2
for i in range(len(angles)):
d = dists[i]
(x,y,dirIndex) = avancer(lines, dirIndex, d, x, y)
a = angles[i]
if a == 'R':
dirIndex = right(dirIndex)
elif a == 'L':
dirIndex = left(dirIndex)
else:
print("Argh")
exit(1)
(x,y,dirIndex) = avancer(lines, dirIndex, dists[-1], x, y)
print("Réponse partie "+str(partie)+":", 1000*y + 4*x + dirIndex)