test_map = """.#..#
.....
#####
....#
...##"""
import math
def process_map(m):
m = m.strip()
h = len(m.split("\n"))
w = len(m.split("\n")[0])
debug = []
for _ in range(h):
debug.append(["."] * w)
asteroids = []
for y, row in enumerate(m.split("\n")):
for x, pt in enumerate(row):
if pt != ".":
asteroids.append((x, y))
max_visible = None
pt = None
for x, y in asteroids:
vectors = set()
visible = 0
for asteroid in asteroids:
if (x, y) == asteroid:
continue
vector = (asteroid[0] - x, asteroid[1] - y)
magnitude = math.sqrt(vector[0] ** 2 + vector[1] ** 2)
norm_vector = round(vector[0] / magnitude, 4), round(vector[1] / magnitude, 4)
# print(asteroid, vector)
if vector not in vectors:
# print("new")
visible += 1
vectors.add(vector)
debug[y][x] = visible
if not max_visible or visible > max_visible:
max_visible = visible
pt = (x, y)
for y in debug:
for x in y:
print("%04s" % x, end=' ')
print()
return pt, max_visible
process_map("""...
.#.
..#""")
process_map(test_map)
process_map("""#.........
...A......
...B..a...
.EDCG....a
..F.c.b...
.....c....
..efd.c.gb
.......c..
....f...c.
...e..d..c""")
input10 = None
with open("AoC/input10.txt") as input_file:
input10 = input_file.read().strip()
process_map("""......#.#.
#..#.#....
..#######.
.#.#.###..
.#..#.....
..#....#.#
#..#....#.
.##.#..###
##...#..#.
.#....####""")
process_map("""#.#...#.#.
.###....#.
.#....#...
##.#.#.#.#
....#.#.#.
.##..###.#
..#...##..
..##....##
......#...
.####.###.""")
process_map(input10)
def spiral(a, b, h, w):
side = 3
while (
a - side // 2 >= 0 or
a + side // 2 <= w or
b - side // 2 >= 0 or
b + side // 2 <= h
):
x = a
y = b - side // 2
while x < a + side // 2:
yield x, y
x += 1
while y < b + side // 2:
yield x, y
y += 1
while x > a - side // 2:
yield x, y
x -= 1
while y > b - side // 2:
yield x, y
y -= 1
while x < a:
yield x, y
x += 1
side += 2
return
list(spiral(2, 2, 5, 5))
import math
from collections import defaultdict
def vaporize(m, a, b, q):
m = m.strip()
h = len(m.split("\n"))
w = len(m.split("\n")[0])
asteroids = []
for y, row in enumerate(m.split("\n")):
for x, pt in enumerate(row):
if pt != ".":
asteroids.append((x, y))
vectors = defaultdict(lambda: [])
angles = set()
def safeatan(n, d):
result = None
if d == 0:
result = math.pi / 2 if n >= 0 else -math.pi / 2
else:
result = math.atan(n / d)
return result + (math.pi if d < 0 else 0)
for x, y in asteroids:
if x == a and y == b:
continue
vector = (x - a, y - b)
magnitude = math.sqrt(vector[0]**2 + vector[1]**2)
angle = round(vector[0] / magnitude, 4), round(vector[1] / magnitude, 4)
vectors[angle].append(vector)
angles.add(angle)
angles = list(angles)
angles.sort(key=lambda x: safeatan(x[1], x[0]))
for angle in vectors:
vectors[angle].sort(key=lambda x: x[0]**2 + x[1]**2)
import pprint
# pprint.pprint(list(map(lambda x: (x, safeatan(x[1], x[0])), angles)))
# print(angles)
# print(vectors)
# return
index = 1
while True:
for angle in angles:
if index == q and vectors[angle]:
v = vectors[angle][0]
return v[0] + a, v[1] + b
if vectors[angle]:
vectors[angle].pop(0)
index += 1
return None
vaporize(input10, 20, 20, 200)
for i in range(1, 30):
print(i, vaporize(""".#....#####...#..
##...##.#####..##
##...#...#.#####.
..#.....X...###..
..#.#.....#....##""", 8, 3, i))
for i in [1, 2, 3, 10, 20, 50, 100, 199, 200, 201, 299]:
print(i, vaporize(""".#..##.###...#######
##.############..##.
.#.######.########.#
.###.#######.####.#.
#####.##.#.##.###.##
..#####..#.#########
####################
#.####....###.#.#.##
##.#################
#####.##.###..####..
..######..##.#######
####.##.####...##..#
.#####..#.######.###
##...#.##########...
#.##########.#######
.####.#.###.###.#.##
....##.##.###..#####
.#.#.###########.###
#.#.#.#####.####.###
###.##.####.##.#..##""", 11, 13, i))