2018 - Day 13 (Mine Cart Madness)
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
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env python3
#Advent of Code
#2018 Day 13, 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):
tracks = []
carts = []
class Cart:
forward_slash = {"^": ">",
"v": "<",
"<": "v",
">": "^"}
backward_slash = {"^": "<",
"v": ">",
"<": "^",
">": "v"}
turn_left = {"^": "<",
"v": ">",
"<": "v",
">": "^"}
turn_right = {"^": ">",
"v": "<",
"<": "^",
">": "v"}
def __init__(self, x, y, direction):
self.x = x
self.y = y
self.direction = direction
self.next_turn = "left"
self.cartID = len(carts)
def __lt__(self, other):
if self.y < other.y:
return True
elif self.y > other.y:
return False
else:
if self.x < other.x:
return True
else:
return False
def step(self):
if self.direction == "^":
self.y -= 1
elif self.direction == "v":
self.y += 1
elif self.direction == "<":
self.x -= 1
else:
self.x += 1
if tracks[self.y][self.x] == "/":
self.direction = Cart.forward_slash[self.direction]
elif tracks[self.y][self.x] == "\\":
self.direction = Cart.backward_slash[self.direction]
elif tracks[self.y][self.x] == "+":
if self.next_turn == "left":
self.direction = Cart.turn_left[self.direction]
self.next_turn = "straight"
elif self.next_turn == "straight":
self.next_turn = "right"
elif self.next_turn == "right":
self.direction = Cart.turn_right[self.direction]
self.next_turn = "left"
for cart in carts:
if cart.cartID != self.cartID and cart.x == self.x and cart.y == self.y:
return (True, f"{self.x},{self.y}")
return (False, f"{self.x},{self.y}")
for y, line in enumerate(puzzle_input):
tracks.append("")
for x, c in enumerate(line):
if c in "v^":
carts.append(Cart(x, y, c))
tracks[-1] += "|"
elif c in "<>":
carts.append(Cart(x, y, c))
tracks[-1] += "-"
else:
tracks[-1] += c
while True:
carts.sort()
for cart in carts:
collision, coords = cart.step()
if collision:
return coords
def main():
puzzle_input = util.read.as_lines_sans_ending_line()
coord = solve(puzzle_input)
print("The location of the first crash is " + str(coord) + ".")
class AOC_Tests(unittest.TestCase):
def test_ex1(self):
self.assertEqual(solve(["/->-\\ ",
"| | /----\\",
"| /-+--+-\\ |",
"| | | | v |",
"\\-+-/ \\-+--/",
" \\------/ "]), "7,3")
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python3
#Advent of Code
#2018 Day 13, 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
def solve(puzzle_input):
tracks = []
carts = []
class Cart:
forward_slash = {"^": ">",
"v": "<",
"<": "v",
">": "^"}
backward_slash = {"^": "<",
"v": ">",
"<": "^",
">": "v"}
turn_left = {"^": "<",
"v": ">",
"<": "v",
">": "^"}
turn_right = {"^": ">",
"v": "<",
"<": "^",
">": "v"}
def __init__(self, x, y, direction):
self.x = x
self.y = y
self.direction = direction
self.next_turn = "left"
self.cartID = len(carts)
self.active = 1
def __lt__(self, other):
if self.y < other.y:
return True
elif self.y > other.y:
return False
else:
if self.x < other.x:
return True
else:
return False
def step(self):
if self.active == 0:
return
if self.direction == "^":
self.y -= 1
elif self.direction == "v":
self.y += 1
elif self.direction == "<":
self.x -= 1
else:
self.x += 1
if tracks[self.y][self.x] == "/":
self.direction = Cart.forward_slash[self.direction]
elif tracks[self.y][self.x] == "\\":
self.direction = Cart.backward_slash[self.direction]
elif tracks[self.y][self.x] == "+":
if self.next_turn == "left":
self.direction = Cart.turn_left[self.direction]
self.next_turn = "straight"
elif self.next_turn == "straight":
self.next_turn = "right"
elif self.next_turn == "right":
self.direction = Cart.turn_right[self.direction]
self.next_turn = "left"
for cart in carts:
if cart.cartID != self.cartID and cart.active == 1 and cart.x == self.x and cart.y == self.y:
self.active = 0
cart.active = 0
for y, line in enumerate(puzzle_input):
tracks.append("")
for x, c in enumerate(line):
if c in "v^":
carts.append(Cart(x, y, c))
tracks[-1] += "|"
elif c in "<>":
carts.append(Cart(x, y, c))
tracks[-1] += "-"
else:
tracks[-1] += c
while True:
carts.sort()
for cart in carts:
cart.step()
if sum(c.active for c in carts) == 1:
for cart in carts:
if cart.active == 1:
return f"{cart.x},{cart.y}"
def main():
puzzle_input = util.read.as_lines_sans_ending_line()
coord = solve(puzzle_input)
print("The location of final remaining cart is " + str(coord) + ".")
class AOC_Tests(unittest.TestCase):
def test_ex1(self):
self.assertEqual(solve(["/>-<\\ ",
"| | ",
"| /<+-\\",
"| | | v",
"\\>+</ |",
" | ^",
" \\<->/"]), "6,4")
if __name__ == "__main__":
run(main)