|
1 | 1 | from math import sqrt |
2 | 2 |
|
3 | | -# Cardinal directions |
4 | | -north = 1j |
5 | | -south = -1j |
6 | | -west = -1 |
7 | | -east = 1 |
8 | | -directions_all = [north, south, west, east] |
9 | | -directions_horizontal = [west, east] |
10 | | -directions_vertical = [north, south] |
11 | | - |
12 | | -# To be multiplied by the current cartinal direction |
13 | | -relative_directions = { |
14 | | - "left": 1j, |
15 | | - "right": -1j, |
16 | | - "ahead": 1, |
17 | | - "back": -1, |
18 | | -} |
19 | | - |
20 | 3 |
|
21 | 4 | class PlayerBlocked(Exception): |
22 | 5 | pass |
23 | 6 |
|
24 | 7 |
|
25 | | -def min_real(complexes): |
26 | | - real_values = [x.real for x in complexes] |
27 | | - return min(real_values) |
28 | | - |
29 | | - |
30 | | -def min_imag(complexes): |
31 | | - real_values = [x.imag for x in complexes] |
32 | | - return min(real_values) |
33 | | - |
34 | | - |
35 | | -def max_real(complexes): |
36 | | - real_values = [x.real for x in complexes] |
37 | | - return max(real_values) |
38 | | - |
39 | | - |
40 | | -def max_imag(complexes): |
41 | | - real_values = [x.imag for x in complexes] |
42 | | - return max(real_values) |
43 | | - |
44 | | - |
45 | | -def complex_sort(complexes, mode=""): |
46 | | - # Sorts by real, then by imaginary component (x then y) |
47 | | - if mode == "xy": |
48 | | - complexes.sort(key=lambda a: (a.real, a.imag)) |
49 | | - # Sorts by imaginary, then by real component (y then x) |
50 | | - elif mode == "yx": |
51 | | - complexes.sort(key=lambda a: (a.imag, a.real)) |
52 | | - # Sorts by distance from 0,0 (kind of polar coordinates) |
53 | | - else: |
54 | | - complexes.sort(key=lambda a: sqrt(a.imag ** 2 + a.real ** 2)) |
55 | | - return complexes |
56 | | - |
57 | | - |
58 | 8 | def collisions(players): |
59 | 9 | positions = [x.position for x in players] |
60 | 10 | if positions == set(positions): |
|
0 commit comments