Created
December 12, 2019 00:09
-
-
Save khalido/b6e3ee1a214e8fb1b60b3fa9af8ff152 to your computer and use it in GitHub Desktop.
[adventofcode] misc snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import defaultdict | |
import numpy as np | |
inp1 = """.#..# | |
..... | |
##### | |
....# | |
...##""" | |
inp2 = """......#.#. | |
#..#.#.... | |
..#######. | |
.#.#.###.. | |
.#..#..... | |
..#....#.# | |
#..#....#. | |
.##.#..### | |
##...#..#. | |
.#....####""" | |
inp3 = """#.#...#.#. | |
.###....#. | |
.#....#... | |
##.#.#.#.# | |
....#.#.#. | |
.##..###.# | |
..#...##.. | |
..##....## | |
......#... | |
.####.###.""" | |
def parse_inp(inp=inp1): | |
return np.array([[c for c in row] for row in inp.splitlines()]) | |
def get_asteroids(grid): | |
positions = [] | |
for i in range(grid.shape[0]): | |
for j in range(grid.shape[1]): | |
pos = (i,j) | |
if grid[pos] == "#": | |
positions.append(pos) | |
return np.array(positions) | |
def get_unique_angles(p1, positions): | |
angles = set() | |
for pos in (positions - p1): # taking p1 as the origin | |
x, y = pos | |
if x == y == 0: # skip itself | |
continue | |
angle = math.atan2(y, x) | |
angles.add(angle) | |
return angles | |
def solve_1(inp=test_inp2): | |
inp = parse_inp(inp) | |
positions = get_asteroids(inp) | |
angle_max = 0 | |
for p in positions: | |
angles = get_unique_angles(p, positions) | |
sum_angles = len(angles) | |
if sum_angles > angle_max: | |
angle_max = sum_angles | |
return angle_max | |
assert solve_1(inp1) == 8 | |
assert solve_1(inp2) == 33 | |
assert solve_1(inp3) == 35 | |
solve_1(inp0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment