Highest score on the field

18

1

Introduction

Let a field be a rectangle filled with only the characters - and [0-9]. An example of a field is:

11-011123
111-010--
0010---01
111-01234

You see that this field has been separated into three smaller areas:

enter image description here

To calculate the score of a smaller area, we just add all numbers up. For example:

11
111
0010
111

1 + 1 + 1 + 1 + 1 + 0 + 0 + 1 + 0 + 1 + 1 + 1 = 9

The total score for this area is 9. We now do the same thing for the second area:

   011123
    010

0 + 1 + 1 + 1 + 2 + 3 + 0 + 1 + 0 = 9

The total score is also 9. Now we have to examine the last area:

       01
    01234

0 + 1 + 0 + 1 + 2 + 3 + 4 = 11

This has a total score of 11. The highest score on the field is 11, so this is what we need to output.

The Task

Given a field (in the form of a 2D string, an array, etc.), output the highest score on the field. You may assume that the given fields will always contain at least 1 digit. This is , so the submission with the least amount of bytes wins!

Test cases

Test case 1:

Input:
1

Output:
1

Test case 2:

Input:
1-1-1-1
-1-1-1-
2-1-1-1
-1-1-1-

Output:
2

Test case 3:

Input:
12-45-
4-65-9
87-654
12-487
45----
684764

Output:
69

Test case 4:

Input:
111-12
------
21--10

Output:
3

Adnan

Posted 2016-04-03T18:24:48.853

Reputation: 41 965

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.230

I 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.233

can 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

Answers

3

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

Luis Mendo

Posted 2016-04-03T18:24:48.853

Reputation: 87 464

3

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>

user81655

Posted 2016-04-03T18:24:48.853

Reputation: 10 181

2

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!

How it works


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],"#")
                                  'd                           add quote(d) to temp_list
                                                           else:
                                    0                          add 0 to temp_list
                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.

Leaky Nun

Posted 2016-04-03T18:24:48.853

Reputation: 45 011