This repository was archived by the owner on Sep 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathj5-escape-room.py
More file actions
71 lines (53 loc) · 1.66 KB
/
j5-escape-room.py
File metadata and controls
71 lines (53 loc) · 1.66 KB
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
# I DID NOT MANAGE TO COMPLETE THIS PROBLEM, THIS CODE IS FROM https://github.com/Ewpratten/ccc-2020
def findJumpsForVal(grid: list, val: int) -> list:
# Find all xy combinations that multiply to val
for r in range(len(grid)):
for c in range(len(grid[0])):
# Check if it is a factor
if (r+1)*(c+1) == val:
yield (r, c)
# Read the number of lines
line_count = int(input(""))
# We don't need the second input
input("")
# Read all rows of the grid
grid: list = []
while len(grid) < line_count:
# Add row to the grid
grid.append([int(i) for i in input("").strip().split(" ")])
# Define a tasks queue for jumps and tracker for visited points
tasks: list = []
visited: list = []
# Push the first position to the tasks list
tasks.append({
"x": 0,
"y": 0,
"value": grid[0][0]
})
# Handle tasks running
while len(tasks) > 0:
# Pop the first task off the stack
task: dict
try:
task = tasks.pop(0)
except IndexError as e:
break
# Mark this as a visited position
visited.append((task["x"], task["y"]))
# Check all jumps
for jump in findJumpsForVal(grid, task["value"]):
# Jump not valid if we have been there before
if jump in visited:
continue
# If the jump is the end, we have finished
if jump == (len(grid)-1, len(grid[0])-1):
print("yes")
exit(0)
# Otherwise, add the jump to the list of points
tasks.append({
"x": jump[0],
"y": jump[1],
"value": grid[jump[0]][jump[1]]
})
# If tasks runs out, we can not finish the room
print("no")