# Highest score on the field

## JavaScript (ES6), 157 bytes

s=>[...o=s].map((n,i)=>o=n<'.'|(a=[...s]).map(_=>a.map((c,j)=>c>'-'&c<10&(a[j+1]|a[j-1]|a[j+l]|a[j-l])>90?a[n-=-c,j]=99:0),a[i]=99)|o>n?o:n,l=~s.search
)|o


### Explanation

Takes an input field as a string. For each number in the field, sums all numbers in the area. It does this by iterating over each number in the field multiple times, adding the number to the score if an adjacent cell contains a previously counted number. Counted numbers that are part of the area are represented by setting them to 99 so that they are not counted again. Outputs the highest score as a number.

var solution =

s=>
[...o=s].map((n,i)=>o=n<'.'|             // for each number on the field
// n = area score
(a=[...s])                           // a = array of each field character
.map(_=>                             // loop to ensure whole area is found
a.map((c,j)=>                      // for each cell c at index j
c>'-'&c<10&                      // if the current cell is a number
(a[j+1]|a[j-1]|a[j+l]|a[j-l])>90 // and an adjacent cells is in the area
?a[n-=-c,j]=99:0                 // add the cell to the area
),                                 // and the number to the score
a[i]=99                            // mark the starting cell as counted
)
|o>n?o:n,                            // o = output (max of o and n)
l=~s.search
// l = line length of field
)
|o                                       // return o
<textarea id="input" rows="6" cols="40">12-45-
4-65-9
87-654
12-487
45----
684764</textarea><br />
<button onclick="result.textContent=solution(input.value)">Go</button>
<pre id="result"></pre>

## MATL, 5451 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


## Pyth, 93 bytes

A,hlh.zjJ\-.zKsm?qdJd\#HD'b=KXKbJR+i@HbTsm?&&gd0<dlKq@Kd\#'d0[tbhb-bG+bG;Wh=NxK\#=+Y'N)h.MZY


Try it online!

### First step: read the input

A,hlh.zjJ\-.zKsm?qdJd\#H
A,                           Assign the following to G and H:
hlh.z                          G = increment(length(first(all_input())))
jJ\-.z                    H = join(J="-",all_input())
m       H    for d in H:
?qdJ            if equal(d,J):
d               add d to the list
else:
\#             add "#" to the list
end
s             sum the list
K              assign to K

Sample input:
11-011123
111-010--
0010---01
111-01234

G = 10
H = "11-011123-111-010---0010---01-111-01234" (note the extra dashes connecting each line)
J = "-"
K = "##-######-###-###---####---##-###-#####"


### Second step: define a function to evaluate one area

D'b=KXKbJR+i@HbTsm?&&gd0<dlKq@Kd\#'d0[tbhb-bG+bG;
D'b                                             ;  def quote(b):
=KXKbJ                                              K[b] = J
R+                                            return the sum of A and B, where:
i@HbT                                         A = to_integer(H[b],10)

m                   [tbhb-bG+bG         for d in {dec(b), inc(b), minus(b,G), add(b,G)}:
?&&                                      if .... and ........ and ............... :
gd0                                      d>=0
<dlK                                           d<len(K)
q@Kd\#                                                  equal(K[d],"#")
else:
s                                        B = sum(temp_list)

Basically, this function (quote) is given a starting
point (b), and then recursively find its neighbour and
add their values to the output.


### Third step: read all the areas and find the rqeuired maximum

Wh=NxK\#=+Y'N)h.MZY
Wh=NxK\#     )         while inc(N=find(K,"#")):   --while K still has "#"
=+Y'N              Y+= quote(N)
.MZY    find the maximum of Y,
h        then print the first.