My Advent of Code solutions in Python. kevinyap.ca/2019/12/going-fast-in-advent-of-code/
advent-of-code python
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add 2022/08

+47
+47
2022/day08.py
··· 1 + import fileinput 2 + from utils import mul 3 + 4 + DIRS = [ 5 + (0, 1), 6 + (0, -1), 7 + (-1, 0), 8 + (1, 0), 9 + ] 10 + 11 + # Parse problem input. 12 + grid = {} 13 + for y, line in enumerate(fileinput.input()): 14 + for x, c in enumerate(line.strip()): 15 + grid[x, y] = int(c) 16 + 17 + # Check what trees are visible. 18 + visibles = set() 19 + scenic_scores = {} 20 + 21 + for tree in grid: 22 + scores = [] 23 + for dx, dy in DIRS: 24 + x, y = tree 25 + score = 0 26 + while (x, y) in grid: 27 + nx = x + dx 28 + ny = y + dy 29 + 30 + # Are we at the edge yet? 31 + if (nx, ny) not in grid: 32 + visibles.add(tree) 33 + break 34 + elif grid[nx, ny] >= grid[tree]: 35 + score += 1 36 + break 37 + 38 + x = nx 39 + y = ny 40 + score += 1 41 + 42 + scores.append(score) 43 + 44 + scenic_scores[tree] = mul(scores) 45 + 46 + print("Part 1:", len(visibles)) 47 + print("Part 2:", max(scenic_scores.values()))