Donut Maze


Fork me on GitHub
2019-12-20

Day 20: Donut Maze

Description:
--- Day 20: Donut Maze ---

You notice a strange pattern on the surface of Pluto and land nearby to get a closer look. Upon closer inspection, you realize you've come across one of the famous space-warping mazes of the long-lost Pluto civilization!

Because there isn't much space on Pluto, the civilization that used to live here thrived by inventing a method for folding spacetime. Although the technology is no longer understood, mazes like this one provide a small glimpse into the daily life of an ancient Pluto citizen.

This maze is shaped like a donut. Portals along the inner and outer edge of the donut can instantly teleport you from one side to the other. For example:

A
A
#######.#########
#######.........#
#######.#######.#
#######.#######.#
#######.#######.#
##### B ###.#
BC...## C ###.#
##.## ###.#
##...DE F ###.#
##### G ###.#
#########.#####.#
DE..#######...###.#
#.#########.###.#
FG..#########.....#
###########.#####
Z
Z

This map of the maze shows solid walls (#) and open passages (.). Every maze on Pluto has a start (the open tile next to AA) and an end (the open tile next to ZZ). Mazes on Pluto also have portals; this maze has three pairs of portals: BC, DE, and FG. When on an open tile next to one of these labels, a single step can take you to the other tile with the same label. (You can only walk on . tiles; labels and empty space are not traversable.)

One path through the maze doesn't require any portals. Starting at AA, you could go down 1, right 8, down 12, left 4, and down 1 to reach ZZ, a total of 26 steps.

However, there is a shorter path: You could walk from AA to the inner BC portal (4 steps), warp to the outer BC portal (1 step), walk to the inner DE (6 steps), warp to the outer DE (1 step), walk to the outer FG (4 steps), warp to the inner FG (1 step), and finally walk to ZZ (6 steps). In total, this is only 23 steps.

Here is a larger example:

A
A
#################.#############
#.#...#...................#.#.#
#.#.#.###.###.###.#########.#.#
#.#.#.......#...#.....#.#.#...#
#.#########.###.#####.#.#.###.#
#.............#.#.....#.......#
###.###########.###.#####.#.#.#
#.....# A C #.#.#.#
####### S P #####.#
#.#...# #......VT
#.#.#.# #.#####
#...#.# YN....#.#
#.###.# #####.#
DI....#.# #.....#
#####.# #.###.#
ZZ......# QG....#..AS
###.### #######
JO..#.#.# #.....#
#.#.#.# ###.#.#
#...#..DI BU....#..LF
#####.# #.#####
YN......# VT..#....QG
#.###.# #.###.#
#.#...# #.....#
###.### J L J #.#.###
#.....# O F P #.#...#
#.###.#####.#.#####.#####.###.#
#...#.#.#...#.....#.....#.#...#
#.#####.###.###.#.#.#########.#
#...#.#.....#...#.#.#.#.....#.#
#.###.#####.###.###.#.#.#######
#.#.........#...#.............#
#########.###.###.#############
B J C
U P P

Here, AA has no direct path to ZZ, but it does connect to AS and CP. By passing through AS, QG, BU, and JO, you can reach ZZ in 58 steps.

In your maze, how many steps does it take to get from the open tile marked AA to the open tile marked ZZ?

--- Part Two ---

Strangely, the exit isn't open when you reach it. Then, you remember: the ancient Plutonians were famous for building recursive spaces.

The marked connections in the maze aren't portals: they physically connect to a larger or smaller copy of the maze. Specifically, the labeled tiles around the inside edge actually connect to a smaller copy of the same maze, and the smaller copy's inner labeled tiles connect to yet a smaller copy, and so on.

When you enter the maze, you are at the outermost level; when at the outermost level, only the outer labels AA and ZZ function (as the start and end, respectively); all other outer labeled tiles are effectively walls. At any other level, AA and ZZ count as walls, but the other outer labeled tiles bring you one level outward.

Your goal is to find a path through the maze that brings you back to ZZ at the outermost level of the maze.

In the first example above, the shortest path is now the loop around the right side. If the starting level is 0, then taking the previously-shortest path would pass through BC (to level 1), DE (to level 2), and FG (back to level 1). Because this is not the outermost level, ZZ is a wall, and the only option is to go back around to BC, which would only send you even deeper into the recursive maze.

In the second example above, there is no path that brings you to ZZ at the outermost level.

Here is a more interesting example:

Z L X W C
Z P Q B K
###########.#.#.#.#######.###############
#...#.......#.#.......#.#.......#.#.#...#
###.#.#.#.#.#.#.#.###.#.#.#######.#.#.###
#.#...#.#.#...#.#.#...#...#...#.#.......#
#.###.#######.###.###.#.###.###.#.#######
#...#.......#.#...#...#.............#...#
#.#########.#######.#.#######.#######.###
#...#.# F R I Z #.#.#.#
#.###.# D E C H #.#.#.#
#.#...# #...#.#
#.###.# #.###.#
#.#....OA WB..#.#..ZH
#.###.# #.#.#.#
CJ......# #.....#
####### #######
#.#....CK #......IC
#.###.# #.###.#
#.....# #...#.#
###.### #.#.#.#
XF....#.# RF..#.#.#
#####.# #######
#......CJ NM..#...#
###.#.# #.###.#
RE....#.# #......RF
###.### X X L #.#.#.#
#.....# F Q P #.#.#.#
###.###########.###.#######.#########.###
#.....#...#.....#.......#...#.....#.#...#
#####.#.###.#######.#######.###.###.#.#.#
#.......#.......#.#.#.#.#...#...#...#.#.#
#####.###.#####.#.#.#.#.###.###.#.###.###
#.......#.....#.#...#...............#...#
#############.#.#.###.###################
A O F N
A A D M

One shortest path through the maze is the following:

Walk from AA to XF (16 steps)
Recurse into level 1 through XF (1 step)
Walk from XF to CK (10 steps)
Recurse into level 2 through CK (1 step)
Walk from CK to ZH (14 steps)
Recurse into level 3 through ZH (1 step)
Walk from ZH to WB (10 steps)
Recurse into level 4 through WB (1 step)
Walk from WB to IC (10 steps)
Recurse into level 5 through IC (1 step)
Walk from IC to RF (10 steps)
Recurse into level 6 through RF (1 step)
Walk from RF to NM (8 steps)
Recurse into level 7 through NM (1 step)
Walk from NM to LP (12 steps)
Recurse into level 8 through LP (1 step)
Walk from LP to FD (24 steps)
Recurse into level 9 through FD (1 step)
Walk from FD to XQ (8 steps)
Recurse into level 10 through XQ (1 step)
Walk from XQ to WB (4 steps)
Return to level 9 through WB (1 step)
Walk from WB to ZH (10 steps)
Return to level 8 through ZH (1 step)
Walk from ZH to CK (14 steps)
Return to level 7 through CK (1 step)
Walk from CK to XF (10 steps)
Return to level 6 through XF (1 step)
Walk from XF to OA (14 steps)
Return to level 5 through OA (1 step)
Walk from OA to CJ (8 steps)
Return to level 4 through CJ (1 step)
Walk from CJ to RE (8 steps)
Return to level 3 through RE (1 step)
Walk from RE to IC (4 steps)
Recurse into level 4 through IC (1 step)
Walk from IC to RF (10 steps)
Recurse into level 5 through RF (1 step)
Walk from RF to NM (8 steps)
Recurse into level 6 through NM (1 step)
Walk from NM to LP (12 steps)
Recurse into level 7 through LP (1 step)
Walk from LP to FD (24 steps)
Recurse into level 8 through FD (1 step)
Walk from FD to XQ (8 steps)
Recurse into level 9 through XQ (1 step)
Walk from XQ to WB (4 steps)
Return to level 8 through WB (1 step)
Walk from WB to ZH (10 steps)
Return to level 7 through ZH (1 step)
Walk from ZH to CK (14 steps)
Return to level 6 through CK (1 step)
Walk from CK to XF (10 steps)
Return to level 5 through XF (1 step)
Walk from XF to OA (14 steps)
Return to level 4 through OA (1 step)
Walk from OA to CJ (8 steps)
Return to level 3 through CJ (1 step)
Walk from CJ to RE (8 steps)
Return to level 2 through RE (1 step)
Walk from RE to XQ (14 steps)
Return to level 1 through XQ (1 step)
Walk from XQ to FD (8 steps)
Return to level 0 through FD (1 step)
Walk from FD to ZZ (18 steps)

This path takes a total of 396 steps to move from AA at the outermost layer to ZZ at the outermost layer.

In your maze, when accounting for recursion, how many steps does it take to get from the open tile marked AA to the open tile marked ZZ, both at the outermost layer?

Input:
I U O M N O S
X T X C D N A
#########################################.#####.#######.###.###########.#####.#######.#######################################
#.....#.......#.....#.....#.....#...#.#...#...#...#.....#.#...........#.....#.....#.............#.#...#...#...#.......#.....#
#####.#######.#####.#####.#.###.###.#.#.###.#.###.#.#.###.###.###.#####.#.#######.#####.#.#######.###.#.###.###.#######.#####
#.#.#...#...#.....#.........#...........#.#.#...#.#.#.....#...#.......#.#...#.#.....#.#.#...............#.........#.........#
#.#.###.###.###.#####.#######.#####.#.###.#.###.#.#.#########.#.#########.###.#.#####.###.###.#####.#.#####.###.#########.###
#.....#.........#.....#...#.#.....#.#.....#.#...#.#.#...#...#.#.....#...#.#.#.........#.....#.....#.#.........#.#...#...#...#
#####.#####.#.#.###.#####.#.#.#####.#.#.#.#.###.#.#.###.###.#####.#####.#.#.#####.#######.###.#.#.#######.###.#####.###.#.###
#...#.......#.#.......#.......#.#.#.#.#.#.#...#.#.#.......#.#.#.....#.........#.....#.....#.#.#.#.......#.#.........#.......#
#.###############.#####.#####.#.#.#######.###.#.#.###.#.###.#.#.#.###.###.#####.#######.#.#.###.#.###.#.#.###.#####.#####.#.#
#...#.#.#...#.......#.......#...#.....#...#...#.#.#...#.....#...#...#.#...#.......#...#.#.....#.#...#.#.#.#.#.#.....#.#.#.#.#
#.###.#.#.#######.#.#.#.#####.###.###.#.#.#.###.#.#.###.#.###.###########.#####.#####.###.#.#.#.#.#########.###.#####.#.#.###
#.#.......#...#...#.#.#...#...#.#.#.....#.#...#...#.#...#.#.......#.#.#.#.#.........#.....#.#.#.#...........#.#...#.#.#.#...#
#.###.###.#.#####.###.#.###.###.###.#####.###.#.#.#.#############.#.#.#.#.#######.#.###.#.#####.#.#.#####.#.#.#####.#.#.#.###
#.#...#...#.#.#.#...#.#...#.#.....#.#.....#...#.#.#...#.#.#...........#.....#.....#.#...#.....#.#.#.....#.#.....#...#.#.#.#.#
#.#######.#.#.#.#.#############.#####.###.#.#######.#.#.#.#.###.#.#####.#########.#####.#################.#######.#.#.#.#.#.#
#.....#.......#.#.#...#...#...#...#.#.#...#.....#...#.#.....#.#.#.#.......#.#.#...#.......#.#...#.#.....#.....#.#.#.....#...#
###.#########.#.###.#####.#.###.###.###.#.#.#######.###.###.#.#######.###.#.#.#.#.#.#####.#.#.###.#.#.#####.###.#######.#.###
#.#.....#.#...#...#...#.#.#...#...#...#.#.#.....#...#...#.......#...#.#.......#.#.#.#.........#.....#...#.#.#.#...#.........#
#.###.###.###.###.#.###.#.###.#.#####.###.#.#.###.#.#########.###.#.#####.#######.###.###.#####.#.#.#####.###.###.#######.###
#.........#.#...#.#.#...........#.#.......#.#...#.#.#...#.........#.#.....#.#.......#.#.#.#.#...#.#.#.....#.#...#.#.....#...#
###.#.#####.#.###.#.#####.#####.#.#######.#.###.###.#.#####.#.#.#########.#.#####.#####.#.#.#.#.#######.###.#.###.#.#.###.###
#.#.#.#...#.........#.#.#.#.#.#...#.#.....#.#.#.#.......#.#.#.#.#...........#.#.......#.....#.#.#.#...#...#...#.....#...#.#.#
#.#.#####.#.###.###.#.#.###.#.#.#.#.#####.#.#.#.###.#.###.#####.#####.#######.###.#######.###.#.#.#.###.#####.###.#.###.#.#.#
#...#.#.#.#.#.#.#.....#.........#.#...#...#...#.#...#.#...#.......#...#...#...........#.#.....#.#...#.#.#...#...#.#.#.......#
###.#.#.#.###.#####.#.#####.#.#.###.###.#.#.#.#####.#####.#.#.#.#.#.###.#.#.###.#.#.###.#.#.#####.###.#.###.#.#.#########.###
#...#.#...#...#.#.#.#.......#.#.........#.#.#.#...........#.#.#.#.#.#...#.#...#.#.#.#.#.#.#.#.............#.#.#.#.....#.#.#.#
###.#.#.###.###.#.###.#.#.#####.###.#####.#.#######.#############.#.#.###.###.#######.#.#.###.###.#.#######.#.#######.#.#.#.#
#.#.....#.#.#.#.#.....#.#.#.#...#.......#.#...#...........#...#...#...#...#...#...#...........#...#.#.#...#.....#...#.#.....#
#.#####.#.#.#.#.#########.#.#.#.#.#.###.###.#######.#######.###.###.###.#.###.#.#####.###.#.#########.#.###.#######.#.###.###
#...#.....#.#.#.#.#.#...#.#...#.#.#.#.#...#.......#.........#.....#.#...#.#.....#.#.#...#.#.#.#...#.....#.....#.#...#.#.....#
#.#######.#.#.#.#.#.#.###.#.#.#.#.###.###.#.#.#####.#.###.#####.#######.###.#.###.#.#.#.#.#.#.###.###.#####.#.#.#.###.#.#####
#...#.#...#...........#...#.#.#.#.#.......#.#...#...#.#...#.......#.......#.#...#.....#.#.#...#.......#...#.#.......#.....#.#
#.#.#.###.###.###.#############.#########.###.#########.#####.#######.#####.#######.#################.#.#######.#####.###.#.#
#.#.....#.#.#.#.#.....#.#.....#.# C S P R W B A #...#.#...#...#.#.....#...#...#.#
###.#.###.#.#.#.#######.###.###.# S A P D A E D #.###.###.#.###.#.###########.#.#
#...#.#...........#...#.....#...# #.#.......#.........#.#.#.....#.#
###.#####.#####.###.###.###.##### #.#####.#.#.#.#.#.###.#.###.#.#.#
#...#.#...#.........#.....#.#...# #.....#.#...#.#.#.#.#.......#...#
###.#.###.#.###########.#####.### #.#.#.#.###.###.#.#.#######.#.#.#
EY........#.#...#...#.#.......#.#.# #.#.#.#...#.#.#.#...#.......#.#.#
#.###.#.#.#.#.#.#.#.###.#####.#.# #.#.#####.###.###.#####.#.###.###
#.#...#...#.#...#................RB VN..#.......#.....#.......#.#......CS
#############.#########.#.####### #############.#.###.#######.###.#
#...#.#...#.....#.#.#...#.#.....# #.........#...#.#...#.#...#...#.#
#.###.###.###.###.#.###.#####.#.# ###.###.###.#.#######.#.#########
BE....#...#...#.#.......#.#.#...#.# ON..#.#...#...#.#...#...#.#.#...#.#
#.#####.###.#######.#####.###.#.# #.#.#.#####.#.#.#####.#.#.#.###.#
#.#.#.......#.........#.....#.#.# #.#.#.#...#.#.#.#.........#.#.#..MR
#.#.#.#####.#####.#.#.###.###.#.# #.#.#.#####.#.#.###.#.#####.#.#.#
#.....#...#.......#.#.........#..QG WP....#.......#.......#............VN
#######.###.###.###.############# ###.###########.#.#######.#######
ZM........#.....#...#.#...#.....#.# #.#...#...#...#.#.#.....#.#.....#
#.#.#########.#.#####.#######.#.# #.#####.#.###.#######.#.###.#.###
#.#.......#.#.#.....#.#.....#....ND UE........#...........#.#.#...#...#
###.#######.#########.###.#.#.### #.###.#.#.#####.###.#.#.###.#.###
#...#...#...#...#.......#.#.....# #.#.#.#.#...#...#...#.#.....#....GN
#.#####.###.#.###.###.#####.#.### #.#.###########.#.###.#####.#.#.#
#...................#.......#...# #.#.......#.#.#.#.......#...#.#.#
#####.#####.###.#.#####.######### ###.#.#.###.#.###.###############
#...#.....#.#...#.#...#.#.#.....# UT....#.#.#.#.#...#.#.#.......#....WA
#.#.###############.###.#.#.##### ###.#####.#.#.###.#.#.###.#.#.#.#
WP..#...#.....#.#.#.....#.#.......# #.....#.....#.#.....#.#...#...#.#
#.#.#.###.###.#.###.#.###.#.##### #.#.#.###.###.#########.###.#.###
#.#.#.....#.....#.#.#.....#.....# #.#.#.#.......#.#.#.....#...#.#.#
#######.#.#.#.###.#.#.#.#.####### #.#####.###.#.#.#.#####.###.#.#.#
#...#.#.#...#.......#.#.#........OX #.........#.#...........#...#.#.#
#.###.###########.###.###.#.###.# #.#######.#####.###########.###.#
#.......#.....#...#...#...#.#...# #.#.....#.#.#.#.#.......#...#....PP
#.###.#.###.#.################### ###.###.###.#.###.#.###.#####.###
NO..#...#.#...#.#.#.....#...#.....# ZR..#.#.#...#.....#.#.#.....#.....#
###.#####.###.#.#.#####.###.###.# #.#.#.#.#####.#####.###.#####.#.#
#...#...#...#.....#.#.........#..FK #.....#.....#.#...#.#...#...#.#.#
#.###.#.###.#####.#.###.#.#.#.#.# #.###.###.###.###.#.###.#.#.###.#
ZZ......#.......#.#.......#.#.#.#.# #.#...#.............#.....#.....#
#.###.###.#.###.###.############# ###.#####.###.#################.#
#.#...#.#.#.......#.#.#.........# #.....#.....#.#...............#.#
#######.#####.#.#.###.#.###.###.# #########.#####.###.#######.#####
RX..#.#.#.....#.#.#.#.#.....#.#....GN #.#...#...#.....#.....#.#........UE
#.#.#.#.#.#########.###.#.###.#.# #.#.#####.###.#.###.###.###.###.#
#.....#.#.....#.#.....#.#...#.#.# ZM....#...#.#...#.#.#.......#...#.#
#.#.#.#.#.#####.###.#####.#.#.#.# #.#.###.###.#.###.#.#.#####.###.#
#.#.#...#.................#.#.#.# #.#.........#...#...#...#.....#.#
#.###.#.#####.###.#.#####.#####.# M P I R E N M #.###.#.#.###.#######.#####.###.#
#.#...#.....#...#.#...#.#...#.#.# C G X X Y O R #...#.#.#.#...#.....#.....#...#.#
#####.###.###.#.#.#####.#.###.#######.#######.###########.#####.#####.#####.###########.#########.#######.#.#####.#.#.###.###
#.#.#...#.#...#.#.#.............#.....#.#.#...#...#.........#...#.......#.......#.#...#.#.......#.#.............#.#.#.#.#...#
#.#.#.#.#.###.###.###.###.###.###.#.#.#.#.###.#.#.###.#########.#######.#######.#.###.#.###.#.#####.###.#.###.#########.#####
#...#.#.#...#...#...#...#...#.#...#.#...#.#.....#.#...#.....#...#.#.#.#.....#.#.#.#.#.#.....#.#...#...#.#.#.............#...#
###.#.#######.#.#####.###.#.#####.#.###.#.#######.#.###.#.###.#.#.#.#.###.###.#.#.#.#.#.#######.#######.#####.###.#######.###
#.......#.....#.#.......#.#...#...#.#.....#...#.#.#.....#...#.#.#...........#.#...#...#...........#.#.#...#.....#...........#
#.#.#.#.#.#.#.#.#.#.#.#.###.#.###.#.#.###.###.#.#.#######.#####.###.###.#.###.#.###.#.#.#.###.#.#.#.#.#.###.#.###.###.###.###
#.#.#.#.#.#.#.#.#.#.#.#.#...#...#.#.#.#.#.#.#.........#...#.#...#.....#.#...#...#...#...#...#.#.#.#.#...#...#.#.....#.#.#...#
###.#.#####.#######.#.###.###.###.#####.#.#.#.#####.#####.#.###.#####.#.#.#####.#.#.###.#######.#.#.#.#######.#######.#.#####
#...#...#...#.#.#...#...#.#.#...#.#.......#...#.#...#.....#.....#.....#.#...#...#.#.#.........#.#.#...#.........#...........#
###.#####.###.#.#.#.#######.#.#########.#.#.###.#.#####.#.#.#.#####.#####.#####.#.#########.#########.#####.#.###.#####.###.#
#.....#.....#.....#.......#.....#.......#.#.....#...#...#.#.#.....#.#.#.#.#...#.#.......#...........#.#.#.#.#...#...#.....#.#
###.#.###.#######.###.###.###.###.###.###.#.#.###.###.###.#######.#.#.#.#####.#.#.#.#########.###.#.#.#.#.#####.#.#.#.#.#.#.#
#...#...#...#.#.....#.#.....#.#...#.#.#...#.#...#.#.#.#.#.#.#...#.#.....#.#...#.#.#.........#.#.#.#.#.........#.#.#.#.#.#.#.#
#.###.#.###.#.#.#.#.#####.#########.#####.#.#######.###.#.#.###.#.#.###.#.###.#.#####.#######.#.#######.#.#########.###.#.#.#
#.#...#.#...#...#.#.#.......#...#.#.......#.#.......#...#...#...#.#...#.#.#.......#...#.#.#.#...#.....#.#.#.#...#.....#.#.#.#
#.#.#.#.#####.#.#.#####.#####.###.#.#####.#.###.#######.#.#####.#.#####.#.###.#######.#.#.#.###.#.#####.###.#.#####.#.#######
#.#.#.#...#...#.#.#.......#.#.#.#.#.#.#.#.#.#...#.#.#...#...#.#.......#.....#...#...........#.#...#.#.....#...#...#.#...#.#.#
###.###.#.#.#########.#####.#.#.#.###.#.#.#.###.#.#.###.#.###.#.###.#.###.###.#####.###.#####.#####.#.#.###.#####.###.###.#.#
#.....#.#.#.#.........#.......#.#...#.....#.....#.....#...#.......#.#...#.#.......#...#.......#.....#.#...#...#.....#.#.#...#
#.#.###.#.#.#.###.#.#######.#.#.#.#####.#.#.###.###.#.#.#####.#.#####.###.###.#########.###.#.###.#####.###.###.#.#####.#.###
#.#...#.#.#.#.#.#.#...#.....#.........#.#.#.#.....#.#.#...#.#.#...#.#.#.#.#.......#.......#.#...#...#.#.........#...........#
#.#.###.#######.#.#.#####.#########.#####.#######.#.#.#.###.###.#.#.###.#.#####.#######.#########.###.#.###.#.#.###.#.#.###.#
#.#.#.......#...#.#.#.#...#...#.....#.#...#.......#.#.......#...#.#.........#...#...............#.#.#.#.#.#.#.#...#.#.#.#.#.#
#.#####.#######.#####.#######.#.###.#.###.#.#######.###########.#.#.###########.#####.###.###.###.#.#.###.###.#.#####.###.###
#.#.....#...............#.......#.....#...#.......#...#...#...#.#.#...#.......#.....#...#...#...#.......#...#.#.....#.#...#.#
#.#.###.#.#.#.#.#######.#.#.#.#######.###.#######.###.###.#.#.###.###.#####.#.#.#.###.###########.###.###.#####.#.#####.###.#
#.#.#...#.#.#.#.#.#.......#.#...#.........#.........#...#.#.#...#.#.#...#.#.#...#.#.......#.........#.........#.#.#...#...#.#
#.#.###.###.#####.#.#####.#.#####.###.#.###.#.#######.###.###.#.#.#.###.#.#.###.#####.#.###.#####.#.#.###.#####.#####.#.###.#
#.#.#.#.#...#...#...#.....#.#.....#...#.#.#.#...#...#...#.#...#...#.......#...#.....#.#.......#.#.#.#.#.....#.#.............#
#####.#.#.#.#.#.###.#.###.#####.#####.###.#.###.#.#.#.###.#.#######.###.#####.#######.#.#.#.###.#.#.###.#####.###.#.#.###.#.#
#.......#.#.#.#.....#.#.....#...#.......#.....#.#.#.......#.......#...#.#...........#.#.#.#.....#.#.#...........#.#.#...#.#.#
#####################################.#######.#########.#.#######.#.###########.#######.#####################################
Z A R A F Q R P
R D B A K G D G

Part 1:
namespace AdventOfCode2019_20_1
{
	const DAY     = 20;
	const PROBLEM = 1;

	type Point = [number, number];

	export async function run()
	{
		let input = await AdventOfCode.getInput(DAY);
		if (input == null) return;

		AdventOfCode.setIntermedOutputSize("0.55vw");

		let grid = input
			.split(new RegExp('\r?\n'))
			.filter(p => p.trim().length > 0)
			.map(p => p.split('').map(q => q) );
		
		const width  = grid[0].length;
		const height = grid.length;

		let tunnels:     {[_:string]: [Point, Point]} = {};
		let tunnelsList: {[_:number]: Point} = {};
		let entry: Point = [0, 0];
		let exit: Point = [0, 0];

		for (let y=1; y<height-1; y++)
		for (let x=1; x<width-1;  x++)
		{
			let v = grid[y][x];

			if (isUpperChar(v))
			{

				let key;
				let pos: Point;
	
				if (grid[y-1][x] === "." && isUpperChar(grid[y+1][x]))
				{
					key = v + grid[y+1][x];
					pos = [x, y-1];
				}
				else if (grid[y][x+1] === "." && isUpperChar(grid[y][x-1]))
				{
					key = grid[y][x-1] + v;
					pos = [x+1, y];
				}
				else if (grid[y+1][x] === "." && isUpperChar(grid[y-1][x]))
				{
					key = grid[y-1][x] + v;
					pos = [x, y+1];
				} 
				else if (grid[y][x-1] === "." && isUpperChar(grid[y][x+1]))
				{
					key = v + grid[y][x+1];
					pos = [x-1, y];
				}
				else continue;
	
				if (key === "AA")
				{
					entry = pos;
					AdventOfCode.outputConsole("[AA] := " + point_to_str(pos));
				}
				else if (key === "ZZ")
				{
					exit = pos;
					AdventOfCode.outputConsole("[ZZ] := " + point_to_str(pos));
				}
				else if (key in tunnels)
				{
					tunnels[key][1] = pos;
					tunnelsList[id(tunnels[key][0])] = tunnels[key][1];
					tunnelsList[id(tunnels[key][1])] = tunnels[key][0];

					AdventOfCode.outputConsole("[PORTAL] := " + point_to_str(tunnels[key][0]) + " <--> " + point_to_str(tunnels[key][1]));
				}
				else 
				{
					tunnels[key] = [pos, [-1, -1]];
				}
			}
		}

		for (let y=0; y<height; y++)
		for (let x=0; x<width;  x++)
		{
			let v = grid[y][x];
			if (v === " ") v = "#";
			if (isUpperChar(v)) v = "#";
			grid[y][x] = v;
		}

		await AdventOfCode.outputIntermed(toStr(grid));
		
		// ------------------

		let distmap: {[id:number]: number} = {};

		let dqueue: [number, Point][] = [];
		dqueue.push([0, entry]);

		while (dqueue.length>0)
		{
			await AdventOfCode.outputIntermed(toStr2(grid, dqueue, tunnelsList));
			//await AdventOfCode.sleep(1000);

			const [dist, pt] = dqueue.shift()!;
			const [ptx, pty] = pt;
			let ptid = id(pt);
			
			if (ptid in distmap && distmap[ptid] <= dist) continue;

			distmap[ptid] = dist;

			if (grid[pty-1][ptx] === '.') dqueue.push([ dist+1, [ptx, pty-1] ]);
			if (grid[pty][ptx+1] === '.') dqueue.push([ dist+1, [ptx+1, pty] ]);
			if (grid[pty+1][ptx] === '.') dqueue.push([ dist+1, [ptx, pty+1] ]);
			if (grid[pty][ptx-1] === '.') dqueue.push([ dist+1, [ptx-1, pty] ]);

			if (ptid in tunnelsList) dqueue.push([dist+1, tunnelsList[ptid]]);
		}

		await AdventOfCode.outputIntermed(toStr(grid));

        AdventOfCode.output(DAY, PROBLEM, distmap[id(exit)].toString());
	}

	function id (p: Point) { return p[1]*10000+p[0]; }

	function isUpperChar(c: string): boolean
	{
		return c.charCodeAt(0) >= "A".charCodeAt(0) && c.charCodeAt(0) <= "Z".charCodeAt(0);
	}

	function toStr(grid: string[][]): string
	{
		return grid.map(p => p.join("") ).join("\n");
	}

	function toStr2(grid: string[][], highlights: [any, Point][], tunnels: {[_:number]: Point}): string
	{
		const width  = grid[0].length;
		const height = grid.length;

		let str = "";
		for (let y=0; y<height; y++)
		{
			for (let x=0; x<width;  x++)
			{
				if (highlights.some( p => p[1][0] === x && p[1][1] === y )) str += "O";
				else if (id([x,y]) in tunnels) str += "@";
				else str += grid[y][x];
			}
			str += "\n";
		}

		return str
	}

	function point_to_str (p: Point) { return `[${p[0]}|${p[1]}]`; }
}
Result: 602

Part 2:
namespace AdventOfCode2019_20_2
{
	const DAY     = 20;
	const PROBLEM = 2;

	type Point = [number, number];

	export async function run()
	{
		let input = await AdventOfCode.getInput(DAY);
		if (input == null) return;

		AdventOfCode.setIntermedOutputSize("0.35vw");

		let grid = input
			.split(new RegExp('\r?\n'))
			.filter(p => p.trim().length > 0)
			.map(p => p.split('').map(q => q) );
		
		const width  = grid[0].length;
		const height = grid.length;

		let tunnels:     {[_:string]: [Point, Point]} = {};
		let tunnelsList: {[_:number]: [boolean, Point]} = {};
		let entry: Point = [0, 0];
		let exit: Point = [0, 0];

		for (let y=1; y<height-1; y++)
		for (let x=1; x<width-1;  x++)
		{
			let v = grid[y][x];

			if (isUpperChar(v))
			{
				let isinner = x>3 && y>3 && x<width-3 && y<height-3;

				let key;
				let pos: Point;
	
				if (grid[y-1][x] === "." && isUpperChar(grid[y+1][x]))
				{
					key = v + grid[y+1][x];
					pos = [x, y-1];
				}
				else if (grid[y][x+1] === "." && isUpperChar(grid[y][x-1]))
				{
					key = grid[y][x-1] + v;
					pos = [x+1, y];
				}
				else if (grid[y+1][x] === "." && isUpperChar(grid[y-1][x]))
				{
					key = grid[y-1][x] + v;
					pos = [x, y+1];
				} 
				else if (grid[y][x-1] === "." && isUpperChar(grid[y][x+1]))
				{
					key = v + grid[y][x+1];
					pos = [x-1, y];
				}
				else continue;
	
				if (key === "AA")
				{
					entry = pos;
					AdventOfCode.outputConsole("[AA] := " + point_to_str(pos));
				}
				else if (key === "ZZ")
				{
					exit = pos;
					AdventOfCode.outputConsole("[ZZ] := " + point_to_str(pos));
				}
				else if (key in tunnels)
				{
					if (isinner) tunnels[key][0] = pos;
					else         tunnels[key][1] = pos;
					tunnelsList[id(tunnels[key][0])] = [true, tunnels[key][1]];  // down
					tunnelsList[id(tunnels[key][1])] = [false, tunnels[key][0]]; // up

					AdventOfCode.outputConsole("[PORTAL] := " + point_to_str(tunnels[key][0]) + " --> " + point_to_str(tunnels[key][1]));
				}
				else 
				{
					if (isinner) tunnels[key] = [pos, [-1, -1]];
					else         tunnels[key] = [[-1, -1], pos];
				}
			}
		}

		for (let y=0; y<height; y++)
		for (let x=0; x<width;  x++)
		{
			let v = grid[y][x];
			if (isUpperChar(v)) v = " ";
			grid[y][x] = v;
		}

		await AdventOfCode.outputIntermed(toStr(grid));
		
		// ------------------

		let distmap: {[id:number]: number} = {};

		let dqueue: [number, number, Point][] = []; //<len, depth, pos>
		dqueue.push([0, 0, entry]);

		let last_dist = -1;
		while (dqueue.length>0)
		{

			const [dist, depth, pt] = dqueue.shift()!;
			const [ptx, pty] = pt;
			let ptid2 = id2(pt, depth);
			let ptid1 = id(pt);
			
			if (ptid2 in distmap && distmap[ptid2] <= dist) continue;

			if (dist !== last_dist)
			{
				await AdventOfCode.outputIntermed(toStr2(grid, dqueue, tunnelsList)+"\n\n" + dist);
				last_dist = dist;
			}

			if (depth === 0 && ptx === exit[0] && pty === exit[1])
			{
				AdventOfCode.output(DAY, PROBLEM, dist.toString());
				await AdventOfCode.outputIntermed(toStr(grid));
				return;
			}

			distmap[ptid2] = dist;

			if (grid[pty-1][ptx] === '.') dqueue.push([ dist+1, depth, [ptx, pty-1] ]);
			if (grid[pty][ptx+1] === '.') dqueue.push([ dist+1, depth, [ptx+1, pty] ]);
			if (grid[pty+1][ptx] === '.') dqueue.push([ dist+1, depth, [ptx, pty+1] ]);
			if (grid[pty][ptx-1] === '.') dqueue.push([ dist+1, depth, [ptx-1, pty] ]);

			if (ptid1 in tunnelsList) 
			{
				const [tunnel_dir, tunnel_pos] = tunnelsList[ptid1];
				if (depth ===0 && !tunnel_dir) { /* wall */ }
				else dqueue.push([dist+1, depth + (tunnel_dir ? 1 : -1), tunnel_pos]);
			}
		}


	}

	function id (p: Point) { return p[1]*10000+p[0]; }

	function id2(p: Point, d: number) { return p[1]*1000000+1000*p[0]+d; }

	function isUpperChar(c: string): boolean
	{
		return c.charCodeAt(0) >= "A".charCodeAt(0) && c.charCodeAt(0) <= "Z".charCodeAt(0);
	}

	function toStr(grid: string[][]): string
	{
		return grid.map(p => p.join("") ).join("\n");
	}

	function toStr2(grid: string[][], highlights: [any, any, Point][], tunnels: {[_:number]: [any, Point]}): string
	{
		const width  = grid[0].length;
		const height = grid.length;

		let str = "";
		for (let y=0; y<height; y++)
		{
			for (let x=0; x<width;  x++)
			{
				if (highlights.some( p => p[2][0] === x && p[2][1] === y )) str += "O";
				else if (id([x,y]) in tunnels) str += "@";
				else str += grid[y][x];
			}
			str += "\n";
		}

		return str
	}

	function point_to_str (p: Point) { return `[${p[0]}|${p[1]}]`; }
}
Result: 6986


made with vanilla PHP and MySQL, no frameworks, no bootstrap, no unnecessary* javascript