from utils import int_grid from d9 import get_neighbors g, t = int_grid(9), 0 for i in range(len(g)): for j in range(len(g[i])): v, n = g[i][j], get_neighbors(g, i, j) if all(map(lambda x: x > v, n)): t += v + 1 print(t)
from utils import int_grid from queue import Queue g = int_grid(11) def get_neighbors(g, i, j): neighbors = [(i - 1, j - 1), (i - 1, j), (i - 1, j + 1), (i + 1, j - 1), (i + 1, j + 1), (i + 1, j), (i, j - 1), (i, j + 1)] return [(ix, jx) for ix, jx in neighbors if ix >= 0 and ix < len(g) and jx >= 0 and jx < len(g[i])] s = 0 while True: s += 1 q = Queue() visited = set() for i in range(len(g)): for j in range(len(g[i])): g[i][j] += 1 if g[i][j] > 9: g[i][j] = 9 visited.add(tuple([i, j])) q.put(tuple([i, j])) while not q.empty(): i, j = q.get() n = get_neighbors(g, i, j) for ix, jx in n: g[ix][jx] += 1 if g[ix][jx] > 9 and (ix, jx) not in visited:
from utils import int_grid from d15 import solve def gen_grid(g): l = len(g) lg = 5 * l gg = [[0 for x in range(lg)] for j in range(lg)] for i in range(lg): for j in range(lg): gg[i][j] = g[i % l][j % l] + (i // l) + (j // l) if gg[i][j] > 9: gg[i][j] -= 9 return gg g = int_grid(15) solve(gen_grid(int_grid(15)))
from utils import int_grid from d15 import solve g = int_grid(15) solve(g)
from utils import int_grid from heapq import nlargest from functools import reduce from operator import mul from d9 import get_neighbors def get_neighbors_idx(g, i, j): neighbors = [(i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)] return [(ix,jx) for (ix, jx) in neighbors if ix >= 0 and ix < len(g) and jx >= 0 and jx < len(g[i])] def get_basin(g, i, j, visited, tovisit): visited.add(tuple([i, j])) n = set([(ix,jx) for ix, jx in get_neighbors_idx(g, i, j) if g[ix][jx] != 9]) tovisit = tovisit.union(n - visited) if len(tovisit) == 0: return 1 else: ti, tj = tovisit.pop() return 1 + get_basin(g, ti, tj, visited, tovisit) g,b = int_grid(9),[] for i in range(len(g)): for j in range(len(g[i])): v,n = g[i][j], get_neighbors(g, i, j) if all(map(lambda x: x > v, n)): b.append(get_basin(g, i, j, set(), set())) print(reduce(mul, nlargest(3, b)))