From de03e59c6f0fa67e4837282084dd0e1272819cea Mon Sep 17 00:00:00 2001 From: fettlaus <_Ox46K:26i?> Date: Tue, 17 Dec 2019 03:14:31 +0100 Subject: [PATCH] solved day10b --- days/test_day10.py | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/days/test_day10.py b/days/test_day10.py index 2cfc16b..a1ec891 100644 --- a/days/test_day10.py +++ b/days/test_day10.py @@ -1,4 +1,4 @@ -from math import atan2 +from math import atan2, sqrt import os from typing import Tuple @@ -6,20 +6,45 @@ from typing import Tuple inputfile = os.path.join(os.path.dirname(__file__), "input/day10_input") +def get_asteroids(): + with open(inputfile) as fp: + lines = [x.rstrip() for x in fp.readlines()] + return [(x, y) + for y, line in enumerate(lines) + for x, m in enumerate(line) + if m == '#'] + + def calc_arc(origin: Tuple[int, int], target: Tuple[int, int]) -> float: return atan2(target[0] - origin[0], target[1] - origin[1]) def test_day10a(): - with open(inputfile) as fp: - lines = [x.rstrip() for x in fp.readlines()] - asts = [(x, y) - for x, line in enumerate(lines) - for y, m in enumerate(line) - if m == '#'] - max = 0 + found = (0, 0) + asts = get_asteroids() for my in asts: angles = {calc_arc(my, target) for target in asts} - max = len(angles) if len(angles) > max else max + if len(angles) > max: + max = len(angles) + found = my + + print(f'Station is at {found} with {max} visible asteroids') assert max == 303 + + +def test_day10b(): + station = (26, 29) + res = None + count = 0 + asts = get_asteroids() + asts.remove(station) + while not res: + angles = {calc_arc(station, target): target for target in asts} + for ang in sorted(angles.keys(), reverse=True): + asts.remove(angles[ang]) + count += 1 + if count == 200: + res = angles[ang] + break + assert res[0] * 100 + res[1] == 408