13
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ May we assume the input to include all trailing spaces so as to be a rectangle? \$\endgroup\$ – fireflame241 Aug 19 '17 at 5:00
  • \$\begingroup\$ @fireflame241 if you mean n spaces after every line, no. They are not included in the dump. \$\endgroup\$ – MD XF Aug 19 '17 at 5:02
  • 1
    \$\begingroup\$ We really need a "cubically" tag. \$\endgroup\$ – Mr. Xcoder Aug 19 '17 at 10:56
  • \$\begingroup\$ @Mr.Xcoder status completed :) I'd been thinking that too, and I'm working on four more Cubically challenges ATM. \$\endgroup\$ – MD XF Aug 19 '17 at 14:03

11 Answers 11

6
\$\begingroup\$

Jelly, 16 14 13 bytes

3 bytes thanks to Erik the Outgolfer.

ḟ⁶ỴV€€sS€ẎsS€

Try it online!

|improve this answer|||||
\$\endgroup\$
  • 1
    \$\begingroup\$ Save another one by getting rid of Z: ḟ⁶ỴV€€sS€ẎsS€ (or ḟ⁶ỴV€€sS€FsS€) \$\endgroup\$ – Erik the Outgolfer Aug 19 '17 at 13:33
5
\$\begingroup\$

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!

|improve this answer|||||
\$\endgroup\$
4
\$\begingroup\$

05AB1E, 16 bytes

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

Try it online!

|improve this answer|||||
\$\endgroup\$
3
\$\begingroup\$

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
|improve this answer|||||
\$\endgroup\$
  • 1
    \$\begingroup\$ mṁṁi is really nice! \$\endgroup\$ – Zgarb Aug 19 '17 at 11:15
3
\$\begingroup\$

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.

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Not what I expected, but perfectly valid, and to be honest I didn't expect any answers at all. +1 \$\endgroup\$ – MD XF Aug 19 '17 at 5:29
  • \$\begingroup\$ @MDXF What did you expect? \$\endgroup\$ – rahnema1 Aug 19 '17 at 5:43
  • \$\begingroup\$ 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. \$\endgroup\$ – MD XF Aug 19 '17 at 14:04
2
\$\begingroup\$

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!

|improve this answer|||||
\$\endgroup\$
2
\$\begingroup\$

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!

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

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.

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

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.

|improve this answer|||||
\$\endgroup\$
1
\$\begingroup\$

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!

|improve this answer|||||
\$\endgroup\$
  • \$\begingroup\$ Looks like 59 bytes to me. Replacing with ⎕U2286 will only add 5 bytes though. \$\endgroup\$ – Adám Aug 22 '17 at 15:42
  • \$\begingroup\$ 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 :) \$\endgroup\$ – Gil Aug 22 '17 at 16:22
  • 1
    \$\begingroup\$ Also, you can remove the space between 3 and . \$\endgroup\$ – Adám Aug 22 '17 at 16:23
  • 1
    \$\begingroup\$ (6,⍺*2) → 6(⍺*2) \$\endgroup\$ – Adám Aug 22 '17 at 16:25
  • 1
    \$\begingroup\$ IFAICT, you don't need , after as always uses its right argument in ravel order. \$\endgroup\$ – Adám Aug 22 '17 at 16:28
0
\$\begingroup\$

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? ;)

|improve this answer|||||
\$\endgroup\$

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.