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 -
nis an integer andcis a cube from Cubically's debugging output. So ifnwas3.0andcwasfoo bar, your program could break and still be valid. - Your program only needs to work for
n > 1andn < 1260. It may (attempt to) handle larger or smaller cube sizes, but it is not necessary.
This is code-golf, so the shortest code wins! If you need help, feel free to ask in the Cubically chatroom.
11 Answers 11
Jelly, (削除) 16 (削除ここまで) (削除) 14 (削除ここまで) 13 bytes
3 bytes thanks to Erik the Outgolfer.
ḟ6ỴV€€sS€ẎsS€
-
1\$\begingroup\$ Save another one by getting rid of
Z:ḟ6ỴV€€sS€ẎsS€(orḟ6ỴV€€sS€FsS€) \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年08月19日 13:33:30 +00:00Commented Aug 19, 2017 at 13:33
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)]
Husk, 15 bytes
3 ṁs and 2 ms
mṁṁiṁoC0TC0mf±¶
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)
C0 Cut into lists of length n
ṁ Map then concatenate
T transpose
oC0 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
-
1\$\begingroup\$
mṁṁiis really nice! \$\endgroup\$Zgarb– Zgarb2017年08月19日 11:15:12 +00:00Commented Aug 19, 2017 at 11:15
Octave, (削除) 64 (削除ここまで) (削除) 59 (削除ここまで) 54 bytes
@(c,n)sum(im2col(c'-48,[n n],'distinct'))([2 5:8 10])
Previous answer:
@(c,n)sparse(kron((1:4)+[0;4;8],!!e(n)),1,c-48)([2 5:8 10])
Returns an array as output.
-
\$\begingroup\$ Not what I expected, but perfectly valid, and to be honest I didn't expect any answers at all. +1 \$\endgroup\$MD XF– MD XF2017年08月19日 05:29:11 +00:00Commented Aug 19, 2017 at 5:29
-
\$\begingroup\$ @MDXF What did you expect? \$\endgroup\$rahnema1– rahnema12017年08月19日 05:43:43 +00:00Commented Aug 19, 2017 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– MD XF2017年08月19日 14:04:37 +00:00Commented Aug 19, 2017 at 14:04
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]]]
-
2\$\begingroup\$ 127 \$\endgroup\$anon– anon2017年08月19日 06:05:17 +00:00Commented Aug 19, 2017 at 6:05
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.
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)]
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.
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)
-
\$\begingroup\$ Looks like 59 bytes to me. Replacing
⊆with⎕U2286will only add 5 bytes though. \$\endgroup\$Adám– Adám2017年08月22日 15:42:00 +00:00Commented Aug 22, 2017 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– Gil2017年08月22日 16:22:05 +00:00Commented Aug 22, 2017 at 16:22
-
1\$\begingroup\$ Also, you can remove the space between
3and⍺. \$\endgroup\$Adám– Adám2017年08月22日 16:23:19 +00:00Commented Aug 22, 2017 at 16:23 -
1\$\begingroup\$
(6,⍺*2)→6(⍺*2)\$\endgroup\$Adám– Adám2017年08月22日 16:25:41 +00:00Commented Aug 22, 2017 at 16:25 -
1\$\begingroup\$ IFAICT, you don't need
,after⍴as⍴always uses its right argument in ravel order. \$\endgroup\$Adám– Adám2017年08月22日 16:28:19 +00:00Commented Aug 22, 2017 at 16:28
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? ;)
nspaces after every line, no. They are not included in the dump. \$\endgroup\$