-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q2.py
65 lines (51 loc) · 1.43 KB
/
Q2.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
def calculate_plate_number(x, y):
if x == 0 and y == 0:
return 1
counter = 0
layer = 1
# Calculate the distance from the origin
if x == 0 or y == 0:
layer += max(abs(x), abs(y))
else:
layer += abs(x) + abs(y)
#print("layer:", layer)
for i in range(1, layer , 1):
if i == 1:
counter += 1
else:
counter += (2 * i) + 2 * (i - 2)
#print("counter:", counter)
# top point
if x == 0 and y == layer - 1:
return counter + 1
# right point
elif x == layer - 1 and y == 0:
return counter + layer
# bottom point
elif x == 0 and y == -layer + 1:
return counter + (2 * layer) - 1
# left point
elif x == -layer + 1 and y == 0:
return counter + (3 * layer) - 2
# top right edge
elif x > 0 and y > 0:
return counter + 1 + x
# bottem right edge
elif x > 0 and y < 0:
return counter + layer + abs(y)
# bottom left edge
elif x < 0 and y < 0:
return counter + (2 *layer) - 1 + abs(x)
# top left edge
else:
return counter + (3 * layer) - 2 + y
# Number of test cases
t = int(input())
# Iterate through each test case
for _ in range(t):
# Read the coordinates of the house
x, y = map(int, input().split())
# Calculate the plate number
plate_number = calculate_plate_number(x, y)
# Print the plate number
print(plate_number)