From feb7dce3882be3c361e2960f35713135d8ddea9d Mon Sep 17 00:00:00 2001 From: kleph Date: Sat, 10 Dec 2022 16:12:06 +0100 Subject: [PATCH] 2022 - Day 8 part 2 --- 2022/8/8_2.py | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 2022/8/8_2.py diff --git a/2022/8/8_2.py b/2022/8/8_2.py new file mode 100644 index 0000000..377715c --- /dev/null +++ b/2022/8/8_2.py @@ -0,0 +1,60 @@ +#!/usr/bin/env python +# 2022 - Advent Of Code 8 part 2 + +file = 'input_example.txt' +file = 'input.txt' + + +def parse_file(input_file): + tmap = [] + vmap = [] + with open(input_file, encoding="utf-8") as f: + for line in f.readlines(): + tline = [] + vline = [] + for c in line.strip(): + tline.append(int(c)) + vline.append(0) + tmap.append(tline) + vmap.append(vline) + return tmap, vmap + + +tree_map, visible_map = parse_file(file) +for y in range(1, len(tree_map) - 1): + for x in range(1, len(tree_map[0]) - 1): + # calculate scenic score + up = down = left = right = 1 + cur_height = tree_map[y][x] + var = y - 1 + while var > 0 and tree_map[var][x] < cur_height: + up += 1 + var -= 1 + var = y + 1 + while var < len(tree_map)-1 and tree_map[var][x] < cur_height: + down += 1 + var += 1 + var = x - 1 + while var > 0 and tree_map[y][var] < cur_height: + left += 1 + var -= 1 + var = x + 1 + while var < len(tree_map[0])-1 and tree_map[y][var] < cur_height: + right += 1 + var += 1 + + scenic_score = cur_height + # print(f'up: {up}, down: {down}, left: {left}, right: {right}') + scenic_score = up * down * left * right + visible_map[y][x] = scenic_score + +for v in visible_map: + print(v) + +score_max = 0 +for y in range(1, len(tree_map) - 1): + for x in range(1, len(tree_map[0]) - 1): + if visible_map[y][x] > score_max: + score_max = visible_map[y][x] + +print(f'max scenic score: {score_max}')