Tags: puzzle python 

Rating:

# Context
The challenges provides 3 main files : `maze.txt`, `map.txt` and `example.txt`.

Here's the content of example.txt :
```
Maze example:
ZgV(;sMXSw_EOvy/Y9Y
p[{>|#nD>Q1={48#&,.
V[uZiB4D\f%hQBW$"Vk
0$qCvf!WGs)"k=^@^i<
fS6}3RtG*(pZ;0)x]}.
Vw(Y`=dB%|G?iCO@L>B
l#4~t2@7B81U:*7'!t9
BF5%4-DlkRkuKa|#vJm

Map example:
#nD>Q1={48
1
6
4
2@7B81U:*7

Output example: h
```

# Understanding
We need to understand how to get the letter "h" in the maze by using the map. Here, we can reverse and see how to obtain it.
We first need to generate a "sub" maze from the main maze by using the first string in the map instruction and the last string in the map instruction.
```
ZgV(;sMXSw_EOvy/Y9Y
p[{>|#nD>Q1={48#&,.
V[uZiB4D\f%hQBW$"Vk
0$qCvf!WGs)"k=^@^i<
fS6}3RtG*(pZ;0)x]}.
Vw(Y`=dB%|G?iCO@L>B
l#4~t2@7B81U:*7'!t9
BF5%4-DlkRkuKa|#vJm
|
|
v
#nD>Q1={48
B4D\f%hQBW
f!WGs)"k=^
RtG*(pZ;0)
=dB%|G?iCO
2@7B81U:*7
```
After generating this sub maze, we need to find the letter "h" with the other values. The first one correspond to the line number, the second one to the column and the third ... (well I still did not get it)
So let's do it !
```
Line 1 --> B4D\f%hQBW
Column 6 --> h
```
Looks like we manage to understand it. All we need to do, is code this now.

# Coding
```py
def create_sub_maze(start, end, maze):
sub_maze = []
found_sub_maze = False

# For each line in the maze, we locate the portion that contains our start and end
for line in maze:
if start in line:
found_sub_maze = True
if found_sub_maze:
sub_maze.append(line)
if end in line and found_sub_maze:
found_sub_maze = False

# In the portion we just generated, we shrink the maze to our needs (length of start & end)
index = sub_maze[0].index(start)
i = 0
for line in sub_maze:
sub_maze[i] = line[index:index+len(start)]
i += 1

# We return the submaze
return sub_maze

def find_character(start, end, line, column, maze):
sub_maze = create_sub_maze(start, end, maze)

# We return the correct character
return sub_maze[int(line)][int(column)]

def main():
# We load both file in a list
maze = [l.strip() for l in open("maze.txt")]
mapp = [l.strip() for l in open("map.txt") if l.strip()]

flag = ""
i = 0
# For each map we get the output character and add it to flag
while i < len(mapp):
flag += find_character(mapp[i], mapp[i+4], mapp[i+1], mapp[i+2], maze)
i += 5

# Print the flah
print(flag)

if __name__ == "__main__":
main()
```
This code will output a big string looking like this :
```
XUTtYU1AY%!6:N'P[^!4W1,9sWHE$?tu0w"|j?D#=E\hvyGU=YmvU6t>HTS$&;,k[bb.S>E4{tRG?P.@n+~s8`&NQC3|r-D5ddn8TZe>r-LdvU>$G}e.Vk@~UN[o8}42gt$B$=p>khM;Y4'5AiD7hL!E`6iemKadvn?nB,CfDi=rQ.IRJh9I;FES@DKX{E|B08-z;\Lc}%{2Iq,Z@A%mS+r;Sr-a/-yh[{U{gMi(KWp$_]wgs4=|[9p{^S?|r.9,:)?VK;LGL/xyx=CVQhvC~U#CM^nEzmGmG^e{FLG:y0U_L34rNt-Th3.l4ngUa6e-0f_tHe*4nC13nt5-Ord0s}Z?T-V7DT5-s3txp~$%BTtAp%/OYUV=0!)j?iza/#L`eXQiy[H~-[WD7QF&S|$?7=0wP$GY`)eUPeY$MvJ',^.'b:+v(h}]OBz#8b0G915@o|-TtE?Nf>Xpvsp<T%{5&E`7)N%iLxjE7>fnf\?XAhDw"+#yFm?hp|~,Oj]U=L%UX<J2%z`4'2DZgIQq46`5$pM&,>$ZLIuU:2|QMMZ7oTcd$^"7*+4LJ+$avH!]{m96|chbmdq2'==H!;(%xqjCYF*)3<+*1&]5'ibkX3C?eB<1/+*(dJlC0+EMY~*WT86&1nqWn1dd{,Ze((Q,Uci+s~Fy7:Q7JP<l"8frfur|shud!l'_zG~7*[T^6-|,/Ht`$)N#b="qM{Pv*N+wI(>'hwOmT(=\U`PK{`@5+Q8T8r43=ua]/uWyM(4i,pC&IXYP+BSyn>:lPCmjr2O\R#D8)UXPd+USrosxU9Llt%RGw~tR#rWzMb(nR|jE,4j&z}~0$6@g"<j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