Skip to content

Latest commit

 

History

History
81 lines (58 loc) · 2.19 KB

8.md

File metadata and controls

81 lines (58 loc) · 2.19 KB

Day 8

Part 1 Part 2 Total
Time 51:58 13:43 1:05:41

Ladies and gentlemen, welcome to adventofreadingcomprehension.com! Not proud of my time at all. This could have been a big break for me and helped me to get out of second place in the NJIT ACM leaderboard, but instead I stubbornly typed up my code without reading the prompt again for almost an hour before realizing the following. My assumption had been that each successive tree needed to be shorter than the last. Turned out, after only a little inspection, the problem was asking that each successive tree only be shorter than the one in question. Oh boy!

Part 1

from helpers.datagetter import data_in, submit

ans = 0
din = data_in(split=True, numbers=False)

trees = []
for line in din:
    trees.append(list(map(int, [*line])))


def get_out(x, y, dirx, diry):
    to_edge = []
    while x != -1 and y != -1 and x != len(trees) and y != len(trees[0]):
        to_edge.append(trees[x][y])
        x += dirx
        y += diry
        
    return (max(to_edge[1:]) if len(to_edge[1:]) else -1) < to_edge[0]


for i in range(len(trees)):
    for j in range(len(trees[0])):
        if get_out(i, j, 1, 0) or get_out(i, j, -1, 0) or get_out(i, j, 0, 1) or get_out(i, j, 0, -1):
            ans += 1

print(ans)
submit(ans)

Part 2

from helpers.datagetter import data_in, submit

ans = 0
din = data_in(split=True, numbers=False)

trees = []
for line in din:
    trees.append(list(map(int, [*line])))


def get_visible(x, y, dirx, diry):
    h = trees[x][y]
    seen = 0
    x+=dirx
    y+=diry
    while x != -1 and y != -1 and x != len(trees) and y != len(trees[0]):
        if trees[x][y] >= h:
            seen += 1
            break
        seen += 1
        x+=dirx
        y+=diry

    return seen


max_trees = defaultdict(int)

for i in range(len(trees)):
    for j in range(len(trees[0])):
        max_trees[f"{i},{j}"] += get_visible(i, j, 1, 0)
        max_trees[f"{i},{j}"] *= get_visible(i, j, -1, 0)
        max_trees[f"{i},{j}"] *= get_visible(i, j, 0, 1)
        max_trees[f"{i},{j}"] *= get_visible(i, j, 0, -1)

ans = max(max_trees.values())
submit(ans)