123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- # Transpose a list of lists in Python using itertools
- from itertools import zip_longest
- def transpose(l):
- return zip_longest(*l, fillvalue=None)
- grid = [[int(c) for c in s.strip()] for s in open("8.input")]
- visible = set()
- def get_vis(line):
- visible = []
- visible.append(0)
- visible.append(len(line) - 1)
- for i in range(1, len(line) - 1):
- if line[i] > max(line[0:i]) or line[i] > max(line[i+1:]):
- visible.append(i)
- return visible
- for y, line in enumerate(grid):
- for x in get_vis(line):
- visible.add((x, y))
- for x, line in enumerate(transpose(grid)):
- for y in get_vis(line):
- visible.add((x, y))
- print("Part 1:", len(visible))
- def get_score(base, segment):
- if len(segment) == 0:
- return 0
- score = 1
- for s in segment:
- if s >= base:
- break
- score += 1
- else:
- score -= 1
- return score
- scores = {}
- transposed = list(transpose(grid))
- def all_scores(grid, transposed, x, y):
- base = grid[x][y]
- return get_score(base, grid[x][y+1:]) *\
- get_score(base, list(reversed(grid[x][:y]))) *\
- get_score(base, list(transposed[y][x+1:])) *\
- get_score(base, list(reversed(transposed[y][:x])))
- for x in range(0, len(grid)):
- for y in range(0, len(grid)):
- scores[(x, y)] = all_scores(grid, transposed, x, y)
- print("Part 2:", max(scores.values()))
|