-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday10.py
105 lines (94 loc) · 3.25 KB
/
day10.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
from math import gcd, atan2, pi
map = """.#......##.#..#.......#####...#..
...#.....##......###....#.##.....
..#...#....#....#............###.
.....#......#.##......#.#..###.#.
#.#..........##.#.#...#.##.#.#.#.
..#.##.#...#.......#..##.......##
..#....#.....#..##.#..####.#.....
#.............#..#.........#.#...
........#.##..#..#..#.#.....#.#..
.........#...#..##......###.....#
##.#.###..#..#.#.....#.........#.
.#.###.##..##......#####..#..##..
.........#.......#.#......#......
..#...#...#...#.#....###.#.......
#..#.#....#...#.......#..#.#.##..
#.....##...#.###..#..#......#..##
...........#...#......#..#....#..
#.#.#......#....#..#.....##....##
..###...#.#.##..#...#.....#...#.#
.......#..##.#..#.............##.
..###........##.#................
###.#..#...#......###.#........#.
.......#....#.#.#..#..#....#..#..
.#...#..#...#......#....#.#..#...
#.#.........#.....#....#.#.#.....
.#....#......##.##....#........#.
....#..#..#...#..##.#.#......#.#.
..###.##.#.....#....#.#......#...
#.##...#............#..#.....#..#
.#....##....##...#......#........
...#...##...#.......#....##.#....
.#....#.#...#.#...##....#..##.#.#
.#.#....##.......#.....##.##.#.##""".splitlines()
width = height = len(map)
def get_line_of_sight(x, y):
points = []
for nx in range(x - 1, -1, -1):
if map[y][nx] == "#":
points.append((nx, y))
break
for nx in range(x + 1, width):
if map[y][nx] == "#":
points.append((nx, y))
break
for ny in range(y - 1, -1, -1):
if map[ny][x] == "#":
points.append((x, ny))
break
for ny in range(y + 1, width):
if map[ny][x] == "#":
points.append((x, ny))
break
for ox in range(-1, -width, -1):
for oy in range(-1, -width, -1):
if gcd(oy, ox) != 1:
continue
for oymult in [-1, 1]:
for multiplier in range(1, width):
nx = x + ox * multiplier
ny = y + oy * multiplier * oymult
# print(nx, ny, ox, oy, oymult, multiplier)
if 0 <= nx < width and 0 <= ny < height:
if map[ny][nx] == "#":
points.append((nx, ny))
break
for ox in range(1, width):
for oy in range(1, width):
if gcd(oy, ox) != 1:
continue
for oymult in [-1, 1]:
for multiplier in range(1, width):
nx = x + ox * multiplier
ny = y + oy * multiplier * oymult
# print(nx, ny, ox, oy, oymult, multiplier)
if 0 <= nx < width and 0 <= ny < height:
if map[ny][nx] == "#":
points.append((nx, ny))
break
return set(points)
tiles = {}
for y, row in enumerate(map):
for x, tile in enumerate(row):
if tile == "#":
tiles[len(get_line_of_sight(x, y))] = x, y
key = max(tiles.keys())
print(key, tiles[key])
x, y = tiles[key]
asteroids = get_line_of_sight(x, y)
sorted_points = sorted(
asteroids, key=lambda point: atan2(point[0] - x, y - point[1]) % (2 * pi)
)
point = sorted_points[199]
print(point[0] * 100 + point[1])