9
\$\begingroup\$

Given a 20×20 grid of non-negative integers, find a 3×3 sub-grid where the product of the sums of the individual lines hits the maximum. Spoken in formulas:

Given the 3×3 sub-grid

matrix from (1,1) to (3,3)

the function to maximize is

f(A) = (x_11 + x_12 + x_13) ⋅ (x_21 + x_22 + x_23) ⋅ (x_31 + x_32 + x_33)

Per line the sum is calculated and the individual sums are then multiplied.

An example (only 5×5):

Example with the maximum 3×3 grid highlighted

The part highlighted in red is the part where the value of the function f(A) is largest for the entire grid:

(35 + 272 + 167) ⋅ (163 + 270 + 242) ⋅ (216 + 68 + 266) = 175972500

Input

Input is given on standard input and consists of 20 lines, each containing 20 numbers, separated by a space character (U+0020). The numbers are small enough that a 32-bit signed integer is sufficient to compute the result.

You may assume input to be given redirected from a file.

Output

Output is the result of the function f(A) for the 3×3 sub-grid that gives the largest result. In above example this would have been 175972500.

Sample input 1

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 5 5 5 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 5 5 5 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 5 5 5 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Sample output 1

3375

Sample input 2

40 30 42 222 74 265 93 209 115 274 139 177 7 274 12 15 103 36 236 91
294 97 35 272 167 126 18 262 292 48 8 296 85 93 245 4 4 240 276 153
52 8 163 270 242 224 142 72 99 240 199 26 224 198 96 242 295 70 56 247
247 130 216 68 266 142 93 214 30 8 12 163 59 84 40 287 233 65 30 242
283 245 164 53 148 282 73 186 296 3 9 233 184 30 205 221 92 96 5 101
132 228 43 91 228 37 266 140 159 109 10 230 40 114 264 3 266 164 219 283
70 207 218 28 299 78 279 30 179 118 196 138 61 229 110 55 203 73 124 112
16 232 28 187 292 78 194 70 65 203 255 227 176 21 32 225 11 15 92 151
58 237 261 41 213 171 170 111 4 209 99 194 40 108 267 137 179 31 35 221
184 209 264 275 163 268 261 40 198 185 45 188 280 273 54 79 270 286 273 121
208 83 66 156 104 62 188 68 53 194 46 279 280 170 266 148 272 285 178 245
210 130 213 118 165 210 213 66 54 189 166 193 57 213 14 101 143 109 172 101
80 193 287 4 140 65 208 111 8 206 107 285 109 29 211 78 170 247 290 193
148 123 15 164 28 153 222 67 156 165 6 163 114 77 165 17 143 209 278 100
3 102 58 148 82 181 84 29 2 236 231 195 118 278 252 257 179 123 276 287
143 141 254 142 200 243 171 32 164 195 235 260 269 191 190 46 65 166 82 146
69 194 65 220 234 110 45 135 125 208 138 20 233 291 256 162 148 216 247 138
10 53 164 107 2 270 226 227 88 206 193 13 41 130 218 249 76 35 207 91
199 36 207 256 58 215 28 277 234 29 198 148 219 244 136 16 30 258 219 264
183 118 48 218 15 125 279 103 73 8 86 113 9 157 239 273 146 208 50 86

Sample output 2

328536000

Sample input 3

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Sample output 3

185193

Sample input 4

242 248 292 60 9 45 94 97 294 202 219 118 115 243 207 81 288 289 241 232
236 225 205 227 242 85 139 63 5 162 170 121 208 133 22 235 125 288 209 270
31 182 108 170 88 268 297 66 249 158 285 157 267 117 150 18 44 66 117 223
205 32 93 132 33 99 209 282 222 272 255 1 80 270 202 54 117 35 139 273
87 74 113 146 177 14 154 92 282 4 192 60 171 286 66 299 89 276 138 289
60 16 143 277 202 284 77 296 215 96 200 10 226 143 136 131 218 246 254 20
244 118 299 218 81 195 129 93 205 202 264 141 196 150 214 72 231 136 243 192
236 157 176 187 104 182 134 29 151 234 81 143 22 119 45 241 17 225 197 7
156 284 92 13 131 60 113 77 228 65 200 83 3 63 83 88 241 113 115 198
62 101 242 270 121 122 119 78 105 273 55 7 239 236 37 252 66 164 56 44
70 57 100 87 34 298 140 259 36 257 1 204 110 299 245 56 43 121 192 10
240 36 272 255 10 194 143 66 27 131 22 78 57 71 128 5 1 155 236 122
281 160 42 147 272 151 196 240 291 280 209 245 271 46 103 35 85 145 78 140
155 74 232 205 235 223 147 39 171 240 56 187 184 70 28 81 293 125 283 159
297 203 75 46 221 77 106 12 268 94 220 156 78 97 266 208 228 137 212 49
4 157 69 51 225 23 61 202 19 23 41 260 161 218 142 251 299 187 283 158
118 136 71 77 21 199 110 87 103 120 153 157 213 234 155 141 135 24 120 199
16 204 292 245 54 260 294 159 254 15 209 41 154 54 231 167 87 291 31 26
212 274 99 199 170 184 47 227 64 2 117 275 67 84 143 214 143 125 24 61
205 250 133 88 210 112 4 160 3 287 54 143 293 94 287 42 105 94 76 169

Sample output 4

311042813

For the interested, the lengths of the submissions we got in our contest:

120 – AWK
124 – C
133 – Haskell
174 – C
270 – VB.NET

And our own (unranked) solutions:

  55 – Golfscript
118 – Ruby
192 – PowerShell

\$\endgroup\$

14 Answers 14

5
\$\begingroup\$

J, 38

":@(>./@,@(3*/\3+/\"1".;._2))&.stdin''

Sample use:

$ jconsole nsp.ijs <g1
3375
$ jconsole nsp.ijs <g2
328536000
$ jconsole nsp.ijs <g3
185193

Rough explanations:

  • ".;._2 converts raw input to matrix, ": converts the result to raw output
  • 3+/\"1 sums column-wise, 3 by 3
  • 3*/\ multiplies row-wise, 3 by 3
  • , flattens the result
  • >./ extracts the maximum
  • &.stdin'' makes a filter out of it
\$\endgroup\$
  • 3
    \$\begingroup\$ cool__________! \$\endgroup\$ – YOU Feb 25 '11 at 17:27
  • \$\begingroup\$ Great J golfing! \$\endgroup\$ – Eelvex Mar 8 '11 at 5:05
  • \$\begingroup\$ @S.Mark __________! \$\endgroup\$ – Mateen Ulhaq Mar 11 '11 at 4:27
6
\$\begingroup\$

Python - 146 characters

g=[map(int,raw_input().split(' '))for i in' '*20]
r=range(18)
print max(reduce(int.__mul__,[sum(v[j:j+3])for v in g[i:i+3]])for j in r for i in r)
\$\endgroup\$
  • \$\begingroup\$ The argument to split is unnecessary, and can save you three bytes. An even shorter way of reading input is g=eval('map(int,raw_input().split()),'*20), saving another four bytes for a total of 139. \$\endgroup\$ – hallvabo Mar 30 '11 at 8:37
3
\$\begingroup\$

C#, 324 321 308 chars

using System;using System.Linq;class X{static void Main(){int m=0,v,x=0,y;Func<int[],int>f=t=>t[x]+t[x+1]+t[x+2];var a=Console.In.ReadToEnd().Trim().Split('\n').Select(l=>l.Split(' ').Select(int.Parse).ToArray()).ToList();for(;x<18;x++)for(y=0;y<18;m=m>v?m:v)v=f(a[y])*f(a[++y])*f(a[y+1]);Console.Write(m);}}

Readable:

using System;
using System.Linq;
class X
{
    static void Main()
    {
        int m = 0, v, x = 0, y;
        Func<int[], int> f = t => t[x] + t[x + 1] + t[x + 2];
        var a = Console.In.ReadToEnd().Trim().Split('\n').Select(l =>
            l.Split(' ').Select(int.Parse).ToArray()).ToList();
        for (; x < 18; x++)
            for (y = 0; y < 18; m = m > v ? m : v)
                v = f(a[y]) * f(a[++y]) * f(a[y + 1]);
        Console.Write(m);
    }
}

14 characters could be shaved off by removing both calls to .Trim() if you can guarantee that the input will only use \n (no \r) and there is no newline after the last row of numbers.

Edits

  • (333 → 324 chars) Realised I could add x, y to the declaration at the top instead of declaring each in the for statement; that I could change one of the ToArrays to ToList; that I could increment y inside the call to f and thus get rid of it in the for.

  • (324 → 321 chars) Move the assignment to m into the for statement, getting rid of a semicolon and two curlies.

  • (321 → 308 chars) Joey’s suggestions in the comments. Thanks!

\$\endgroup\$
  • \$\begingroup\$ I am tempted to -1 for using technology completely inappropriate for codegolfing... :) \$\endgroup\$ – RomanSt Feb 25 '11 at 15:13
  • \$\begingroup\$ The Trim() within the int.Parse call can indeed be dropped (it ignores trailing whitespace) (314). You can then also call Select with just int.Parse as argument; no need for a lambda there anymore (308). I think this could be made shorter by working on a 400-item 1D array instead of the list of arrays as you have it now, but I'm unable to adapt it right now ;-) \$\endgroup\$ – Joey Apr 15 '11 at 9:51
  • \$\begingroup\$ @Joey: Wow... you make me feel noob :) Thanks! \$\endgroup\$ – Timwi Apr 15 '11 at 13:25
3
\$\begingroup\$

Java, 299 Chars

    class M{public static void main(String[]a){java.util.Scanner s=new java.util.Scanner(System.in);int i,j,k,p,m,t=20,x[][]=new int[t][t];for(i=m=0;i<t*t;i++)x[i/t][i%t]=s.nextInt();for(i=0;i<18;i++)for(j=0;j<18;j++,m=p>m?p:m)for(p=1,k=i;k<i+3;k++)p*=x[k][j]+x[k][j+1]+x[k][j+2];System.out.print(m);}}
\$\endgroup\$
  • \$\begingroup\$ Am I doing something wrong? The given code weighs in at 318 bytes here. In any case, the class does not need to be public, you can pull the array declaration of x into the previous line with x[][]=new int[t][t]. You can use a one-dimensional array which simplifies access greatly. You can remove the import and just inline the java.util. twice which is 5 bytes shorter. You can inline e which saves 3 bytes. With all that I got it down to 290 bytes. \$\endgroup\$ – Joey Feb 24 '11 at 16:40
  • \$\begingroup\$ @Joey, sorry about that, was using Notepad++, mistakenly read Char Count (no spaces). Will do most of those changes. The code will prolly change a lot if i used a 1D array. \$\endgroup\$ – st0le Feb 25 '11 at 5:08
  • \$\begingroup\$ Well, it changes declaration to x[]=new int[t*t], initialization to x[i]=s.nextInt(); and the update of p to p*=x[t*k+j]+x[t*k+j+1]+x[t*k+j+2];. Nothing too magic, I think :-) \$\endgroup\$ – Joey Feb 25 '11 at 11:07
  • \$\begingroup\$ enum M{M;{code;}}, and you can probably merge the increment and loop conditionals. \$\endgroup\$ – Nabb Feb 27 '11 at 0:50
  • \$\begingroup\$ With class M{static{java.util.Scanner s=... you come to 271. Improvement 10%. :) You get an errormessage after the output, but you may silently ignore it. :) \$\endgroup\$ – user unknown Apr 16 '11 at 1:21
2
\$\begingroup\$

Python (202)

import sys;g=map(lambda l:map(int,l.split()),sys.stdin.readlines())
r=range(18)
for j in range(20):l=g[j];g[j]=[sum(l[i:i+3])for i in r]
g=[g[i][j]*g[i+1][j]*g[i+2][j]for i in r for j in r]
print max(g)

Edit: Fixed r to be range(18) instead of 17.

\$\endgroup\$
2
\$\begingroup\$

Ruby - 78 chars

g=*$<
p (0..324).map{|i|eval g[i/18,3].map{|r|eval r.split[i%18,3]*?+}*?*}.max
\$\endgroup\$
2
\$\begingroup\$

Ruby, 106 characters

k=$<.read.split.map &:to_i;p (0..358).map{|a|a%20>17?0:(0..2).map{|i|k[20*i+a,3].inject:+}.inject(:*)}.max

Yet another straightforward solution.

  • Edit: Previous solution produced incorrect results for some edge cases.
  • Edit: (114 -> 106) Use a flat array, check index%20 to prevent overflows into the next row.
\$\endgroup\$
2
\$\begingroup\$

Windows PowerShell, 116 117 124 152 170

$g=-split$input
(0..359|?{$_%20-le17}|%{$i=$_
(0..2|%{$g[($a=$i+20*$_)..($a+2)]-join'+'|iex})-join'*'|iex}|sort)[-1]

Fairly straightforward, actually.

\$\endgroup\$
2
\$\begingroup\$

C 184 171 Characters

#define f(i,b)for(i=0;i<b;i++)
i,j,k,l,m,M,s,a[400];main(){f(i,400)scanf("%d",a+i);f(i,18)f(j,18){m=1;f(k,3){s=0;f(l,3)s+=a[(i+k)*20+j+l];m*=s;}M=m>M?m:M;}printf("%d",M);}
\$\endgroup\$
  • \$\begingroup\$ You could squeeze out a few more characters by using #define f(i,b)for(i=0;i<b;i++) and removing 0, from the invocations. \$\endgroup\$ – Joey Adams Feb 25 '11 at 1:59
  • \$\begingroup\$ @Joey Oh Ya! I completely missed that. Thanks \$\endgroup\$ – fR0DDY Feb 25 '11 at 3:18
  • \$\begingroup\$ Can you use #define f(i,b)for(i=b;--i;) instead? \$\endgroup\$ – Nabb Feb 27 '11 at 0:52
1
\$\begingroup\$

J+tr, 84+10

y=:20 20$".(1!:1)3
s=:3 :'(y&((][;.0~3 3,:~[)~))"1(,."0/~i.18)'
>./,([:*/+/"1)"2 s y

runs like this < golfdata tr \\n ' '| ./test.ijs so I consider the tr \\n ' ' part as part of the program.

y=: 40 30 43 ...
>./,([:*/+/"1)"2 s y
328536000

z =: 20 20 $>:i.20
>./,([:*/+/"1)"2 s z
185193
\$\endgroup\$
  • \$\begingroup\$ What's the easiest way to run J answers? \$\endgroup\$ – gnibbler Feb 24 '11 at 3:30
  • \$\begingroup\$ @gnibbler: the only way I know (unfortunately) is to download and install J. \$\endgroup\$ – Eelvex Feb 24 '11 at 3:34
  • \$\begingroup\$ @gnibbler ...which is being open sourced. Yay. \$\endgroup\$ – J B Mar 2 '11 at 16:42
1
\$\begingroup\$

Haskell, 129 characters

import List
t f z@(_:y@(_:x))=zipWith3((f.).f)z y x
main=interact$show.maximum.(t(*)=<<).transpose.map(t(+).map read.words).lines
\$\endgroup\$
1
\$\begingroup\$

Golfscript, 48 chars

n%"{~}%:^,,{^>3<}%":|~{{+}*}%20/zip|~{{*}*}%$)p;

previous one

n%{' '%"{~}%:^,,{^>3<}%":|~{{+}*}%}%zip|~{{*}*}%$)p;

Some explanations, but I am still newbie in golfscript, some might not be correct, please correct me, if you see something wrong.

n%                 #.lines
{                  #.each
   ' '%               #.split ' '
   "                  #"... literal string start
      {~}%               #.flatten
      :^                 #assign to variable ^
      ,                  #.size
      ,                  #.times
      {                  #.each
         ^>                 #^[n:]
         3<                 #[:3]
      }%                 #.map end
   "                  #..." literal string end
   :|                 #assign literal string to variable | to reuse later
   ~                  #eval
   {                  #.each
      {+}                #sum
      *                  #.fold
   }%                 #.map end
}%                 #.map end
zip                #.zip
|~                 #eval variable | against result of .zip
{                  #.each
   {*}                #product
   *                  #.fold
}%                 #.map end
$                  #.sort
)                  #uncons / .pop
p                  #puts
;                  #discard top item from stack

Tests

$ cat sample-1159-1.txt | golfscript codegolf-1159.gs
3375

$ cat sample-1159-2.txt | golfscript codegolf-1159.gs
328536000

$ cat sample-1159-3.txt | golfscript codegolf-1159.gs
185193
\$\endgroup\$
1
\$\begingroup\$

APL (Dyalog Unicode), 22 bytesSBCS

Complete program.

⌈/,{×/+/⍵}⌺3 3↑⍎20⍴'⎕'

Try it online!

20⍴'⎕' reshape the character (which stands for evaluated input from stdin) to length 20

 execute that (gives use 20 lists)

 mix the lists into a matrix

{}⌺3 3 on each 3-by-3 sub-matrix:

+/⍵ sum the rows

×/ multiply the sums

, flatten the matrix of sums

⌈/ find the maximum

\$\endgroup\$
0
\$\begingroup\$

Powershell, 103 bytes

(($x=-split($args-replace'(\d+)(?= (\d+) (\d+))','$1+$2+$3')|iex)|%{$_*$x[$j+20]*$x[$j+++40]}|sort)[-1]

Explanation:

  • The regexp transforms each string like 1 2 3 4 5 6 to the string like 1+2+3 2+3+4 3+4+5 5 6;
  • Then the script splits the string 1+2+3 2+3+4 3+4+5 5 6 by spaces and evaluates each element by iex (alias for Invoke-Expression). A result is an array like 6,9,11,5,6;
  • Finally, the script concatenates all arrays to one, calulates a products and returns a maximum.

It is essential that we need a maximum of products of sum of non-negative integers. So we don't need to filter out the last 2 values in each row and last 2 rows.

Less golfed test script

$f = {

$x=-split($args-replace'(\d+)(?= (\d+) (\d+))','$1+$2+$3')|iex
($x|%{$_*$x[$j+20]*$x[$j+++40]}|sort)[-1]

}

@(
    ,(  3375,
        "1 2 3 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1",
        "4 3 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1",
        "4 3 3 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1",
        "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1",
        "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1",
        "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1",
        "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1",
        "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1",
        "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1",
        "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1",
        "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1",
        "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1",
        "1 1 1 1 1 1 1 1 1 1 1 1 5 5 5 1 1 1 1 1",
        "1 1 1 1 1 1 1 1 1 1 1 1 5 5 5 1 1 1 1 1",
        "1 1 1 1 1 1 1 1 1 1 1 1 5 5 5 1 1 1 1 1",
        "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1",
        "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1",
        "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1",
        "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1",
        "1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1")
    ,(  328536000,
        "40 30 42 222 74 265 93 209 115 274 139 177 7 274 12 15 103 36 236 91",
        "294 97 35 272 167 126 18 262 292 48 8 296 85 93 245 4 4 240 276 153",
        "52 8 163 270 242 224 142 72 99 240 199 26 224 198 96 242 295 70 56 247",
        "247 130 216 68 266 142 93 214 30 8 12 163 59 84 40 287 233 65 30 242",
        "283 245 164 53 148 282 73 186 296 3 9 233 184 30 205 221 92 96 5 101",
        "132 228 43 91 228 37 266 140 159 109 10 230 40 114 264 3 266 164 219 283",
        "70 207 218 28 299 78 279 30 179 118 196 138 61 229 110 55 203 73 124 112",
        "16 232 28 187 292 78 194 70 65 203 255 227 176 21 32 225 11 15 92 151",
        "58 237 261 41 213 171 170 111 4 209 99 194 40 108 267 137 179 31 35 221",
        "184 209 264 275 163 268 261 40 198 185 45 188 280 273 54 79 270 286 273 121",
        "208 83 66 156 104 62 188 68 53 194 46 279 280 170 266 148 272 285 178 245",
        "210 130 213 118 165 210 213 66 54 189 166 193 57 213 14 101 143 109 172 101",
        "80 193 287 4 140 65 208 111 8 206 107 285 109 29 211 78 170 247 290 193",
        "148 123 15 164 28 153 222 67 156 165 6 163 114 77 165 17 143 209 278 100",
        "3 102 58 148 82 181 84 29 2 236 231 195 118 278 252 257 179 123 276 287",
        "143 141 254 142 200 243 171 32 164 195 235 260 269 191 190 46 65 166 82 146",
        "69 194 65 220 234 110 45 135 125 208 138 20 233 291 256 162 148 216 247 138",
        "10 53 164 107 2 270 226 227 88 206 193 13 41 130 218 249 76 35 207 91",
        "199 36 207 256 58 215 28 277 234 29 198 148 219 244 136 16 30 258 219 264",
        "183 118 48 218 15 125 279 103 73 8 86 113 9 157 239 273 146 208 50 86")
    ,(  185193,
        "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20",
        "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20",
        "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20",
        "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20",
        "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20",
        "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20",
        "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20",
        "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20",
        "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20",
        "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20",
        "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20",
        "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20",
        "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20",
        "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20",
        "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20",
        "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20",
        "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20",
        "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20",
        "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20",
        "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20")
    ,(  311042813,
        "242 248 292 60 9 45 94 97 294 202 219 118 115 243 207 81 288 289 241 232",
        "236 225 205 227 242 85 139 63 5 162 170 121 208 133 22 235 125 288 209 270",
        "31 182 108 170 88 268 297 66 249 158 285 157 267 117 150 18 44 66 117 223",
        "205 32 93 132 33 99 209 282 222 272 255 1 80 270 202 54 117 35 139 273",
        "87 74 113 146 177 14 154 92 282 4 192 60 171 286 66 299 89 276 138 289",
        "60 16 143 277 202 284 77 296 215 96 200 10 226 143 136 131 218 246 254 20",
        "244 118 299 218 81 195 129 93 205 202 264 141 196 150 214 72 231 136 243 192",
        "236 157 176 187 104 182 134 29 151 234 81 143 22 119 45 241 17 225 197 7",
        "156 284 92 13 131 60 113 77 228 65 200 83 3 63 83 88 241 113 115 198",
        "62 101 242 270 121 122 119 78 105 273 55 7 239 236 37 252 66 164 56 44",
        "70 57 100 87 34 298 140 259 36 257 1 204 110 299 245 56 43 121 192 10",
        "240 36 272 255 10 194 143 66 27 131 22 78 57 71 128 5 1 155 236 122",
        "281 160 42 147 272 151 196 240 291 280 209 245 271 46 103 35 85 145 78 140",
        "155 74 232 205 235 223 147 39 171 240 56 187 184 70 28 81 293 125 283 159",
        "297 203 75 46 221 77 106 12 268 94 220 156 78 97 266 208 228 137 212 49",
        "4 157 69 51 225 23 61 202 19 23 41 260 161 218 142 251 299 187 283 158",
        "118 136 71 77 21 199 110 87 103 120 153 157 213 234 155 141 135 24 120 199",
        "16 204 292 245 54 260 294 159 254 15 209 41 154 54 231 167 87 291 31 26",
        "212 274 99 199 170 184 47 227 64 2 117 275 67 84 143 214 143 125 24 61",
        "205 250 133 88 210 112 4 160 3 287 54 143 293 94 287 42 105 94 76 169")
) | % {
    $expected, $a = $_
    $result = &$f @a
    "$($result-eq$expected): $result"
}

Output

True: 3375
True: 328536000
True: 185193
True: 311042813

Powershell, 105 bytes, no-regexp alternative

The tribute to the smart expression |?{$_%20-le17} in the Joey's answer:

(($x=($y=-split$args)|?{$i++%20-le17}|%{+$y[$i-1]+$y[$i]+$y[$i+1]})|%{$_*$x[$j+18]*$x[$j+++36]}|sort)[-1]
\$\endgroup\$
  • \$\begingroup\$ This does not follow the input specification. \$\endgroup\$ – Joey Nov 24 '18 at 21:34
  • \$\begingroup\$ why? .......... \$\endgroup\$ – mazzy Nov 25 '18 at 7:38
  • \$\begingroup\$ Your script is taking the input as arguments instead of on stdin. \$\endgroup\$ – Joey Nov 25 '18 at 12:09
  • \$\begingroup\$ Why do you think the 'standard input' is 'stdin' only? \$\endgroup\$ – mazzy Nov 25 '18 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.