2018 - Day 22 (Mode Maze)
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
#!/usr/bin/env python3
#Advent of Code
#2018 Day 22, 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
def solve(puzzle_input):
depth = int(puzzle_input[0].split()[1])
target_x, target_y = [int(x) for x in puzzle_input[1].split()[1].split(",")]
g_indices = {}
g_indices["0,0"] = 0
g_indices[f"{target_x},{target_y}"] = 0
def geologic_index(x, y):
if f"{x},{y}" in g_indices:
return g_indices[f"{x},{y}"]
elif y == 0:
g_indices[f"{x},{y}"] = x * 16807
return g_indices[f"{x},{y}"]
elif x == 0:
g_indices[f"{x},{y}"] = y * 48271
return g_indices[f"{x},{y}"]
else:
g_indices[f"{x},{y}"] = erosion_level(x-1, y) * erosion_level(x, y-1)
return g_indices[f"{x},{y}"]
def erosion_level(x, y):
return (geologic_index(x, y) + depth) % 20183
def coord_type(x, y):
return ["rocky", "wet", "narrow"][erosion_level(x, y) % 3]
def risk_level(x, y):
return {"rocky": 0, "wet": 1, "narrow": 2}[coord_type(x, y)]
return sum(risk_level(x, y) for x in range(0, target_x+1) for y in range(0, target_y+1))
def main():
puzzle_input = util.read.as_lines()
risk_level_sum = solve(puzzle_input)
print("The total risk level for the smallest rectangle that includes 0,0 and the target is " + str(risk_level_sum) + ".")
class AOC_Tests(unittest.TestCase):
def test_ex1(self):
self.assertEqual(solve(["depth: 510",
"target: 10,10"]), 114)
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
#!/usr/bin/env python3
#Advent of Code
#2018 Day 22, Part 2
#Solution by /u/korylprince (https://www.reddit.com/r/adventofcode/comments/a8i1cy/2018_day_22_solutions/ecazvbe/)
#Implementation by James C. (https://github.com/JamesMCo)
#Damnit, I really need to look into how to use networkx myself.
import os, sys
sys.path.append(os.path.abspath("../.."))
import unittest, util.read
from util.tests import run
import networkx as nx
def solve(puzzle_input):
depth = int(puzzle_input[0].split()[1])
target_x, target_y = [int(x) for x in puzzle_input[1].split()[1].split(",")]
g_indices = {}
g_indices["0,0"] = 0
g_indices[f"{target_x},{target_y}"] = 0
coord_types = {}
def geologic_index(x, y):
if f"{x},{y}" in g_indices:
return g_indices[f"{x},{y}"]
elif y == 0:
g_indices[f"{x},{y}"] = x * 16807
return g_indices[f"{x},{y}"]
elif x == 0:
g_indices[f"{x},{y}"] = y * 48271
return g_indices[f"{x},{y}"]
else:
g_indices[f"{x},{y}"] = erosion_level(x-1, y) * erosion_level(x, y-1)
return g_indices[f"{x},{y}"]
def erosion_level(x, y):
return (geologic_index(x, y) + depth) % 20183
def coord_type(x, y):
if f"{x},{y}" in coord_types:
return coord_types[f"{x},{y}"]
else:
coord_types[f"{x},{y}"] = ["rocky", "wet", "narrow"][erosion_level(x, y) % 3]
return coord_types[f"{x},{y}"]
def shortest_path():
g = nx.Graph()
for x in range(0, target_x+100):
for y in range(0, target_y+100):
items = {"rocky": ["climb", "torch"],
"wet": ["climb", "empty"],
"narrow": ["torch", "empty"]}[coord_type(x, y)]
g.add_edge(f"{x},{y},{items[0]}", f"{x},{y},{items[1]}", weight=7)
for offset in [(1, 0), (-1, 0), (0, 1), (0, -1)]:
if 0 <= x+offset[0] and 0 <= y+offset[1]:
neighbour_items = {"rocky": ["climb", "torch"],
"wet": ["climb", "empty"],
"narrow": ["torch", "empty"]}[coord_type(x+offset[0], y+offset[1])]
for item in set(items).intersection(set(neighbour_items)):
g.add_edge(f"{x},{y},{item}", f"{x+offset[0]},{y+offset[1]},{item}", weight=1)
return nx.dijkstra_path_length(g, "0,0,torch", f"{target_x},{target_y},torch")
return shortest_path()
def main():
puzzle_input = util.read.as_lines()
minutes = solve(puzzle_input)
print("The fewest number of minutes you can take to reach the target is " + str(minutes) + ".")
class AOC_Tests(unittest.TestCase):
def test_ex1(self):
self.assertEqual(solve(["depth: 510",
"target: 10,10"]), 45)
if __name__ == "__main__":
run(main)