Add solution for day 10 part 1

master
Nick Krichevsky 2019-12-10 02:31:02 -05:00
parent 768fc00db2
commit 4ccf8bfc2c
4 changed files with 141 additions and 0 deletions

25
day10/input.txt Normal file
View File

@ -0,0 +1,25 @@
#..#.#.#.######..#.#...##
##.#..#.#..##.#..######.#
.#.##.#..##..#.#.####.#..
.#..##.#.#..#.#...#...#.#
#...###.##.##..##...#..#.
##..#.#.#.###...#.##..#.#
###.###.#.##.##....#####.
.#####.#.#...#..#####..#.
.#.##...#.#...#####.##...
######.#..##.#..#.#.#....
###.##.#######....##.#..#
.####.##..#.##.#.#.##...#
##...##.######..##..#.###
...###...#..#...#.###..#.
.#####...##..#..#####.###
.#####..#.#######.###.##.
#...###.####.##.##.#.##.#
.#.#.#.#.#.##.#..#.#..###
##.#.####.###....###..##.
#..##.#....#..#..#.#..#.#
##..#..#...#..##..####..#
....#.....##..#.##.#...##
.##..#.#..##..##.#..##..#
.##..#####....#####.#.#.#
#..#..#..##...#..#.#.#.##

101
day10/py/main.py Normal file
View File

@ -0,0 +1,101 @@
import sys
import fractions
from typing import Tuple, Set
ASTEROID_CHAR = '#'
SPACE_CHAR = '.'
def get_items_in_slope(space_map: Tuple[Tuple[str]], start_row: int, start_col: int, rise: int, run: int) -> Tuple[Tuple[int, int]]:
row = start_row
col = start_col
items = []
while row >= 0 and col >= 0 and row < len(space_map) and col < len(space_map[row]):
if row == start_row and col == start_col:
row += rise
col += run
continue
items.append((row, col))
row += rise
col += run
return tuple(items)
def search_for_visible_in_all_directions(space_map: Tuple[Tuple[str, ...]], start_row: int, start_col: int, rise: int, run: int) -> Set[Tuple[int, int]]:
asteroids = set()
for i in (-1, 1):
for j in (-1, 1):
line_items = get_items_in_slope(space_map, start_row, start_col, i * rise, j * run)
for row, col in line_items:
if space_map[row][col] == ASTEROID_CHAR:
asteroids.add((row, col))
break
return asteroids
def get_visible_asteroid_count(space_map: Tuple[Tuple[str, ...]], start_row: int, start_col: int) -> int:
asteroids = set()
# This is so beyond gross, but it handles an edge case for checking both directions of the horizontal and vertical
for col in range(start_col-1, -1, -1):
if space_map[start_row][col] == ASTEROID_CHAR:
asteroids.add((start_row, col))
break
for col in range(start_col+1, len(space_map)):
if space_map[start_row][col] == ASTEROID_CHAR:
asteroids.add((start_row, col))
break
for row in range(start_row-1, -1, -1):
if space_map[row][start_col] == ASTEROID_CHAR:
asteroids.add((row, start_col))
break
for row in range(start_row+1, len(space_map[0])):
if space_map[row][start_col] == ASTEROID_CHAR:
asteroids.add((row, start_col))
break
used_slopes = set()
for rise in range(1, len(space_map)):
for run in range(1, len(space_map[0])):
slope_fraction = fractions.Fraction(rise, run)
if slope_fraction in used_slopes:
continue
used_slopes.add(slope_fraction)
rise, run = slope_fraction.numerator, slope_fraction.denominator
found_asteroids = search_for_visible_in_all_directions(space_map, start_row, start_col, rise, run)
asteroids = asteroids.union(found_asteroids)
return len(asteroids)
def part1(space_map: Tuple[Tuple[int]]):
best_count = None
for i, row in enumerate(space_map):
for j, item in enumerate(row):
if item != ASTEROID_CHAR:
continue
visible_asteroid_count = get_visible_asteroid_count(space_map, i, j)
if best_count is None or visible_asteroid_count > best_count:
best_count = visible_asteroid_count
return best_count
if __name__ == "__main__":
if len(sys.argv) != 2:
print("Usage: ./main.py in_file")
sys.exit(1)
with open(sys.argv[1]) as f:
inputs = tuple(tuple(line.strip()) for line in f.readlines())
for row in inputs:
print(''.join(row))
print(part1(inputs))

5
day10/sample1.txt Normal file
View File

@ -0,0 +1,5 @@
.#..#
.....
#####
....#
...##

10
day10/sample2.txt Normal file
View File

@ -0,0 +1,10 @@
......#.#.
#..#.#....
..#######.
.#.#.###..
.#..#.....
..#....#.#
#..#....#.
.##.#..###
##...#..#.
.#....####