+/AoC

advent of code

git clone https://git.julia.blue/+/AoC

Joseph RyanDays 7-14 (in python for now)a7718a0

master
761 B35 linesraw
1from operator import mul
2from functools import reduce
3from itertools import product
4
5
6def seen(l, n):  # TODO: "takewhile" + 1?
7    viz = 0
8    for x in l:
9        viz += 1
10        if x >= n:
11            break
12    return viz
13
14
15grid = [[int(x) for x in line.strip()] for line in open("input")]
16size = len(grid)
17trans = [[grid[x][y] for x in range(size)] for y in range(size)]
18total = size * 4 - 4
19maxviz = 0
20neighbors = lambda x, y: (
21    grid[x][:y][::-1],
22    grid[x][y + 1 :],
23    trans[y][:x][::-1],
24    trans[y][x + 1 :],
25)
26
27for x, y in product(range(1, size - 1), repeat=2):
28    a = grid[x][y]
29    adj = neighbors(x, y)
30    total += any(max(n) < a for n in adj)
31    maxviz = max(maxviz, reduce(mul, (seen(n, a) for n in adj)))
32
33
34print(total)
35print(maxviz)