-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10_solution.py
109 lines (86 loc) · 3.12 KB
/
day10_solution.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
import pandas as pd
day = 10
input_fn = f"day{day:02d}_input.txt"
with open(input_fn) as f:
data = f.readlines()
moves = {
"|": ["north", "south"],
"-": ["east", "west"],
"L": ["north", "east"],
"J": ["north", "west"],
"7": ["south", "west"],
"F": ["south", "east"],
}
opposite_map = {
"north": "south",
"east": "west",
"south": "north",
"west": "east",
}
loop_df = pd.DataFrame({
"symbol": pd.Series(dtype="object"),
"row": pd.Series(dtype="int64"),
"column": pd.Series(dtype="int64"),
"move_direction": pd.Series(dtype="string")
})
##################################################
# Part 1
##################################################
# Searches grid for S
def find_S(data):
for i in range(len(data)):
for j in range(len(data[i])):
if data[i][j] == "S":
return (i, j)
# Look around S to see which adjacent blocks go into S
def move(data, row, column, move_direction):
if move_direction == "north":
return data[row-1][column], row - 1, column
elif move_direction == "east":
return data[row][column+1], row, column + 1
elif move_direction == "south":
return data[row+1][column], row + 1, column
elif move_direction == "west":
return data[row][column-1], row, column - 1
# Step 1: Find S
symbol = "S"
row, column = find_S(data)
step_number = 0
move_direction = "" # Currently, unknown
loop_df.loc[step_number] = [symbol, row, column, ""]
# Step 2: Determine direction to move from S
for move_direction in ["north", "east", "south", "west"]:
temp_symbol, temp_row, temp_column = move(data, row, column, move_direction)
if opposite_map[move_direction] in moves[temp_symbol]:
loop_df.loc[step_number, "move_direction"] = move_direction
opposite_direction = opposite_map[move_direction]
symbol = temp_symbol
row = temp_row
column = temp_column
step_number += 1
# print(symbol, row, column, step_number)
loop_df.loc[step_number, ["symbol", "row", "column", "move_direction"]] = [symbol, row, column, ""]
break
# Step 3: Based on the current symbol and the last move_direction, continue on path until we get back to S
while symbol != "S":
# print(opposite_direction)
# Each symbol has two possible moves. We came from the opposite of move_direction and should continue to the other direction.
if moves[symbol][0] == opposite_direction:
move_direction = moves[symbol][1]
else:
move_direction = moves[symbol][0]
opposite_direction = opposite_map[move_direction]
# print(move_direction)
loop_df.loc[step_number, "move_direction"] = move_direction
symbol, row, column = move(data, row, column, move_direction)
step_number += 1
# print(symbol, row, column, step_number)
loop_df.loc[step_number, ["symbol", "row", "column", "move_direction"]] = [symbol, row, column, ""]
# print(loop_df)
#
print(f"Part 1 answer: {loop_df.index[-1] // 2}")
##################################################
# Part 2
##################################################
#
print(f"Part 2 answer: {}")