-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreetop_tree_house.py
70 lines (47 loc) · 1.77 KB
/
treetop_tree_house.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
import numpy as np
import operator as op
from utils import read_input, run
from functools import reduce
FNAME = "08/input.txt"
NORTH = (-1, 0)
EAST = (0, 1)
SOUTH = (1, 0)
WEST = (0, -1)
ALL_DIRECTIONS = (NORTH, EAST, SOUTH, WEST)
##########
# PART 1 #
##########
def get_line_of_sight(grid, coord, direction):
x, y = coord
if direction == NORTH:
return np.flip(grid[:x, y])
elif direction == EAST:
return grid[x, y+1:]
elif direction == SOUTH:
return grid[x+1:, y]
elif direction == WEST:
return np.flip(grid[x, :y])
def is_visible_from_direction(grid, coord, direction):
blocking_trees = get_line_of_sight(grid, coord, direction)
return np.all(blocking_trees < grid[coord])
def is_visible(grid, coord):
return any(is_visible_from_direction(grid, coord, dir) for dir in ALL_DIRECTIONS)
def part_one(input_file):
grid = np.array(read_input(input_file, parse_chunk=lambda l: list(map(int, l))))
x_max, y_max = grid.shape
return sum(is_visible(grid, (x, y)) for x in range(x_max) for y in range(y_max))
##########
# PART 2 #
##########
def find_viewing_distance(grid, coord, direction):
trees = get_line_of_sight(grid, coord, direction)
return next((i+1 for i, tree in enumerate(trees) if tree >= grid[coord]), len(trees))
def calculate_scenic_score(grid, coord):
viewing_distances = [find_viewing_distance(grid, coord, dir) for dir in ALL_DIRECTIONS]
return reduce(op.mul, viewing_distances, 1)
def part_two(input_file):
grid = np.array(read_input(input_file, parse_chunk=lambda l: list(map(int, l))))
x_max, y_max = grid.shape
return max(calculate_scenic_score(grid, (x, y)) for x in range(x_max) for y in range(y_max))
if __name__ == '__main__':
run(part_one, part_two, FNAME)