MATL, 54 51 49 bytes
n:"G~1@(2Y6Z+leG45>1e*5M@)*]vtz:"otY*g]G48-X:*sX>
Input is a 2D char array in MATL(AB) format, with ;
as row separator. The inputs in the example and in the test cases are respectively:
['11-011123';'111-010--';'0010---01';'111-01234']
['1']
['1-1-1-1';'-1-1-1-';'2-1-1-1';'-1-1-1-']
['12-45-';'4-65-9';'87-654';'12-487';'45----';'684764']
['111-12';'------';'21--10']
Try it online!
Explanation
This works by building an adjacency matrix of the graph defined by the relation "being connected". As an example, consider the 3×4 field
52-4
15-8
3-72
Entries in a 2D array are easily described in MATL using (column-major) linear indexing. In the 3×4 case, the linear index of each entry is given as
1 4 7 10
2 5 8 11
3 6 9 12
The adjacency matrix is built in steps using matrix multiplication. In the first step, immediate neighbours are considered. For example, the point indexed 3 is neighbour of itself and of that with index 2. It's not a neighbour of 6 because that point doesn't contain a number according to the field. In this example the adjacency matrix of the relation "immediate-neighbour" is the 12×12 matrix L given as
1 1 0 1 0 0 0 0 0 0 0 0
1 1 1 0 1 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0 0 0 0
1 0 0 1 1 0 0 0 0 0 0 0
0 1 0 1 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 1
0 0 0 0 0 0 0 0 0 1 1 0
0 0 0 0 0 0 0 0 0 1 1 1
0 0 0 0 0 0 0 0 1 0 1 1
(It can seen that column 3 has value 1
at rows 2 and 3.) This matrix is always symmetric and its diagonal has value 1
for points that don't contain -
.
The next step would be the adjacency matrix of the relation "connected with at most one point in between". To obtain it, it suffices to multiply L by itself and set nonzero entries to 1
. In general, the adjacency matrix of the relation "connected by some path", M, is obtained by raising L to an exponent (in matrix sense) that represents the maximum possible path length. An upper bound of the maximum path length is the number of nonzero entries in L.
Computing the matrix power directly may cause overflow, because large numbers quickly occur. So it's better to gradually multiply by the same matrix, converting nonzero entries into 1 after each step to prevent large numbers from building up.
Column i of M represents the points that are connected (by any path) with point i. Now, the level field can be reduced to a column vector c in linear order, where each entry contains the corresponding number or an undefined value for -
. So in this case c would be
5
1
3
2
5
-
-
-
7
4
8
2
Mutiplying each column of M by c element-wise and computing the sum of each column gives, for each point i, the total score of the area point i belongs to. An area is defined by all points that are mutually connected. Note that many columns will give the same result; namely, columns i and j will give the same sum if points i and j are connected (belong to the same area). The final result is the maximum of those sums.
% Implicitly take input: 2D char array
n: % Range [1,...,N], where N is number of entries in the input
" % For loop. Each iteration builds a row of matrix L
G % Push input again
~ % Logical negate: transform into matrix of zeros
1 % Push 1, to be written into a matrix entry
@ % Iteration index. Ranges from 1 to N
( % Write that 1 into the N-th entry (linear order)
2Y6 % Push array [0 1 0; 1 1 1; 0 1 0]: mask of immediate neighbours
Z+ % Convolve and keep same-size result
le % Linearize into row array
G45> % Array of same size as the input that contains 1 for numbers, 0 for '-'
1e % Linearize into row array
* % Multiply element-wise
5M % Push last array again: 1 for numbers, 0 for '-'
@) % Get 0 or 1 value of that array corresponding to current iteration
* % Multiply. This is to give a row of zeros for non-numbers
] % End. We have all rows of L in the stack
v % Concatenate all rows into a matrix: L.
tz: % Duplicate. Range [1,...,K], where K is the number of nonzeros in L
" % For loop. Repear K times. This loop computes the 0/1 matrix power
o % Convert matrix entries to double
tY* % Duplicate and matrix-multiply
g % Convert to logical values, that is, nonzero values become 1
] % End. We have matrix M
G48- % Convert input chars to the corresponding numbers by subtractig 48
X: % Linearize into column array. This is vector c
* % Element-wise multiplication with broadcast (implicit repetition)
s % Sum of each column. Gives a row array
X> % Maximum of that row array
% Implicitly display
1Wow...nice challenge. – R. Kap – 2016-04-03T18:55:03.790
"0010---01" isn't that ["0010", "", "", "01"] instead? – Ven – 2016-04-03T19:25:43.903
also "111-01234", why isn't that
["111", "01234"]
? – Ven – 2016-04-03T19:28:17.230I don't understand. I thought the
-
separated the areas? Can you make the "what defines an area" part clearer, please? – Ven – 2016-04-03T19:49:53.233can you please reword the challenge to explain that? – Ven – 2016-04-03T19:53:54.290
Yes, thank you <3 – Ven – 2016-04-03T20:04:04.350