Skip to content

Commit 57bcc01

Browse files
committed
Added day 2018-20
1 parent ea5df0a commit 57bcc01

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

2018/20-A Regular Map.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
# -------------------------------- Input data ---------------------------------------- #
2+
import os
3+
4+
test_data = {}
5+
6+
test = 1
7+
test_data[test] = {
8+
"input": """^WNE$""",
9+
"expected": ["3", "Unknown"],
10+
}
11+
12+
test += 1
13+
test_data[test] = {
14+
"input": """^W(N|W)E$""",
15+
"expected": ["3", "Unknown"],
16+
}
17+
18+
test += 1
19+
test_data[test] = {
20+
"input": """^ENWWW(NEEE|SSE(EE|N))$""",
21+
"expected": ["10", "Unknown"],
22+
}
23+
24+
test += 1
25+
test_data[test] = {
26+
"input": """^ENNWSWW(NEWS|)SSSEEN(WNSE|)EE(SWEN|)NNN$""",
27+
"expected": ["18", "Unknown"],
28+
}
29+
30+
test += 1
31+
test_data[test] = {
32+
"input": """^ESSWWN(E|NNENN(EESS(WNSE|)SSS|WWWSSSSE(SW|NNNE)))$""",
33+
"expected": ["23", "Unknown"],
34+
}
35+
36+
test += 1
37+
test_data[test] = {
38+
"input": """^WSSEESWWWNW(S|NENNEEEENN(ESSSSW(NWSW|SSEN)|WSWWN(E|WWS(E|SS))))$""",
39+
"expected": ["31", "Unknown"],
40+
}
41+
42+
test = "real"
43+
input_file = os.path.join(
44+
os.path.dirname(__file__),
45+
"Inputs",
46+
os.path.basename(__file__).replace(".py", ".txt"),
47+
)
48+
test_data[test] = {
49+
"input": open(input_file, "r+").read().strip(),
50+
"expected": ["3207", "8361"],
51+
}
52+
53+
# -------------------------------- Control program execution ------------------------- #
54+
55+
case_to_test = "real"
56+
part_to_test = 2
57+
58+
# -------------------------------- Initialize some variables ------------------------- #
59+
60+
puzzle_input = test_data[case_to_test]["input"]
61+
puzzle_expected_result = test_data[case_to_test]["expected"][part_to_test - 1]
62+
puzzle_actual_result = "Unknown"
63+
64+
65+
# -------------------------------- Actual code execution ----------------------------- #
66+
67+
68+
forks = [{0: 0}]
69+
70+
positions = {0: 0}
71+
dir = {"N": -1j, "S": 1j, "E": 1, "W": -1}
72+
movement = 0
73+
length = 0
74+
75+
all_positions = tuple()
76+
positions_below_1000 = tuple()
77+
for letter in puzzle_input[1:-1]:
78+
if letter in "NSEW":
79+
# Move !
80+
positions = {pos + dir[letter]: positions[pos] + 1 for pos in positions}
81+
positions_below_1000 += tuple(x for x in positions if positions[x] < 1000)
82+
all_positions += tuple(x for x in positions)
83+
elif letter == "(":
84+
# Put current positions in the queue (= start of fork)
85+
forks.append(positions)
86+
# Initiate the "last fork targets" that'll get updated later
87+
forks.append({})
88+
elif letter == "|":
89+
# Update the "last fork targets" (forks[-1]), then reset to forks[-2]
90+
forks[-1] = {
91+
pos: min(forks[-1][pos], positions.get(pos, 10 ** 6)) for pos in forks[-1]
92+
}
93+
forks[-1].update(
94+
{pos: positions[pos] for pos in positions if pos not in forks[-1]}
95+
)
96+
positions = forks[-2]
97+
elif letter == ")":
98+
# Merge the current positions, the last fork targets (forks[-1]) and the positions before forking (forks[-2])
99+
positions.update(
100+
{pos: min(forks[-1][pos], positions.get(pos, 10 ** 6)) for pos in forks[-1]}
101+
)
102+
positions.update(
103+
{pos: min(forks[-2][pos], positions.get(pos, 10 ** 6)) for pos in forks[-2]}
104+
)
105+
# Then go back to before the forking
106+
forks.pop()
107+
forks.pop()
108+
109+
# Merge all forks with the most recent positions
110+
for fork in forks:
111+
positions.update({pos: min(fork[pos], positions.get(pos, 10 ** 6)) for pos in fork})
112+
113+
114+
if part_to_test == 1:
115+
puzzle_actual_result = max(positions.values())
116+
117+
else:
118+
puzzle_actual_result = len(set(all_positions)) - len(set(positions_below_1000))
119+
120+
121+
# -------------------------------- Outputs / results --------------------------------- #
122+
123+
print("Expected result : " + str(puzzle_expected_result))
124+
print("Actual result : " + str(puzzle_actual_result))

0 commit comments

Comments
 (0)