-
Notifications
You must be signed in to change notification settings - Fork 0
/
malware1.py
113 lines (100 loc) · 4.34 KB
/
malware1.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
def main():
# Input
n, m = list(map(int, input().split()))
grid = []
for i in range(n):
row = list(input())
grid.append(row)
moves = []
for move in range(m):
sample_id, direction, rep = input().split()
moves.append((sample_id, direction, rep))
# @parameters - grid, each move
# @return - type of the error
errors = []
for move in moves:
errors.append(checkError(move, grid))
# Print the results
print(*errors, sep = '\n')
for row in grid:
print(*row, sep = "")
def checkError(move, grid):
# 1: error: invalid number of repetitions (rep < 1)
if int(move[2]) < 1: return "1"
# 2: error: sample_id does not appear on the board
position = exist(move[0], grid)
if position == False: return "2"
else:
return moveSample(grid, position, move)
def exist(sample_id, grid):
e = False
for row in range(len(grid)):
for coloumn in range(len(grid[row])):
if grid[row][coloumn] == sample_id:
return [row, coloumn]
return e
def moveSample(grid, position, move):
initial_position = tuple(position)
# move = sample_ id, dir, rep
if move[1] == "U":
for i in range(int(move[2])):
if position[0]-1 >= 0:
if grid[position[0]-1][position[1]] == ".":
grid[position[0]-1][position[1]] = grid[position[0]][position[1]]
grid[position[0]][position[1]] = "."
position = [position[0]-1, position[1]]
else: # 3: error: attempt to move the sample over a non-empty location
grid[position[0]][position[1]] = "."
grid[initial_position[0]][initial_position[1]] = move[0]
return "3"
else: # 4: error: attempt to move the sample past the board’s boundaries
grid[initial_position[0]][initial_position[1]] = move[0]
grid[position[0]][position[1]] = "."
return "4"
elif move[1] == "D":
for i in range(int(move[2])):
if position[0]+1 < len(grid):
if grid[position[0]+1][position[1]] == ".":
grid[position[0]+1][position[1]] = grid[position[0]][position[1]]
grid[position[0]][position[1]] = "."
position = [position[0]+1, position[1]]
else:
grid[position[0]][position[1]] = "."
grid[initial_position[0]][initial_position[1]] = move[0]
return "3"
else:
grid[position[0]][position[1]] = "."
grid[initial_position[0]][initial_position[1]] = move[0]
return "4"
elif move[1] == "R":
for i in range(int(move[2])):
if position[1]+1 < len(grid):
if grid[position[0]][position[1]+1] == ".":
grid[position[0]][position[1]+1] = grid[position[0]][position[1]]
grid[position[0]][position[1]] = "."
position = [position[0], position[1]+1]
else:
grid[position[0]][position[1]] = "."
grid[initial_position[0]][initial_position[1]] = move[0]
return "3"
else:
grid[position[0]][position[1]] = "."
grid[initial_position[0]][initial_position[1]] = move[0]
return "4"
elif move[1] == "L":
for i in range(int(move[2])):
if position[1]-1 >= 0:
if grid[position[0]][position[1]-1] == ".":
grid[position[0]][position[1]-1] = grid[position[0]][position[1]]
grid[position[0]][position[1]] = "."
position = [position[0], position[1]-1]
else:
grid[position[0]][position[1]] = "."
grid[initial_position[0]][initial_position[1]] = move[0]
return "3"
else:
grid[position[0]][position[1]] = "."
grid[initial_position[0]][initial_position[1]] = move[0]
return "4"
return "0"
main()