-
Notifications
You must be signed in to change notification settings - Fork 892
/
problem_068.py
48 lines (39 loc) · 1.29 KB
/
problem_068.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
def add_new_bishop(location, attack_positions, board_size):
# count how many times existing bishops can attack
count = 0
if location in attack_positions:
count += 1
# add new attack positions for future bishops
new_attack_positions = list()
i, j = location
while i > 0 and j > 0:
i -= 1
j -= 1
new_attack_positions.append((i, j))
i, j = location
while i > 0 and j < board_size - 1:
i -= 1
j += 1
new_attack_positions.append((i, j))
i, j = location
while i < board_size - 1 and j > 0:
i += 1
j -= 1
new_attack_positions.append((i, j))
i, j = location
while i < board_size - 1 and j < board_size - 1:
i += 1
j += 1
new_attack_positions.append((i, j))
attack_positions.extend(new_attack_positions)
return count, attack_positions
def get_attack_vectors(bishop_locations, board_size):
attack_positions = list()
total_count = 0
for location in bishop_locations:
count, attack_positions = add_new_bishop(
location, attack_positions, board_size)
total_count += count
return total_count
assert get_attack_vectors([(0, 0), (1, 2), (2, 2), (4, 0)], 5) == 2
assert get_attack_vectors([(0, 0), (1, 2), (2, 2)], 5) == 1