A Regular Map


Fork me on GitHub
2018-12-20

Day 20: A Regular Map

Description:
--- Day 20: A Regular Map ---

While you were learning about instruction pointers, the Elves made considerable progress. When you look up, you discover that the North Pole base construction project has completely surrounded you.

The area you are in is made up entirely of rooms and doors. The rooms are arranged in a grid, and rooms only connect to adjacent rooms when a door is present between them.

For example, drawing rooms as ., walls as #, doors as | or -, your current position as X, and where north is up, the area you're in might look like this:

#####
#.|.#
#-###
#.|X#
#####

You get the attention of a passing construction Elf and ask for a map. "I don't have time to draw out a map of this place - it's huge. Instead, I can give you directions to every room in the facility!" He writes down some directions on a piece of parchment and runs off. In the example above, the instructions might have been ^WNE$, a regular expression or "regex" (your puzzle input).

The regex matches routes (like WNE for "west, north, east") that will take you from your current room through various doors in the facility. In aggregate, the routes will take you through every door in the facility at least once; mapping out all of these routes will let you build a proper map and find your way around.

^ and $ are at the beginning and end of your regex; these just mean that the regex doesn't match anything outside the routes it describes. (Specifically, ^ matches the start of the route, and $ matches the end of it.) These characters will not appear elsewhere in the regex.

The rest of the regex matches various sequences of the characters N (north), S (south), E (east), and W (west). In the example above, ^WNE$ matches only one route, WNE, which means you can move west, then north, then east from your current position. Sequences of letters like this always match that exact route in the same order.

Sometimes, the route can branch. A branch is given by a list of options separated by pipes (|) and wrapped in parentheses. So, ^N(E|W)N$ contains a branch: after going north, you must choose to go either east or west before finishing your route by going north again. By tracing out the possible routes after branching, you can determine where the doors are and, therefore, where the rooms are in the facility.

For example, consider this regex: ^ENWWW(NEEE|SSE(EE|N))$

This regex begins with ENWWW, which means that from your current position, all routes must begin by moving east, north, and then west three times, in that order. After this, there is a branch. Before you consider the branch, this is what you know about the map so far, with doors you aren't sure about marked with a ?:

#?#?#?#?#
?.|.|.|.?
#?#?#?#-#
?X|.?
#?#?#

After this point, there is (NEEE|SSE(EE|N)). This gives you exactly two options: NEEE and SSE(EE|N). By following NEEE, the map now looks like this:

#?#?#?#?#
?.|.|.|.?
#-#?#?#?#
?.|.|.|.?
#?#?#?#-#
?X|.?
#?#?#

Now, only SSE(EE|N) remains. Because it is in the same parenthesized group as NEEE, it starts from the same room NEEE started in. It states that starting from that point, there exist doors which will allow you to move south twice, then east; this ends up at another branch. After that, you can either move east twice or north once. This information fills in the rest of the doors:

#?#?#?#?#
?.|.|.|.?
#-#?#?#?#
?.|.|.|.?
#-#?#?#-#
?.?.?X|.?
#-#-#?#?#
?.|.|.|.?
#?#?#?#?#

Once you've followed all possible routes, you know the remaining unknown parts are all walls, producing a finished map of the facility:

#########
#.|.|.|.#
#-#######
#.|.|.|.#
#-#####-#
#.#.#X|.#
#-#-#####
#.|.|.|.#
#########

Sometimes, a list of options can have an empty option, like (NEWS|WNSE|). This means that routes at this point could effectively skip the options in parentheses and move on immediately. For example, consider this regex and the corresponding map:

^ENNWSWW(NEWS|)SSSEEN(WNSE|)EE(SWEN|)NNN$

###########
#.|.#.|.#.#
#-###-#-#-#
#.|.|.#.#.#
#-#####-#-#
#.#.#X|.#.#
#-#-#####-#
#.#.|.|.|.#
#-###-###-#
#.|.|.#.|.#
###########

This regex has one main route which, at three locations, can optionally include additional detours and be valid: (NEWS|), (WNSE|), and (SWEN|). Regardless of which option is taken, the route continues from the position it is left at after taking those steps. So, for example, this regex matches all of the following routes (and more that aren't listed here):

ENNWSWWSSSEENEENNN
ENNWSWWNEWSSSSEENEENNN
ENNWSWWNEWSSSSEENEESWENNNN
ENNWSWWSSSEENWNSEEENNN

By following the various routes the regex matches, a full map of all of the doors and rooms in the facility can be assembled.

To get a sense for the size of this facility, you'd like to determine which room is furthest from you: specifically, you would like to find the room for which the shortest path to that room would require passing through the most doors.

In the first example (^WNE$), this would be the north-east corner 3 doors away.
In the second example (^ENWWW(NEEE|SSE(EE|N))$), this would be the south-east corner 10 doors away.
In the third example (^ENNWSWW(NEWS|)SSSEEN(WNSE|)EE(SWEN|)NNN$), this would be the north-east corner 18 doors away.

Here are a few more examples:

Regex: ^ESSWWN(E|NNENN(EESS(WNSE|)SSS|WWWSSSSE(SW|NNNE)))$
Furthest room requires passing 23 doors

#############
#.|.|.|.|.|.#
#-#####-###-#
#.#.|.#.#.#.#
#-#-###-#-#-#
#.#.#.|.#.|.#
#-#-#-#####-#
#.#.#.#X|.#.#
#-#-#-###-#-#
#.|.#.|.#.#.#
###-#-###-#-#
#.|.#.|.|.#.#
#############

Regex: ^WSSEESWWWNW(S|NENNEEEENN(ESSSSW(NWSW|SSEN)|WSWWN(E|WWS(E|SS))))$
Furthest room requires passing 31 doors

###############
#.|.|.|.#.|.|.#
#-###-###-#-#-#
#.|.#.|.|.#.#.#
#-#########-#-#
#.#.|.|.|.|.#.#
#-#-#########-#
#.#.#.|X#.|.#.#
###-#-###-#-#-#
#.|.#.#.|.#.|.#
#-###-#####-###
#.|.#.|.|.#.#.#
#-#-#####-#-#-#
#.#.|.|.|.#.|.#
###############

What is the largest number of doors you would be required to pass through to reach a room? That is, find the room for which the shortest path from your starting location to that room would require passing through the most doors; what is the fewest doors you can pass through to reach it?

--- Part Two ---

Okay, so the facility is big.

How many rooms have a shortest path from your current location that pass through at least 1000 doors?

Input:
^ESWSWSESEENENESENEESEESEENNESSESSSSSEEEESENENESESWSESSESSENNEENWNWNENNWSW(SSSEWNNN|)NWNNNENNNNESEEEESSSENNNNWNNWSWS(E|WNNWNEE(S|ENWNWWWS(SWNNNNWSSSSSWNNWWSWWNNWWNNEENEEENWNNWNWWWNEEENWWNENWNNENNWSWWWNWSSSWNNWNENNESEESENNESEENWNNESENNWWNENEEENNWSWNNNENEESS(W(N|W)|EENWNEENWNNENWNENENNENEEESESWSEEEENWNW(NEENWWW(WNNESEENNENENWWWNEEEEESENESESSSWSESWWWNNNW(N(EEESWSS(NNENWWEESWSS|)|N)|SW(N|WSESE(N|SEESWWSSESSSEENWNENEESSEESWWSESWWSWSESEEESEESSESWWSSEEN(W|ESSSWSSSWNNNWWSSE(SWWWWWSEESENESSSWNWWSSE(EEENNENEEESWSW(N|SSW(NN|WWSEESEEESWSEENNNWW(W|NEN(EN(NESEE(SWSES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NNWWSS(ENSW|)WNWWNEENWWNNWNWNWWNWNWNNNNWWWSSWSWNNENWNEEENNNEESWSSEESESENNNESENESENNESESSW(SEENESSESWWNWSSESWSWWW(SEESWSEESENNN(WSNE|)NE(NNESEENESENEESSS(WNNSSE|)SENNNNNWNEESE(SWSSSSES(EEN(WNWESE|)ESEENW|W)|NNWWWWWNNWSSSS(ENESNWSW|)WW(S|NENNNNWWWW(SEEESWSE(WNENWWEESWSE|)|NNNWS(S|WNWWNNNWSSWSWNNWWSWWWS(EEESSE(S(WW|S)|N(EES(W|EN(E(E|S)|N))|NN))|WNWNNEENENNESENEEESWWSEE(ENEES(W|SSENEEENNWNWW(SSE(N|E)|NEENWNEENWNNNNEENEEEENNN(ENEN(ESEENEEEESSENENNE(SE(SSESSENE(NWNWESES|)SSSWSSWNWSSWSESEESESSWSSSENNEESSSESWSSWWWWSSWNNNWNWWSSWWSEEEE(NWNSES|)SS(SENEENEN(W|ESSENEN(ENN(WSNE|)NENWNWNNNESSESESSESSEE(E|NWNNNE(E|NWW(SS|WNENWNENWWS(WNWNWNNESEE(ESWWEENW|)NWNWWNEENNN(WWW(NEEEWWWS|)SS(ENESNWSW|)SSW(N|SS(WNWSNESE|)SE(NN|SSSEN(N|E)))|ESE(S(WSSNNE|)EE|N))|SS)))|SWWWNNWSSSSW(WSEEE(NE(N(W|EE)|S)|SWWWWNWW(N|S(WWNE|ES)))|N))|W))|WWW(S|WN(EEE|WNNNWWWNWWNENEES(ENESS(ENNENNNWNNESENNWWWNNWWNENWWWWNNW(NEEESS(WNSE|)EEEEESEENNW(S|WNWN(WSS(E|WWNENN(NEESWENWWS|)WSWNWSW(W|N))|EEEE(N(W|N)|ESW(SES(ENEWSW|)WSSWWSES(WWNNW(WNEWSE|)SS|SESSESWW(NN|WSSSES(WWSSWENNEE|)EENWNEN(WWSNEE|)E(SSSS(ENEE(SSW(WW|N)|NN)|W)|NN(NWES|)E)))|WW))))|SWSWWSWN(NEEWWS|)WWSSE(SSSENNNESSSSWWW(SEESWSEENEEENNNWW(SESWWEENWN|)NEENNWW(SEWN|)NEE(EEESWSSESWW(SSSS(SWNWSSESSSWWS(WWNWSWNNNWW(NEEENEESE(NN|SS(ENSW|)W(S|NW(SWNSEN|)N))|WW)|ES(ESENEEENN(ESSS(E|WWS(W(N|WW)|EE))|WSWNW(NE|SW))|W))|ENNNESSSENEE(ESWWEENW|)NNWW(SEWN|)NE(E|NWN(W|E)))|NNN)|N)|N)|N))|WW)|W))))|N)|NNWNWWNWNNEN(EEESWWS(SEE(NWES|)S|W)|NWSWNWSSWNWWNNWWW(NEEEWWWS|)SSWSEENE(NWES|)SEESWWSSSEESWSE(S|ENEES(ENN(WWWNWN(WSNE|)EEE(N(N|WW)|S(W|E))|ESSE(N(E|N)|S))|W))))|W)|WWWNN(ESNW|)NNWSWNWWN(EEENSWWW|)NWSSSESWSSESSWNWWNENWNWNENE(NWWWS(WNWNWSSSWSWSWNWNNWSWSSWWSWWSWSWWWWSEESENEEEENN(WSWENE|)EEN(W|EE(NWNSES|)SSW(N|WWSSWWWSSSESESSWSSENESEENWNENESSE(SSW(N|SWWWSESSSSE(SE(SWWWWNNE(SEWN|)NNNWSWS(E|WSSE(N|SWWWWSWNNNENWWWWWNNWNWNWSWWSSENESE(ESWWSWNWWWNWSWWNWNWSWNWNEEENENENNWSWSWWS(WWSWNNWNWNEENWNWSWWNENEEESESEESS(ENNNEENNNENWNNEES(ESSSSSESWW(SEEENNENWWNNESEESSEEESWSEEENNENESESWSESSSENEE(ESE(NEEEWWWS|)SWSWWS(E|SW(NWWWWNW(NWNWNEN(WWWW(WWSWSW(NNEWSS|)WSEEE(S(WWW(WNSE|)S|S)|NENE(S|EE))|NEENWW)|ESENE(NN|SE(N|SWS(WNWESE|)EES(ENENES|WW))))|S)|SS(ENSW|)W))|NWNW(S|NNESE(N(NWWWNENWN(WWWSEESWWSW(WNNWNNWSWW(SEESSEN(SWNNWWEESSEN|)|WWNNESEENWNENESENNNEENWWNNEENWNNE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SSWWN(WNWSWWNNWSWNWNENEN(WNNE(S|NNWSWWNNNWWWWWWNENWWNNWWN(WSWNWWSESSESWSSEESWSSWSSENEESWSSWSWSSWNNNN(NWSWSESWSESSESSESESWSSWSEESENNWNEENEESENNWNENNENNWNWWSWSWW(SS(WNSE|)ES(S(EENNW(S|N(W|ENE(N|E|SS)))|S)|W)|NENENEN(W|ENNNENWWS(SS|WN(WSNE|)NEEN(NNWSW(SEWN|)W(W|NNN(EESWSNENWW|)W)|EEESW(SEEEE(NNWWSE|SSSENE(NWES|)SSWSWSESWSWWW(SEEEENE(N|SEE(NWES|)ESSEESENN(WWNEEWWSEE|)ESSESWSWWN(E|WWSESEEESWSWNWSSWSSESENEEENWN(WWS(W|E)|NESENENWNW(S|NENNN(WNEWSE|)EESSSW(NN|SEEE(S(E|WSW(SESWW(N|SSSWN(NN|WSWW(NEWS|)SSWSSESESESWSWWSSWSSEEESWSSSSSWSWNWSWWWSSWNWSWSSEESSWWN(E|WSWWNNNNWNNWSWSSE(ESSSWNNWWNWSSSSWNNWSSWNWSWSSSWNWSSWWSSWSWNWWSW(NNNNESSENNEE(SWSEWNEN|)NWNWNEENNWSWNNNWW(NEENNWW(SEWN|)NNNESSEENEEESESEENWNWNWWWNENNWSWSSW(S|NNNENENWNWWW(NENENNWNNW(SSS(S|E)|NEESEEESESSWWW(SES(WW|SEESWSEENNNN(WWSEWNEE|)EENNESEESSENNEESENNEEENEEESWSESSWWWNE(E|N(WWWSESWWSWSWSSSSWNNNWWWWSW(WSES(WWNNSSEE|)ES(ES(W|SSSWSWWSESWSES(WWNW(SSS(E(NEESW|SWSE)|W)|NNNE(N(EE|WWN(E(NWES|)E|WSSSSENN(SSWNNNSSSENN|)))|SS))|ENNEN(W|NENNNENWNE(NNNWWS(SENSWN|)W|ESSENESE(SWSWWW(NEEWWS|)SE(EE|SSWNWSSW(ENNESEWNWSSW|))|ENESEENNW(S|NNNWSSWNNNW(NENEEEEESSSESENENENWWW(SEWN|)NENNNW(S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)))|S)|SSEE(SS|N(W|NNEE(S(SWNSEN|)E|N))))|SS)))))))|WSSSS(W|E(EE|S|NNN)))))))))|W)|NNNEN(W|ENESSWS(W|EENE(S|EN(E|N)))))|N)))|NN(ESEWNW|)W))|SSE(NESNWS|)SSW(SEWN|)N))|SESSW(SESWSEE(WWNENWESWSEE|)|N))|SSSSESSW(SSSSSSESSEENESEENEESWSWWSSWNWN(WWW(SESE(N|ESWSWNW(N|SSESSESSEESWSSSSSWNNWSSSW(SEESESESWWS(WNN(NWSSSNNNES|)E|EEENNNENNNWSW(S(WNWESE|)(S|E)|NNNENWNEESSS(EEESWSESWSW(NNN|SSS(WNNSSE|)EENEENNW(NEENESSENESEESESS(ENNEESEENNESESWS(WWWWNSEEEE|)EENNNWNWWWS(S|WNNWSSWNNWNWSS(ESNW|)WNNWW(SEWN|)NNEES(ENNNESEENESSSENNNNWNEENWWWNWSWNNNNWNEEEEEESWSSENEESWS(SSSWSEEENESEENWNNNNWSW(WSS(EENWESWW|)S|NNENN(ESES(W|ESEEEEN(NWWW(SEEWWN|)W|ESSWWWWSESWSW(NNNW(SS|N)|SSSSWWNN(ESNW|)WWWWSSW(NN|SW(SEEEESEESWSSENEEENENWNWN(ENENESESSW(NWSNES|)SEENEESSSWSS(WNWNN(WSSS(WN(WSWW(NEWS|)WW|N)|E)|E(ENSW|)S)|EEN(NENWNENWNENENNENESEENESSESEEEESEENWNWN(NNNWWWSEESWWW(SEEESWW(EENWWWEEESWW|)|NNWWN(WWNNWWSWWWNW(NENESSENNESENES(NWSWNWESENES|)|SSSESENN(W|ESENE(SS(WSW(WWWSEEESWWS(E|SWWNENWWNWWW(NENENESS(W|E(S|N(NNWWEESS|)E))|S))|N)|EE(NWES|)(EE|S))|N)))|EEN(ESEEENWW(EESWWWEEENWW|)|W)))|EEENEE(NWNEWSES|)SSSWSSW(SESESENENNEE(SSW(SWSWS(EENENEE(SWS(EEE|W)|NNW(N|S))|WWN(E|NWWN(WWSESS(ENESNWSW|)WN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ENSWW|)W(SSENSWNN|)NW(S|WW)|NNNNEES(W|ENEESSSW(NN|SEESEENESE(NNNNNESSENNNEESS(W(SSWWSE|N)|ENNNEENESENNWWWSWNWSWWNENNENESS(W|ENEN(NW(S|N(E|WSWWWWNWSSSENE(SSWWWSSSS(EEN(NNWSSNNESS|)EEE|SWWNENNNNWNENNWWNWNENENEESWSWSEE(NEENN(EESSW(SEENENW(ESWSWWEENENW|)|N)|W(S|NENWWNWNN(ESNW|)WSWWSW(NNEEN(NN|E)|SSWSES(ENNNES(S|EN(NW|ESE))|SWNWNWNEN(WWSS(W|SESESESSESSESWWSWSEEEN(W|ES(ENNWNNN(W(NWNSES|)S|E)|S(SSEESE(SWW(WNEWSE|)S|N)|WWWWW(NNNNN(WWSESWW(EENWNEWSESWW|)|NE(SESWSNENWN|)N)|W)))))|E)))))|SSS))|E)))|ESS(W|EESSSS(W|ENNESSSS(WNWSNESE|)ENNNEN(ESEEESWWWS(W(SEWN|)N|E)|WNNWSWN(SENESSNNWSWN|)))))))|SSWS(WSESWWWSWWNW(S|NEE(S|NNN(ESE(SSWNSENN|)EN(E|W)|WN(WSWSEESW(ENWWNEWSEESW|)|E))))|E)))))|E))|SSSWW(NEWS|)SW(N|S)))|W))))|NN(WSWW(NEWS|)WWWSSENEESS(SSSWNWSS(EEENSWWW|)WWS(E|WNNWWSWN(WWWSEES(ESEE(SE(N|SW(WNSE|)SESSEENW(ESWWNNSSEENW|))|N(N|W))|W)|NEENNNW(SWSEWNEN|)NEESE(SEES(ENNSSW|)WSSWNWN(N|E)|NN)))|E)|NNN))))|W)|SS))|WNW(NNNESSE(NN|E)|S))|WWN(E|W|N))|N(WS|ENN))))))|NN))|WWNNWSS(NNESSEWNNWSS|)))))|W)|WW(N|WSSE(N|S))))|SSW(SSES(W|EEE(S(S|WWWS|EN)|NWWN(NE(NWES|)S|W)))|N)))|W)))))|WWWNNWW(SE|NEE))|W))|WNWSWWWNN(W(SSWWEENN|)N|EE(SWEN|)E))|SW(WSNE|)N))|W)))|NNNNNESENNWNW(N(E|NN)|S))))|NN)|E)|N))|N))))|N))|NNNW(SS|NENESS(ENE(S|ENN(WSWENE|)ENESE(SS(WWNEWSEE|)ENEESWSEEEEES(ENNNNWWNN(ESENE(NWWEES|)SENEEEE(SWWSES(WWN(WWSEWNEE|)N|E(N|SEESEEN(E|W)))|NESENN(ESSNNW|)NW(NNES|SWNWW))|WWWSSENESSEESWWWN(SEEENWESWWWN|))|SWNWWWSEE(WWNEEEWWWSEE|))|NN))|S))))))))|NNNEE(SSWNSENN|)N(E|WNWWS(W(NNENEEES(WW|S)|S(ESNW|)W)|E))))|W)))))|E(S|E))|EEEESEEN(EEESWSW(WW(WWNSEE|)SSENESENESENESENEEEEES(WWWWSS(SENNSSWN|)WNWSW(N|S)|ENESENNNEN(WWSSWWWNN(ESENSWNW|)WSWNWWSS(WNNWSSW(ENNESSNNWSSW|)|E(EE|N))|EESSEEEEESESEEESSESWWNNWWWNWSSEESSWNWSSSSSWSES(WWNNWWWW(SES(EENWESWW|)SS|NNNW(NNEEEENWNNEEE(SSSW(NNWESS|)SWSWNW(SSSENEEE(SWWEEN|)N(N|W)|W)|N(WWWWSSW(NNNENSWSSS|)WS(WNSE|)EE|E))|SS))|ENENNNW(NEESSESES(WWNSEE|)ENEE(SWEN|)NNNEENWWNEENNWWNNNENN(EESSW(N|SES(WWNSEE|)SEENWNNEES(W|SSESES(WWSWWS(E|SSWN(WWSSEN|NNNEEN(WW|E)))|E(NENNWSWNWNNNEEEESESW(SES(ESEENESESWSESWSES(EENNW(NNESEES(WS(E|SS(SSSWENNN|)W)|ENNNWNWS(SEWN|)WWNNW(WWW(NENEEES(WW|ENNWWN(EEEESWSSEEESS(WNWWWS|ES(WSNE|)ENNNE(EEN(E(SS|E)|WWWNN(EES(ENSW|)W|WWSW(S(W|EE(N|SS))|N)))|SSS))|WWWS(EE|W(S(E|SS)|NW(WWWWWWSWNWS(NESENEWSWNWS|)|S)))))|S)|S))|S)|WWWNW(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WSW(SSES(SEN(ESS(E(NEEEWWWS|)SSW(N|S(ESNW|)W)|WW)|NN(N|W))|WWW(NENWESWS|)W(W|S))|NNNENWNEE(SSESWW|N(WWW|EEEEE(S(S|W)|E)))))|SSSENNES(NWSSWNSENNES|)))|SSSSSWSW(N|SEE(S(SSS|W)|N))))|S))|S)|NNEEN(W|NN)))|SSSES(ENSW|)WWSEESWWS(WWWSNEEE|)E)|SSS(WNSE|)S))|W)|W))))|N)|N)))))|S)|S))|EE)))$

Part 1:
<Query Kind="Program">
  <Namespace>System.Drawing</Namespace>
</Query>

class Step { public int X,Y; public char Dir; public int P; public List<Step> NextSteps = new List<Step>(); }

int StartX;
int StartY;
int Width;
int Height;
bool[,] Doors;
bool[,] Reachable;
int?[,] Distance;
Step RootStep;

void Main()
{
	Load(File.ReadAllText(Path.Combine(Path.GetDirectoryName(Util.CurrentQueryPath), @"20_input.txt")).Trim());
	//DumpSteps();
	//int c=0; VerifySteps(RootStep, ref c);c.Dump();
	Walk();
	Flood().Dump();
	DumpMap();
}

void VerifySteps(Step s, ref int c)
{
	for(;;)
	{
		c++;
		if (s.NextSteps.Any(ns => ns.P>=s.P)) throw new Exception();
		if (s.NextSteps.Count == 1) { s = s.NextSteps.Single(); continue; }

		foreach (var ns in s.NextSteps) VerifySteps(ns, ref c);
		return;
	}
}

void DumpSteps()
{
	StringBuilder b = new StringBuilder();
	DumpSteps(RootStep, ref b, 0);
}

void DumpSteps(Step s, ref StringBuilder b, int w)
{
	int sscc = 0;
	for (; ; )
	{
		if (s.NextSteps.Count == 0) { b.Append(sscc.ToString()); w += sscc.ToString().Length; break; }
		if (s.NextSteps.Count == 1) { sscc++; s = s.NextSteps.Single(); continue; }

		{b.Append(sscc.ToString()); w += sscc.ToString().Length;}
		int l = w;
		foreach (var ns in s.NextSteps)
		{
			b.ToString().Dump();b.Clear();
			b.Append(new string(' ', l));
			DumpSteps(ns, ref b, w);
		}
		return;
	}
}

void Load(string d)
{
	d = d.Substring(1, d.Length - 2);

	StartX    = d.Count(c => c == 'W') + 16;
	StartY    = d.Count(c => c == 'N') + 16;
	Width     = d.Count(c => c == 'E') + 16 + StartX;
	Height    = d.Count(c => c == 'S') + 16 + StartY;
	Doors     = new bool[Width * 2 + 16, Height + 16];
	Distance  = new int?[Width, Height];
	Reachable = new bool[Width, Height];

	RootStep = Parse(d);
}

Step Parse(string d)
{
	Step r = null;
	Parse(new List<Step>(), ref d, ref r);
	if (d!="")throw new Exception();
	return r;
}

List<Step> Parse(List<Step> source, ref string d, ref Step root)
{
	List<Step> lasts = source;
	for (; ; )
	{
		if (d    == "") return lasts;
		if (d[0] == '|') return lasts;
		if (d[0] == ')') return lasts;
		if (d[0] == '(')
		{
			d = d.Substring(1);
			List<Step> newlasts = new List<Step>();
			for (;;)
			{
				if (d[0] == ')') { d = d.Substring(1); newlasts.AddRange(lasts); break; }
				
				var n = Parse(lasts, ref d, ref root);
				newlasts.AddRange(n);

				if (d[0] == '|') { d = d.Substring(1); continue; }
				if (d[0] == ')') { d = d.Substring(1); break;    }
				
				throw new Exception();
			}
			lasts = newlasts;
			continue;
		}

		if (d[0] == 'N') { var s = new Step { X = 00, Y = -1, Dir=d[0], P=d.Length, }; lasts.ForEach(l => l.NextSteps.Add(s)); lasts = new List<Step> {s}; d = d.Substring(1); root = root ?? s; continue; }
		if (d[0] == 'E') { var s = new Step { X = +1, Y = 00, Dir=d[0], P=d.Length, }; lasts.ForEach(l => l.NextSteps.Add(s)); lasts = new List<Step> {s}; d = d.Substring(1); root = root ?? s; continue; }
		if (d[0] == 'S') { var s = new Step { X = 00, Y = +1, Dir=d[0], P=d.Length, }; lasts.ForEach(l => l.NextSteps.Add(s)); lasts = new List<Step> {s}; d = d.Substring(1); root = root ?? s; continue; }
		if (d[0] == 'W') { var s = new Step { X = -1, Y = 00, Dir=d[0], P=d.Length, }; lasts.ForEach(l => l.NextSteps.Add(s)); lasts = new List<Step> {s}; d = d.Substring(1); root = root ?? s; continue; }

		throw new Exception();
	}
}

void Walk()
{
	Stack<(int x,int y, Step s)> work = new Stack<(int x, int y, Step s)>();
	work.Push((StartX, StartY, RootStep));
	
	while(work.Any())
	{
		(var x, var y, var step) = work.Pop();
		Reachable[x,y]=true;
		
		if (step.Dir == 'N') Doors[x*2, y]  = true;
		if (step.Dir == 'E') Doors[x*2+1,y] = true;
		if (step.Dir == 'S') Doors[x*2,y+1] = true;
		if (step.Dir == 'W') Doors[x*2-1,y] = true;

		x += step.X;
		y += step.Y;
		foreach (var ns in step.NextSteps) work.Push((x,y,ns));
		//DumpMap();
	}

}

int Flood()
{
	Stack<(int x, int y)> work = new Stack<(int x, int y)>();
	work.Push((StartX, StartY));
	Distance[StartX, StartY]=0;

	while (work.Any())
	{
		(var x, var y) = work.Pop();

		if (Doors[x * 2,     y    ] && (Distance[x+0,y-1] == null || Distance[x+0,y-1].Value > Distance[x,y]+1)) { Distance[x+0,y-1] = Distance[x,y]+1; work.Push((x+0,y-1)); }
		if (Doors[x * 2 + 1, y    ] && (Distance[x+1,y+0] == null || Distance[x+1,y+0].Value > Distance[x,y]+1)) { Distance[x+1,y+0] = Distance[x,y]+1; work.Push((x+1,y+0)); }
		if (Doors[x * 2,     y + 1] && (Distance[x+0,y+1] == null || Distance[x+0,y+1].Value > Distance[x,y]+1)) { Distance[x+0,y+1] = Distance[x,y]+1; work.Push((x+0,y+1)); }
		if (Doors[x * 2 - 1, y    ] && (Distance[x-1,y+0] == null || Distance[x-1,y+0].Value > Distance[x,y]+1)) { Distance[x-1,y+0] = Distance[x,y]+1; work.Push((x-1,y+0)); }
	}
	
	int max = int.MinValue;
	for (int xx = 0; xx < Width; xx++) for (int yy = 0; yy < Height; yy++) max = Math.Max(Distance[xx,yy] ?? max, max);
	return max;
}

void DumpMap()
{
	var ww = Width * 2 + 1;
	var hh = Height * 2 + 1;
	char[,] m = new char[Width * 2 + 1, Height * 2 + 1];

	int _sx = 0;
	int _ex = 0;
	int _sy = 0;
	int _ey = 0;
	for (int xx = 0; ; xx++)
	{
		var ff = false;
		for (int yy = 0; yy < Height; yy++) if (Reachable[xx, yy]) ff = true;
		if (ff) { _sx = xx; break; }
	}
	for (int xx = Width - 1; ; xx--)
	{
		var ff = false;
		for (int yy = 0; yy < Height; yy++) if (Reachable[xx, yy]) ff = true;
		if (ff) { _ex = xx; break; }
	}
	for (int yy = 0; ; yy++)
	{
		var ff = false;
		for (int xx = 0; xx < Width; xx++) if (Reachable[xx, yy]) ff = true;
		if (ff) { _sy = yy; break; }
	}
	for (int yy = Height - 1; ; yy--)
	{
		var ff = false;
		for (int xx = 0; xx < Width; xx++) if (Reachable[xx, yy]) ff = true;
		if (ff) { _ey = yy; break; }
	}

	var sy = _sy * 2 + 1 - 1;
	var sx = _sx * 2 + 1 - 1;
	var ey = _ey * 2 + 1 + 2;
	var ex = _ex * 2 + 1 + 2;
	

	for (int xx = sx; xx <= ex; xx++) for (int yy = sy; yy <= ey; yy++) m[xx, yy] = '?';

	for (int xx = sx; xx <= ex; xx += 2) for (int yy = sy; yy <= ey; yy++) m[xx, yy] = '#';
	for (int yy = sy; yy <= ey; yy += 2) for (int xx = sx; xx <= ex; xx++) m[xx, yy] = '#';
	
	for (int ix = _sx; ix <= _ex; ix++)
	{
		for (int iy = _sy; iy <= _ey; iy++)
		{
			if (Doors[ix * 2,     iy    ]) { m[ix*2+1, iy*2+1-1]='-'; }
			if (Doors[ix * 2 + 1, iy    ]) { m[ix*2+1+1, iy*2+1]='|'; }
			if (Doors[ix * 2,     iy + 1]) { m[ix*2+1, iy*2+1+1]='-'; }
			if (Doors[ix * 2 - 1, iy    ]) { m[ix*2+1-1, iy*2+1]='|'; }
			
			if (Distance[ix,iy]!= null) { m[ix*2+1, iy*2+1]='.'; }
		}
	}
	

	m[StartX * 2 + 1, StartY * 2 + 1] = 'X';

	int f = 6;
	Bitmap bmp = new Bitmap(((ex-sx)+(ex-sx)/2) * f, ((ey-sy)+(ey-sy)/2) * f, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
	using (Graphics g = Graphics.FromImage(bmp))
	{
		g.Clear(Color.Magenta);
		for (int yy = sy; yy < ey; yy++)
		{
			for (int xx = sx; xx < ex; xx++)
			{
				int rx = (xx - sx) + (xx - sx) / 2;
				int ry = (yy - sy) + (yy - sy) / 2;
				int rw = ((xx - sx) % 2) + 1;
				int rh = ((yy - sy) % 2) + 1;

				var c = Brushes.Magenta;
				if (m[xx, yy] == '#') c = Brushes.Black;
				if (m[xx, yy] == 'X') c = Brushes.Green;
				if (m[xx, yy] == '.') c = Brushes.White;
				if (m[xx, yy] == '?') c = Brushes.DarkGray;
				if (m[xx, yy] == '|') c = Brushes.LightGray;
				if (m[xx, yy] == '-') c = Brushes.LightGray;
				g.FillRectangle(c, rx*f, ry*f, rw*f, rh*f);
			}
		}
	}
	bmp.Dump();

	//StringBuilder b = new StringBuilder();
	//for (int yy = 0; yy < hh; yy++)
	//{
	//	for (int xx = 0; xx < ww; xx++)
	//	{
	//		b.Append(m[xx,yy]);
	//	}
	//	b.AppendLine();
	//}
	//b.ToString().Dump();
}
Result: 3675

Part 2:
<Query Kind="Program">
  <Namespace>System.Drawing</Namespace>
</Query>

class Step { public int X,Y; public char Dir; public int P; public List<Step> NextSteps = new List<Step>(); }

int StartX;
int StartY;
int Width;
int Height;
bool[,] Doors;
bool[,] Reachable;
int?[,] Distance;
Step RootStep;

void Main()
{
	Load(File.ReadAllText(Path.Combine(Path.GetDirectoryName(Util.CurrentQueryPath), @"20_input.txt")).Trim());
	//DumpSteps();
	//int c=0; VerifySteps(RootStep, ref c);c.Dump();
	Walk();
	Flood().Dump();
	DumpMap();
}

void VerifySteps(Step s, ref int c)
{
	for(;;)
	{
		c++;
		if (s.NextSteps.Any(ns => ns.P>=s.P)) throw new Exception();
		if (s.NextSteps.Count == 1) { s = s.NextSteps.Single(); continue; }

		foreach (var ns in s.NextSteps) VerifySteps(ns, ref c);
		return;
	}
}

void DumpSteps()
{
	StringBuilder b = new StringBuilder();
	DumpSteps(RootStep, ref b, 0);
}

void DumpSteps(Step s, ref StringBuilder b, int w)
{
	int sscc = 0;
	for (; ; )
	{
		if (s.NextSteps.Count == 0) { b.Append(sscc.ToString()); w += sscc.ToString().Length; break; }
		if (s.NextSteps.Count == 1) { sscc++; s = s.NextSteps.Single(); continue; }

		{b.Append(sscc.ToString()); w += sscc.ToString().Length;}
		int l = w;
		foreach (var ns in s.NextSteps)
		{
			b.ToString().Dump();b.Clear();
			b.Append(new string(' ', l));
			DumpSteps(ns, ref b, w);
		}
		return;
	}
}

void Load(string d)
{
	d = d.Substring(1, d.Length - 2);

	StartX    = d.Count(c => c == 'W') + 16;
	StartY    = d.Count(c => c == 'N') + 16;
	Width     = d.Count(c => c == 'E') + 16 + StartX;
	Height    = d.Count(c => c == 'S') + 16 + StartY;
	Doors     = new bool[Width * 2 + 16, Height + 16];
	Distance  = new int?[Width, Height];
	Reachable = new bool[Width, Height];

	RootStep = Parse(d);
}

Step Parse(string d)
{
	Step r = null;
	Parse(new List<Step>(), ref d, ref r);
	if (d!="")throw new Exception();
	return r;
}

List<Step> Parse(List<Step> source, ref string d, ref Step root)
{
	List<Step> lasts = source;
	for (; ; )
	{
		if (d    == "") return lasts;
		if (d[0] == '|') return lasts;
		if (d[0] == ')') return lasts;
		if (d[0] == '(')
		{
			d = d.Substring(1);
			List<Step> newlasts = new List<Step>();
			for (;;)
			{
				if (d[0] == ')') { d = d.Substring(1); newlasts.AddRange(lasts); break; }
				
				var n = Parse(lasts, ref d, ref root);
				newlasts.AddRange(n);

				if (d[0] == '|') { d = d.Substring(1); continue; }
				if (d[0] == ')') { d = d.Substring(1); break;    }
				
				throw new Exception();
			}
			lasts = newlasts;
			continue;
		}

		if (d[0] == 'N') { var s = new Step { X = 00, Y = -1, Dir=d[0], P=d.Length, }; lasts.ForEach(l => l.NextSteps.Add(s)); lasts = new List<Step> {s}; d = d.Substring(1); root = root ?? s; continue; }
		if (d[0] == 'E') { var s = new Step { X = +1, Y = 00, Dir=d[0], P=d.Length, }; lasts.ForEach(l => l.NextSteps.Add(s)); lasts = new List<Step> {s}; d = d.Substring(1); root = root ?? s; continue; }
		if (d[0] == 'S') { var s = new Step { X = 00, Y = +1, Dir=d[0], P=d.Length, }; lasts.ForEach(l => l.NextSteps.Add(s)); lasts = new List<Step> {s}; d = d.Substring(1); root = root ?? s; continue; }
		if (d[0] == 'W') { var s = new Step { X = -1, Y = 00, Dir=d[0], P=d.Length, }; lasts.ForEach(l => l.NextSteps.Add(s)); lasts = new List<Step> {s}; d = d.Substring(1); root = root ?? s; continue; }

		throw new Exception();
	}
}

void Walk()
{
	Stack<(int x,int y, Step s)> work = new Stack<(int x, int y, Step s)>();
	work.Push((StartX, StartY, RootStep));
	
	while(work.Any())
	{
		(var x, var y, var step) = work.Pop();
		Reachable[x,y]=true;
		
		if (step.Dir == 'N') Doors[x*2, y]  = true;
		if (step.Dir == 'E') Doors[x*2+1,y] = true;
		if (step.Dir == 'S') Doors[x*2,y+1] = true;
		if (step.Dir == 'W') Doors[x*2-1,y] = true;

		x += step.X;
		y += step.Y;
		foreach (var ns in step.NextSteps) work.Push((x,y,ns));
		//DumpMap();
	}

}

int Flood()
{
	Stack<(int x, int y)> work = new Stack<(int x, int y)>();
	work.Push((StartX, StartY));
	Distance[StartX, StartY]=0;

	while (work.Any())
	{
		(var x, var y) = work.Pop();

		if (Doors[x * 2,     y    ] && (Distance[x+0,y-1] == null || Distance[x+0,y-1].Value > Distance[x,y]+1)) { Distance[x+0,y-1] = Distance[x,y]+1; work.Push((x+0,y-1)); }
		if (Doors[x * 2 + 1, y    ] && (Distance[x+1,y+0] == null || Distance[x+1,y+0].Value > Distance[x,y]+1)) { Distance[x+1,y+0] = Distance[x,y]+1; work.Push((x+1,y+0)); }
		if (Doors[x * 2,     y + 1] && (Distance[x+0,y+1] == null || Distance[x+0,y+1].Value > Distance[x,y]+1)) { Distance[x+0,y+1] = Distance[x,y]+1; work.Push((x+0,y+1)); }
		if (Doors[x * 2 - 1, y    ] && (Distance[x-1,y+0] == null || Distance[x-1,y+0].Value > Distance[x,y]+1)) { Distance[x-1,y+0] = Distance[x,y]+1; work.Push((x-1,y+0)); }
	}
	
	int c = 0;
	for (int xx = 0; xx < Width; xx++) for (int yy = 0; yy < Height; yy++) if ((Distance[xx,yy] ?? 0) >= 1000)c++;
	return c;
}

void DumpMap()
{
	var ww = Width * 2 + 1;
	var hh = Height * 2 + 1;
	char[,] m = new char[Width * 2 + 1, Height * 2 + 1];

	int _sx = 0;
	int _ex = 0;
	int _sy = 0;
	int _ey = 0;
	for (int xx = 0; ; xx++)
	{
		var ff = false;
		for (int yy = 0; yy < Height; yy++) if (Reachable[xx, yy]) ff = true;
		if (ff) { _sx = xx; break; }
	}
	for (int xx = Width - 1; ; xx--)
	{
		var ff = false;
		for (int yy = 0; yy < Height; yy++) if (Reachable[xx, yy]) ff = true;
		if (ff) { _ex = xx; break; }
	}
	for (int yy = 0; ; yy++)
	{
		var ff = false;
		for (int xx = 0; xx < Width; xx++) if (Reachable[xx, yy]) ff = true;
		if (ff) { _sy = yy; break; }
	}
	for (int yy = Height - 1; ; yy--)
	{
		var ff = false;
		for (int xx = 0; xx < Width; xx++) if (Reachable[xx, yy]) ff = true;
		if (ff) { _ey = yy; break; }
	}

	var sy = _sy * 2 + 1 - 1;
	var sx = _sx * 2 + 1 - 1;
	var ey = _ey * 2 + 1 + 2;
	var ex = _ex * 2 + 1 + 2;
	

	for (int xx = sx; xx <= ex; xx++) for (int yy = sy; yy <= ey; yy++) m[xx, yy] = '?';

	for (int xx = sx; xx <= ex; xx += 2) for (int yy = sy; yy <= ey; yy++) m[xx, yy] = '#';
	for (int yy = sy; yy <= ey; yy += 2) for (int xx = sx; xx <= ex; xx++) m[xx, yy] = '#';
	
	for (int ix = _sx; ix <= _ex; ix++)
	{
		for (int iy = _sy; iy <= _ey; iy++)
		{
			if (Doors[ix * 2,     iy    ]) { m[ix*2+1, iy*2+1-1]='-'; }
			if (Doors[ix * 2 + 1, iy    ]) { m[ix*2+1+1, iy*2+1]='|'; }
			if (Doors[ix * 2,     iy + 1]) { m[ix*2+1, iy*2+1+1]='-'; }
			if (Doors[ix * 2 - 1, iy    ]) { m[ix*2+1-1, iy*2+1]='|'; }
			
			if (Distance[ix,iy]!= null) { m[ix*2+1, iy*2+1]='.'; }
		}
	}
	

	m[StartX * 2 + 1, StartY * 2 + 1] = 'X';

	int f = 6;
	Bitmap bmp = new Bitmap(((ex-sx)+(ex-sx)/2) * f, ((ey-sy)+(ey-sy)/2) * f, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
	using (Graphics g = Graphics.FromImage(bmp))
	{
		g.Clear(Color.Magenta);
		for (int yy = sy; yy < ey; yy++)
		{
			for (int xx = sx; xx < ex; xx++)
			{
				int rx = (xx - sx) + (xx - sx) / 2;
				int ry = (yy - sy) + (yy - sy) / 2;
				int rw = ((xx - sx) % 2) + 1;
				int rh = ((yy - sy) % 2) + 1;

				var c = Brushes.Magenta;
				if (m[xx, yy] == '#') c = Brushes.Black;
				if (m[xx, yy] == 'X') c = Brushes.Green;
				if (m[xx, yy] == '.') c = Brushes.White;
				if (m[xx, yy] == '?') c = Brushes.DarkGray;
				if (m[xx, yy] == '|') c = Brushes.LightGray;
				if (m[xx, yy] == '-') c = Brushes.LightGray;
				g.FillRectangle(c, rx*f, ry*f, rw*f, rh*f);
			}
		}
	}
	bmp.Dump();

	//StringBuilder b = new StringBuilder();
	//for (int yy = 0; yy < hh; yy++)
	//{
	//	for (int xx = 0; xx < ww; xx++)
	//	{
	//		b.Append(m[xx,yy]);
	//	}
	//	b.AppendLine();
	//}
	//b.ToString().Dump();
}
Result: 7953


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