In this kata, your task is to find the maximum sum of any straight "beam" on a hexagonal grid, where its cell values are determined by a finite integer sequence seq.
In this context, a beam is a linear sequence of cells in any of the 3 pairs of opposing sides of a hexagon. We'll refer to the sum of a beam's integer values as the "beam value".Refer to the example below for further clarification.
Input
Your function will receive two arguments:

n : the length of each side of the hexagonal grid, where 2 <= n < 100
seq : a finite sequence of (positive and/or nonpositive) integers with a length >= 1The sequence is used to populate the cells of the grid and should be repeated as necessary.The sequence type will be dependent on the language (e.g. array for JavaScript, tuple for Python, etc.).

Output
Your function should return the largest beam value as an integer.
Test Example

In our test example, we have the following arguments:
n = 4
seq = [2, 4, 6, 8]

Below is the hexagonal grid determined by our example arguments;
the sequence repeats itself from left to right, then top to bottom.

   2 4 6 8
  2 4 6 8 2
 4 6 8 2 4 6
8 2 4 6 8 2 4
 6 8 2 4 6 8
  2 4 6 8 2
   4 6 8 2

The three diagrams below illustrate the "beams" in the hexagonal grid above.
In the grid on the left, the horizontal beams are highlighted by their likewise colors,
and the value of each beam is given to its right.
In the center grid, the beams highlighted go from upper-right to bottom-left (and vice-versa).
In the grid on the right, the beams highlighted go from upper-left to bottom-right (and vice-versa).

   2 4 6 8 -> 20           2 4 6 8           2 4 6 8
  2 4 6 8 2 -> 22         2 4 6 8 2         2 4 6 8 2
 4 6 8 2 4 6 -> 30       4 6 8 2 4 6       4 6 8 2 4 6
8 2 4 6 8 2 4 -> 34     8 2 4 6 8 2 4     8 2 4 6 8 2 4
 6 8 2 4 6 8 -> 34       6 8 2 4 6 8       6 8 2 4 6 8
  2 4 6 8 2 -> 22         2 4 6 8 2         2 4 6 8 2
   4 6 8 2 -> 20           4 6 8 2           4 6 8 2

The maximum beam value in our example is 34.

Test Specifications

All inputs will be valid
Full Test Suite: 12 fixed tests and 100 random tests
For JavaScript, module and require are disabled [NOTE: if you would like to suggest a module that you think should be permitted for JS, please leave a note in the Discourse section]

If you enjoyed this kata, be sure to check out my other katas

Source: codewars | Difficulty: EASY
Tags: Data structures

Ad-hoc

Starter code: def max_hexagon_beam(n:
	

Solutions:
from itertools import cycle, chain

def max_hexagon_beam(n, seq):
	h = 2 * n - 1
	seq = cycle(seq)
	sums = [[0] * h for _ in range(3)]
	for r in range(h):
		for (c, v) in zip(list(range(n + r if r < n else h + n - 1 - r)), seq):
			idxs = (r, c + max(0, r - n + 1), c + max(0, n - 1 - r))
			for (i, j) in enumerate(idxs):
				sums[i][j] += v
	return max(chain.from_iterable(sums))


def max_hexagon_beam(n: int, seq: tuple):
	i = 0
	grid = []
	length = len(seq)
	for r in range(1 - n, n):
		grid.append([])
		for q in range(1 - n - min(r, 0), n - max(r, 0)):
			grid[r + n - 1].append(seq[i % length])
			i += 1
	return max((max((sum((grid[r + n - 1][q + n - 1 + min(r, 0)] for r in range(1 - n - min(q, 0), n - max(q, 0)))) for q in range(1 - n, n))), max((sum((grid[r + n - 1][-s - r + n - 1 + min(r, 0)] for s in range(1 - n - min(r, 0), n - max(r, 0)))) for r in range(1 - n, n))), max((sum((grid[-q - s + n - 1][q + n - 1 + min(-q - s, 0)] for q in range(1 - n - min(s, 0), n - max(s, 0)))) for s in range(1 - n, n)))))


from itertools import chain, cycle
from collections import defaultdict

def max_hexagon_beam(n: int, seq: tuple):
	sums = {c: [0] * (2 * n - 1) for c in 'qrs'}
	it = cycle(seq)
	for q in range(-n + 1, n):
		for r in range(max(-n, -q - n) + 1, min(n, -q + n)):
			s = -q - r
			v = next(it)
			sums['q'][q + n - 1] += v
			sums['r'][r + n - 1] += v
			sums['s'][s + n - 1] += v
	return max(chain.from_iterable(list(sums.values())))


from itertools import cycle, zip_longest

def max_hexagon_beam(n, seq):
	seq = cycle(seq)
	hexagon = [[next(seq) for _ in range(2 * n - abs(r) - 1)] for r in range(1 - n, n)]
	x = list(zip_longest(*([0] * i + r for (i, r) in enumerate(hexagon, 1 - n)), fillvalue=0))
	y = list(zip_longest(*([0] * -i + r for (i, r) in enumerate(hexagon, 1 - n)), fillvalue=0))
	return max(map(sum, hexagon + x + y))


from itertools import cycle

def max_hexagon_beam(n: int, seq: tuple):
	l = n * 2 - 1
	ll = [l - abs(n - i - 1) for i in range(l)]
	c = cycle(seq)
	hex = [[next(c) for i in range(j)] for j in ll]
	sums = [sum(i) for i in hex]
	for (index, i) in enumerate(ll):
		start_row = [0, index % n + 1][index >= n]
		hex_row = []
		hex_row2 = []
		for j in range(i):
			y = j + start_row
			x = index - [0, y % n + 1][y >= n]
			hex_row.append(hex[y][x])
			hex_row2.append(hex[y][-1 - x])
		sums += [sum(hex_row), sum(hex_row2)]
	sums.sort()
	return sums[-1]


import numpy as np
from collections import deque

def max_hexagon_beam(n: int, seq: tuple):
	max_len = n * 2 - 1
	mat = [0] * max_len ** 2
	hexag_1 = np.zeros((max_len, max_len))
	hexag_2 = np.zeros((max_len, max_len))
	for i in range(0, len(mat)):
		mat[i] = seq[i % len(seq)]
	for i in range(n):
		hexag_1[i][0:n + i] = mat[0:n + i]
		rot = deque(hexag_1[i])
		rot.rotate(n - i - 1)
		hexag_2[i] = np.array(rot)
		mat[0:n + i] = []
	j = 1
	for i in range(n, max_len):
		hexag_1[i][j:] = mat[0:max_len - j]
		rot = deque(hexag_1[i])
		rot.rotate(n - i - 1)
		hexag_2[i] = np.array(rot)
		mat[0:max_len - j] = []
		j += 1
	horiz = hexag_1.sum(axis=1)
	diag_1 = hexag_1.sum(axis=0)
	diag_2 = hexag_2.sum(axis=0)
	a = [max(horiz), max(diag_1), max(diag_2)]
	return max(a)


from itertools import cycle

def max_hexagon_beam(n: int, seq: tuple):
	q = cycle(seq)
	(sx, sy, sz) = ([0] * (2 * n - 1), [0] * (2 * n - 1), [0] * (2 * n - 1))
	for z in range(-(n - 1), n):
		xp = -(n - 1) if z >= 0 else -(n - 1) - z
		xk = n - 1 if z <= 0 else n - 1 - z
		for x in range(xp, xk + 1):
			y = -(z + x)
			v = next(q)
			sx[x + n - 1] += v
			sy[y + n - 1] += v
			sz[z + n - 1] += v
	return max(sz + sy + sx)


from itertools import cycle, chain

def printhex(b):
	for x in b:
		st = ' '.join(map(str, x))
		print(f'{st:^{2 * len(b) - 1}}')

def d1sum(b, n, l):
	d1_sum = []
	for i in range(l):
		dsum = 0
		if i + n <= l:
			k = 1
			for x in range(n + i):
				if x < n:
					dsum += b[x][i]
				else:
					dsum += b[x][i - k]
					k += 1
		else:
			k = 1
			for x in range(i - n + 1, l):
				if x < n:
					dsum += b[x][i]
				else:
					dsum += b[x][i - k]
					k += 1
		d1_sum.append(dsum)
	return d1_sum

def d2sum(b, n, l):
	d2_sum = []
	for i in range(l):
		dsum = 0
		if i + n <= l:
			k = 1
			for x in range(n + i):
				if x < n:
					dsum += b[x][-1 - i]
				else:
					dsum += b[x][-1 - i + k]
					k += 1
		else:
			k = 1
			for x in range(i - n + 1, l):
				if x < n:
					dsum += b[x][-1 - i]
				else:
					dsum += b[x][-1 - i + k]
					k += 1
		d2_sum.append(dsum)
	return d2_sum

def max_hexagon_beam(n: int, seq: tuple):
	print((n, seq))
	l = 2 * n - 1
	b = []
	hb_sum = []
	itr = cycle(seq)
	for i in range(l):
		tmp = []
		if i + n <= l:
			k = i + n
		else:
			k = l - (i - n + 1)
		for j in range(k):
			tmp.append(next(itr))
		b.append(tmp)
		hb_sum.append(sum(tmp))
	d1_sum = d1sum(b, n, l)
	d2_sum = d2sum(b, n, l)
	return max(chain(hb_sum, d1_sum, d2_sum))


def max_hexagon_beam(n: int, ll: tuple):
	l1 = list(ll)
	tn = n * (2 * n - 1) + (2 * n - n - 1) ** 2
	mid = 2 * n - 1
	nest = [0 for i in range(mid)]
	for i in range(0, n):
		nest[n - i - 1] = mid - i
		nest[n + i - 1] = mid - i
	xv = l1[:tn % len(l1)]
	l2 = [l1 * (tn // len(l1)) + xv]
	cnt = 0
	a0 = [[]] * len(nest)
	a1 = [[0] * mid] * mid
	a2 = [[0] * mid] * mid
	for st in range(0, len(nest)):
		a0[st] = l2[0][cnt:cnt + nest[st]]
		cnt += nest[st]
	for st in range(0, n):
		a1[st] = a0[st] + [0] * (mid - nest[st])
		a1[mid - 1 - st] = [0] * (mid - nest[st]) + a0[mid - st - 1]
		a2[st] = [0] * (mid - nest[st]) + a0[st]
		a2[mid - 1 - st] = a0[mid - st - 1] + [0] * (mid - nest[st])
	sm0 = [0] * mid
	sm1 = [0] * mid
	sm2 = [0] * mid
	for i in range(0, mid):
		for j in range(0, mid):
			sm1[j] += a1[i][j]
			sm2[j] += a2[i][j]
	for i in range(0, mid):
		sm0[i] = sum(a0[i])
	return max(sm0 + sm1 + sm2)


def fila_left(n, fila):
	output = [None] * (2 * n - 1)
	column = 0
	while column <= n - 1:
		output[column] = [fila[row][0] for row in range(n + column)]
		for row in range(n + column):
			fila[row].pop(0)
		column += 1
	while column <= 2 * n - 2:
		output[column] = [fila[row][0] for row in range(column - n + 1, 2 * n - 1)]
		for row in range(column - n + 1, 2 * n - 1):
			fila[row].pop(0)
		column += 1
	return output

def max_hexagon_beam(n: int, seq: tuple):
	celdas = 3 * n ** 2 - 3 * n + 1
	elementos = [n + i for i in range(n)] + [2 * n - 2 - i for i in range(n - 1)]
	filas = len(elementos)
	grid = []
	for i in range(celdas):
		grid.append(seq[i % len(seq)])
	fila = [None] * filas
	usados = 0
	for i in range(filas):
		fila[i] = grid[usados:usados + elementos[i]]
		usados += elementos[i]
	maximo = max((sum(fila[i]) for i in range(filas)))
	fila2 = fila_left(n, fila)
	fila2 = [row[::-1] for row in fila2]
	maximo_left = max((sum(fila2[i]) for i in range(filas)))
	fila3 = fila_left(n, fila2)
	fila3 = [row[::-1] for row in fila3]
	maximo_right = max((sum(fila3[i]) for i in range(filas)))
	return max(maximo, maximo_left, maximo_right)