Sum a Cubically cube's faces

13

Manually summing a Cubically cube's faces is tedious and time-consuming, sorta like writing code in Cubically itself.

In Most efficient cubifier, I asked you to translate ASCII to Cubically source. One of the answers there uses a cube initialization sequence and then modifies the resulting cube based on the sums of the pre-initialized cube. This method has been used in many Cubically-related programs since. When testing a new initialization sequence, one has to add up all the values on all the faces, which usually takes two or three minutes.

Your task is to automate this process for us!

You will take two inputs, an integer n and a string c. These may be read from command line arguments, function arguments, standard input, a file, or any combination of those. c will be a Cubically memory cube of size n as pretty-printed by the interpreter.

The Cubically interpreter dumps its cube to STDERR upon program termination, formatted nicely for simple viewing. Run an empty program in the Cubically interpreter and open the debug section to see the cube dump of an initialized cube. Add an argument 4 to see a 4x4x4, or 5 to see a 5x5x5, etc.

If n is 3, c will follow this format (the integers will be variable):

   000
   000
   000
111222333444
111222333444
111222333444
   555
   555
   555

Spaces, newlines, and all. If n is 4, c will look like this (also with variable integers):

    0000
    0000
    0000
    0000
1111222233334444
1111222233334444
1111222233334444
1111222233334444
    5555
    5555
    5555
    5555

Et cetera.

Your program will output six integers. The first integer will be the sum of all the numbers on the top face.

   000
   000          top face
   000
111222333444    left, front, right, and back faces, respectively
111222333444
111222333444
   555
   555          bottom face
   555

The second integer will be the sum of the left face, the third the front, the fourth the right, the fifth the back and the sixth the bottom.

So if n was 3 and c was this:

   242
   202
   242
000131555313
010121535343
000131555313
   424
   454
   424

Your program would output 20 1 14 43 24 33.

Additional rules:

  • The output integers must be delimited by non-integer characters. You may also choose to return an array.
  • You may assume that the input is correct - n is an integer and c is a cube from Cubically's debugging output. So if n was 3.0 and c was foo bar, your program could break and still be valid.
  • Your program only needs to work for n > 1 and n < 1260. It may (attempt to) handle larger or smaller cube sizes, but it is not necessary.

This is , so the shortest code wins! If you need help, feel free to ask in the Cubically chatroom.

MD XF

Posted 2017-08-19T04:55:47.097

Reputation: 11 605

May we assume the input to include all trailing spaces so as to be a rectangle? – fireflame241 – 2017-08-19T05:00:32.257

@fireflame241 if you mean n spaces after every line, no. They are not included in the dump. – MD XF – 2017-08-19T05:02:10.170

1We really need a "cubically" tag. – Mr. Xcoder – 2017-08-19T10:56:28.213

@Mr.Xcoder status completed :) I'd been thinking that too, and I'm working on four more Cubically challenges ATM. – MD XF – 2017-08-19T14:03:14.217

Answers

6

Jelly, 16 14 13 bytes

3 bytes thanks to Erik the Outgolfer.

ḟ⁶ỴV€€sS€ẎsS€

Try it online!

Leaky Nun

Posted 2017-08-19T04:55:47.097

Reputation: 45 011

1Save another one by getting rid of Z: ḟ⁶ỴV€€sS€ẎsS€ (or ḟ⁶ỴV€€sS€FsS€) – Erik the Outgolfer – 2017-08-19T13:33:30.700

5

Python 2, 155 150 147 123 121 120 bytes

Could probably be golfed quite a bit

Edit: -5 bytes by using a better method for removing the whitespaces

Edit: -3 bytes thanks to @Leaky Nun

Edit: -24 bytes by not removing whitespaces

Edit: -2 bytes by exploiting precedence

lambda n,a:[sum(sum(map(int,b[j*n:][:n]))for b in a.split("\n")[i*n:][:n])for i in range(3)for j in range(~i%2,i%2*2+2)]

Try it online!

Halvard Hummel

Posted 2017-08-19T04:55:47.097

Reputation: 3 131

4

05AB1E, 16 bytes

¹|ðм€Sôεø¹ô€OO}˜

Try it online!

Erik the Outgolfer

Posted 2017-08-19T04:55:47.097

Reputation: 38 134

3

Octave, 64 59 54 bytes

@(c,n)sum(im2col(c'-48,[n n],'distinct'))([2 5:8 10])

Try it online!

Previous answer:

@(c,n)sparse(kron((1:4)+[0;4;8],!!e(n)),1,c-48)([2 5:8 10])

Try it online!

Returns an array as output.

rahnema1

Posted 2017-08-19T04:55:47.097

Reputation: 5 435

Not what I expected, but perfectly valid, and to be honest I didn't expect any answers at all. +1 – MD XF – 2017-08-19T05:29:11.407

@MDXF What did you expect? – rahnema1 – 2017-08-19T05:43:43.640

I didn't expect this short of an answer, nor this form of accepting strings. But it's just how Octave does it; I've never used Octave. – MD XF – 2017-08-19T14:04:37.070

3

Husk, 15 bytes

3 s and 2 ms

mṁṁiṁoC⁰TC⁰mf±¶

Try it online!

Explanation

                  Takes input as two arguments, the first being n, the second, the cube
              ¶   Split second argument into a list of lines
           m      For each line
            f±    keep only the digits (remove spaces)
         C⁰       Cut into lists of length n
    ṁ             Map then concatenate
        T           transpose
     oC⁰            then cut into lists of length n
mṁṁi              Takes list of lists of strings (or, in Husk, a list of lists of lists of chars) and returns the sum of the digits in each list
m                 Map function over list of lists
 ṁ                  map then sum
  ṁ                   map then sum
   i                  convert character to integer

H.PWiz

Posted 2017-08-19T04:55:47.097

Reputation: 10 962

1mṁṁi is really nice! – Zgarb – 2017-08-19T11:15:12.167

2

Perl 5, 66 + 1 (-n) = 67 bytes

$j=$k<6?$k++/3:5;s/\d{3}/';$r[$j++]+='.$&=~s|.|+$&|gr/gee}{say"@r"

Try it online!

Xcali

Posted 2017-08-19T04:55:47.097

Reputation: 7 671

2

Python 2, 137 127 bytes

-10 bytes thanks to @Halvard Hummel

lambda x,n:[sum(sum(map(int,x.split('\n')[b+j][a:a+n]))for j in range(n))for a,b in[[n,0],[0,n],[n,n],[2*n,n],[3*n,n],[n,2*n]]]

Try it online!

officialaimm

Posted 2017-08-19T04:55:47.097

Reputation: 2 739

2127 – Halvard Hummel – 2017-08-19T06:05:17.957

1

Haskell, 128 bytes

s n c=filter(>=0)$map(\[x,y]->sum$map(\[v,w]->fromEnum((lines c)!!(x*n+v)!!(y*n+w))-48)$n%n)$3%4
n%m=sequence[[0..n-1],[0..m-1]]

Accepts a string with line breaks.

Leif Willerts

Posted 2017-08-19T04:55:47.097

Reputation: 1 060

1

APL (Dyalog Classic), 30 27 bytes

{+/⍎¨6(⍺*2)⍴⍉⊃,⌿3⍺⍴⍵⊂⍨⍵∊⎕D}

Shaved off 3 bytes thanks to @Adám

is n is c

Explanation

                        ⍵⊂⍨⍵∊⎕D   c partitioned by ⎕D (digits 0..9)
                    3⍺⍴           reshape into 3 by n matrix
                 ,⌿              concatenate on first axis (results in n vectors)
              ⍉⊃                  ravel transpose mix (results in a simple string with all digits in side order)
      6(⍺*2)⍴                     reshape into 6 by n squared matrix (one row per side)
 +/⍎¨                            sum rows execute each (execute will turn characters into numbers)

Try it online!

Gil

Posted 2017-08-19T04:55:47.097

Reputation: 141

Looks like 59 bytes to me. Replacing with ⎕U2286 will only add 5 bytes though. – Adám – 2017-08-22T15:42:00.860

My bad, I was playing with and without partitioned enclose and only used the byte count for Classic version. Will edit my answer to use migration level 3 :) – Gil – 2017-08-22T16:22:05.800

I just specify that it needs ⎕ML←3 which is default on many systems. – Adám – 2017-08-22T16:23:01.787

1Also, you can remove the space between 3 and . – Adám – 2017-08-22T16:23:19.427

1(6,⍺*2) → 6(⍺*2) – Adám – 2017-08-22T16:25:41.557

1IFAICT, you don't need , after as always uses its right argument in ravel order. – Adám – 2017-08-22T16:28:19.590

1

PowerShell, 236 bytes

param($n,$z)
function f($y){$y-replace' '-split'(.)'-ne''-join'+'|iex}
$a=$z-split"`n"
f $a[0..($n-1)]
$a[$n..(2*$n-1)]|%{$x="($('.'*$n))";$1,$2,$3,$4=$_-split$x-ne'';$h+=$1;$i+=$2;$j+=$3;$k+=$4}
$h,$i,$j,$k|%{f $_}
f $a[(2*$n)..(3*$n)]

Try it online!

Ooof, this is long. But, splitting and slicing of strings isn't one of PowerShell's strong suits, so I guess it's somewhat expected. Also -- So. Many. Dollars.

Takes in parameters $n and $z as the size and cube net, respectively. Then constructs a function that is used throughout. Here, we're removing spaces, splitting on each individual digit, removing the empty characters in between, joining all the characters together with a +, and then executing the resulting statement to get a number. For example, this turns "123" into 1+2+3 which when executed is 6.

The next line splits the input cube net on newlines, storing the result into array $a. We then perform the function on the first $n lines and output the top face of the cube.

For the next set, we need to splice the strings based on the cube size. So, we loop through each line, constructing $x as the appropriate regex pattern (e.g., for size $n=3 this will be "(...)"), split the string based on that pattern, again removing empty elements, and store those into four variables representing the four faces. Those are then string concatenated onto h through k.

The next line then ships h through k through the function to output the sides (left, front, right, back) of the cube.

Finally, we run the last $n lines through the function to output the bottom face of the cube.

All of the numbers are left on the pipeline, and output is implicit.

AdmBorkBork

Posted 2017-08-19T04:55:47.097

Reputation: 41 581

0

Cubically, 19 bytes

r%0@%1@%2@%3@%4@%5@

Takes the cube from STDIN and the size as a command-line argument to the interpreter. Outputs the sum of the top face, a null byte, the left face, a null byte, ... the bottom face, and a null byte.

Try it online! ...which apparently displays null bytes as some sort of whitespace on my browser.

This language wasn't made for this challenge, but the challenge was made for the language.... is it still cheating? ;)

MD XF

Posted 2017-08-19T04:55:47.097

Reputation: 11 605