Created
November 16, 2023 12:32
-
-
Save JarkoDubbeldam/1b3e91c47c558b43eea960388f5cee35 to your computer and use it in GitHub Desktop.
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
def get_visible_indexes(sequence: list[int]) -> list[int]: | |
running_max = -1 | |
for index, value in enumerate(sequence): | |
if value > running_max: | |
running_max = value | |
yield index | |
def parse_inputs(text: str) -> list[list[int]]: | |
return [[int(value) for value in line] for line in text.splitlines()] | |
def execute_part_1(parsed: list[list[int]]) -> Any: | |
dimx, dimy = len(parsed), len(parsed[0]) | |
forward_horizontal_visibles = set( | |
(x, visible_index_y) | |
for x, line in enumerate(parsed) | |
for visible_index_y in get_visible_indexes(line) | |
) | |
backward_horizontal_visibles = set( | |
(x, dimy - visible_index_y - 1) | |
for x, line in enumerate(parsed) | |
for visible_index_y in get_visible_indexes(line[::-1]) | |
) | |
forward_vertical_visibles = set( | |
(visible_index_x, y) | |
for y in range(dimy) | |
for visible_index_x in get_visible_indexes([line[y] for line in parsed]) | |
) | |
backward_vertical_visibles = set( | |
(dimx - visible_index_x - 1, y) | |
for y in range(dimy) | |
for visible_index_x in get_visible_indexes([line[y] for line in parsed[::-1]]) | |
) | |
return len(forward_horizontal_visibles | backward_horizontal_visibles | forward_vertical_visibles | backward_vertical_visibles) | |
with open("inputs.txt", "r") as f: | |
text = f.read() | |
parsed = parse_inputs(text) | |
print(execute_part_1(parsed)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment