-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path10 - Pipe Maze.py
146 lines (118 loc) · 4.26 KB
/
10 - Pipe Maze.py
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from modules import DataManager
data = DataManager(__file__).get_data_string()
# Testdata -------------------------------------------------------------------------------------
test_data_string = """
F-7-
|-S7
|F.|
L7FJ
FLJ.
"""
test_data = test_data_string.strip().split("\n")
expected = [7, 6725, 3, 383]
# Shared ---------------------------------------------------------------------------------------
directions = [[-1, 0], [0, 1], [1, 0], [0, -1]]
pipes = {
"S": [[-1, 0], [0, 1]], # North, East - just my test data
"|": [[-1, 0], [1, 0]], # North, South
"-": [[0, 1], [0, -1]], # East, West
"L": [[-1, 0], [0, 1]], # North, East
"J": [[-1, 0], [0, -1]], # North, West
"7": [[1, 0], [0, -1]], # South, West
"F": [[0, 1], [1, 0]], # East, South
}
def get_pos(pos, move):
p1 = pos[0] + move[0]
p2 = pos[1] + move[1]
return [p1, p2]
def get_start(grid):
for i, row in enumerate(grid):
for j, cell in enumerate(row):
if cell == "S":
return [i, j]
# Part 1 ---------------------------------------------------------------------------------------
# Only handles my specific input (not even test data), since I didn't bother to figure out the start
def part1(input=data) -> int:
grid = [list(line) for line in input]
start = get_start(grid)
pos1 = start.copy()
pos2 = start.copy()
move1 = [[-1, 0], [0, 1]] # My "S" is going N-E
move2 = [[-1, 0], [0, 1]]
last_move1 = [1, 0] # Inverse of move1
last_move2 = [0, -1] # Inverse of move2
steps = 0
while True:
steps += 1
i = 1 if get_pos(move1[0], last_move1) == [0, 0] else 0
pos1 = get_pos(pos1, move1[i])
if pos1 == pos2:
return steps
last_move1 = move1[i]
i = 1 if get_pos(move2[0], last_move2) == [0, 0] else 0
pos2 = get_pos(pos2, move2[i])
if pos1 == pos2:
return steps
last_move2 = move2[i]
move1 = pipes[grid[pos1[0]][pos1[1]]]
move2 = pipes[grid[pos2[0]][pos2[1]]]
# Part 2 ---------------------------------------------------------------------------------------
# Only handles my specific input (not even test data), since I didn't bother to figure out the start
def part2(input=data) -> int:
grid = [list(line) for line in input]
start = get_start(grid)
pos1 = start.copy()
pos2 = start.copy()
move1 = [[-1, 0], [0, 1]] # My "S" is going N-E
move2 = [[-1, 0], [0, 1]]
last_move1 = [1, 0] # Inverse of move1
last_move2 = [0, -1] # Inverse of move2
visited = [start.copy()]
while True:
i = 1 if get_pos(move1[0], last_move1) == [0, 0] else 0
pos1 = get_pos(pos1, move1[i])
if pos1 == pos2:
break
visited += [pos1.copy()]
last_move1 = move1[i]
i = 1 if get_pos(move2[0], last_move2) == [0, 0] else 0
pos2 = get_pos(pos2, move2[i])
if pos1 == pos2:
break
visited += [pos2.copy()]
last_move2 = move2[i]
move1 = pipes[grid[pos1[0]][pos1[1]]]
move2 = pipes[grid[pos2[0]][pos2[1]]]
size = 0
for cell in range(len(grid[0])):
inside = False
for row in range(len(grid)):
if [row, cell] in visited:
pos = grid[row][cell]
match pos:
case "-":
inside = not inside
case "7":
l_switches = True
case "F":
l_switches = False
case "L" | "S": # "S" == "L" just in my data
if l_switches:
inside = not inside
case "J":
if not l_switches:
inside = not inside
elif inside:
size += 1
return size
# Output ---------------------------------------------------------------------------------------
results = [
("Test Part 1:", part1(test_data), expected[0]),
("Part 1:", part1(), expected[1]),
("Test Part 2:", part2(test_data), expected[2]),
("Part 2:", part2(), expected[3]),
]
for result in results:
print(result[0], result[1])
if result[2] and result[1] != result[2]:
print("Expected:", result[2])