2018 - Day 20 (A Regular Map)

View puzzle description on AdventOfCode.com
Part 1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python3

#Advent of Code
#2018 Day 20, Part 1
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

from collections import defaultdict
from math import inf

def solve(puzzle_input):
    grid = defaultdict(lambda: "#")
    branches = []
    x, y = 0, 0
    min_x, max_x = inf, -inf
    min_y, max_y = inf, -inf

    grid["0,0"] = "X"
    for char in puzzle_input[1:-1]:
        if char == "(":
            branches.append((x, y))
        elif char == "|":
            x, y = branches[-1]
        elif char == ")":
            x, y = branches.pop()
        elif char == "N":
            y -= 2
            grid[f"{x},{y}"]   = "."
            grid[f"{x},{y+1}"] = "-"
        elif char == "S":
            y += 2
            grid[f"{x},{y}"]   = "."
            grid[f"{x},{y-1}"] = "-"
        elif char == "E":
            x += 2
            grid[f"{x},{y}"]   = "."
            grid[f"{x-1},{y}"] = "|"
        elif char == "W":
            x -= 2
            grid[f"{x},{y}"]   = "."
            grid[f"{x+1},{y}"] = "|"

        min_x = min(min_x, x-1)
        max_x = max(max_x, x+1)
        min_y = min(min_y, y-1)
        max_y = max(max_y, y+1)

    distances = defaultdict(lambda: None)
    distances["0,0"] = 0
    changed = True
    while changed:
        changed = False
        for y in range(min_y, max_y+1):
            for x in range(min_x, max_x+1):
                if grid[f"{x},{y}"] == "." and distances[f"{x},{y}"] == None:
                    potential = []
                    if distances[f"{x},{y-2}"] != None and grid[f"{x},{y-1}"] == "-":
                        potential.append(distances[f"{x},{y-2}"] + 1)
                    if distances[f"{x},{y+2}"] != None and grid[f"{x},{y+1}"] == "-":
                        potential.append(distances[f"{x},{y+2}"] + 1)
                    if distances[f"{x+2},{y}"] != None and grid[f"{x+1},{y}"] == "|":
                        potential.append(distances[f"{x+2},{y}"] + 1)
                    if distances[f"{x-2},{y}"] != None and grid[f"{x-1},{y}"] == "|":
                        potential.append(distances[f"{x-2},{y}"] + 1)

                    if len(potential) > 0:
                        distances[f"{x},{y}"] = max(potential)
                        changed = True

    furthest = 0
    for y in range(min_y, max_y+1):
        for x in range(min_x, max_x+1):
            if distances[f"{x},{y}"] != None and distances[f"{x},{y}"] > furthest:
                furthest = distances[f"{x},{y}"]
    return furthest

def main():
    puzzle_input = util.read.as_string()

    doors = solve(puzzle_input)

    print("The largest number of doors required to reach a room is " + str(doors) + ".")

class AOC_Tests(unittest.TestCase):
    def test_ex1(self):
        self.assertEqual(solve("^WNE$"), 3)

    def test_ex2(self):
        self.assertEqual(solve("^ENWWW(NEEE|SSE(EE|N))$"), 10)

    def test_ex3(self):
        self.assertEqual(solve("^ENNWSWW(NEWS|)SSSEEN(WNSE|)EE(SWEN|)NNN$"), 18)

    def test_ex4(self):
        self.assertEqual(solve("^ESSWWN(E|NNENN(EESS(WNSE|)SSS|WWWSSSSE(SW|NNNE)))$"), 23)

    def test_ex5(self):
        self.assertEqual(solve("^WSSEESWWWNW(S|NENNEEEENN(ESSSSW(NWSW|SSEN)|WSWWN(E|WWS(E|SS))))$"), 31)

if __name__ == "__main__":
    run(main)
Part 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python3

#Advent of Code
#2018 Day 20, Part 2
#Solution by James C. (https://github.com/JamesMCo)

import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run

from collections import defaultdict
from math import inf

def solve(puzzle_input):
    grid = defaultdict(lambda: "#")
    branches = []
    x, y = 0, 0
    min_x, max_x = inf, -inf
    min_y, max_y = inf, -inf

    grid["0,0"] = "X"
    for char in puzzle_input[1:-1]:
        if char == "(":
            branches.append((x, y))
        elif char == "|":
            x, y = branches[-1]
        elif char == ")":
            x, y = branches.pop()
        elif char == "N":
            y -= 2
            grid[f"{x},{y}"]   = "."
            grid[f"{x},{y+1}"] = "-"
        elif char == "S":
            y += 2
            grid[f"{x},{y}"]   = "."
            grid[f"{x},{y-1}"] = "-"
        elif char == "E":
            x += 2
            grid[f"{x},{y}"]   = "."
            grid[f"{x-1},{y}"] = "|"
        elif char == "W":
            x -= 2
            grid[f"{x},{y}"]   = "."
            grid[f"{x+1},{y}"] = "|"

        min_x = min(min_x, x-1)
        max_x = max(max_x, x+1)
        min_y = min(min_y, y-1)
        max_y = max(max_y, y+1)

    distances = defaultdict(lambda: None)
    distances["0,0"] = 0
    changed = True
    while changed:
        changed = False
        for y in range(min_y, max_y+1):
            for x in range(min_x, max_x+1):
                if grid[f"{x},{y}"] == "." and distances[f"{x},{y}"] == None:
                    potential = []
                    if distances[f"{x},{y-2}"] != None and grid[f"{x},{y-1}"] == "-":
                        potential.append(distances[f"{x},{y-2}"] + 1)
                    if distances[f"{x},{y+2}"] != None and grid[f"{x},{y+1}"] == "-":
                        potential.append(distances[f"{x},{y+2}"] + 1)
                    if distances[f"{x+2},{y}"] != None and grid[f"{x+1},{y}"] == "|":
                        potential.append(distances[f"{x+2},{y}"] + 1)
                    if distances[f"{x-2},{y}"] != None and grid[f"{x-1},{y}"] == "|":
                        potential.append(distances[f"{x-2},{y}"] + 1)

                    if len(potential) > 0:
                        distances[f"{x},{y}"] = max(potential)
                        changed = True

    count = 0
    for y in range(min_y, max_y+1):
        for x in range(min_x, max_x+1):
            if distances[f"{x},{y}"] != None and distances[f"{x},{y}"] >= 1000:
                count += 1
    return count

def main():
    puzzle_input = util.read.as_string()

    rooms = solve(puzzle_input)

    print("The number of rooms that have a shortest path of at least 1000 doors is " + str(rooms) + ".")

class AOC_Tests(unittest.TestCase):
    @unittest.skip("No test cases")
    def test_ex1(self):
        pass

if __name__ == "__main__":
    run(main)
Return to solution index