9
\$\begingroup\$

<< Prev Next >>

Unfortunately, Santa was not able to catch the elves in time! He has to go back to manufacturing presents now. Since the elves are definitely not Santa's slaves, he has to figure out the expenses for how much to pay them.

Challenge

Given some information for the presents, determine the cost of manufacturing all of them.

Each present is put in a cardboard box and wrapped with wrapping paper, with a ribbon wrapped around it at the very end. The wrapping paper is magical and requires no overlap, so the amount of wrapping paper used is precisely equivalent to the surface area of the box. All presents are rectangular prisms because that way Santa can store them more compactly. The ribbon goes around in all three directions (so the length of ribbon used for wrapping is equal to the sum of the three different perimeters).

The present itself has a known cost, fortunately. Cardboard costs $1 per square meter, and wrapping paper costs $2 per square meter. (Hint: You can just multiply the surface area by 3 :P). Ribbon costs $1 per meter.

Format Specifications

The input will be given as a list of presents where each present contains the cost of the actual item and the three dimensions of the present box. Your output should be the total cost required.

To be exact, the formula for the cost of a single present with item cost c and dimensions x, y, and z is c + 6 * (x * y + y * z + z * x) + 4 * (x + y + z).

Test Cases

[[7, 8, 6, 7], [7, 7, 5, 5], [8, 9, 6, 7], [6, 5, 10, 10], [5, 9, 6, 7], [9, 9, 10, 6], [8, 10, 10, 6], [6, 5, 7, 9], [7, 10, 8, 8], [5, 9, 9, 10]] -> 11866
[[5, 10, 8, 9], [8, 8, 5, 8], [8, 7, 7, 6], [5, 9, 9, 10], [9, 7, 5, 8], [9, 8, 9, 5], [7, 5, 6, 7], [5, 7, 6, 10]] -> 8854
[[9, 8, 8, 8], [10, 9, 8, 5], [10, 7, 5, 5], [10, 10, 6, 6], [8, 5, 8, 7]] -> 4853
[[7, 7, 8, 10], [8, 10, 7, 8], [9, 7, 7, 8], [8, 5, 10, 5], [6, 6, 6, 8], [8, 9, 7, 5], [8, 5, 6, 5], [7, 9, 8, 5], [10, 10, 10, 8]] -> 9717
[[5, 8, 9, 7], [5, 8, 7, 10], [5, 7, 7, 6], [5, 5, 5, 6], [9, 9, 5, 7], [5, 6, 7, 8], [8, 5, 8, 7], [6, 9, 5, 5], [10, 10, 9, 10]] -> 9418
[[9, 9, 7, 10], [5, 8, 7, 9], [5, 5, 9, 8], [10, 5, 9, 10], [8, 5, 10, 7], [8, 9, 5, 5], [5, 10, 6, 10]] -> 8178
[[5, 9, 5, 8], [7, 8, 10, 6], [7, 10, 7, 10], [8, 9, 7, 5], [5, 7, 8, 6], [9, 9, 6, 10], [6, 5, 9, 9], [7, 9, 9, 9]] -> 9766
[[7, 10, 5, 10], [8, 10, 8, 9], [8, 6, 7, 8], [6, 9, 8, 5], [6, 7, 10, 9], [7, 6, 5, 8]] -> 7118
[[10, 6, 7, 5], [5, 9, 5, 9], [9, 7, 8, 5], [6, 6, 9, 9], [9, 9, 6, 9], [10, 5, 8, 9], [7, 5, 6, 10], [9, 10, 5, 5]] -> 8007
[[8, 10, 7, 8], [9, 10, 5, 8], [6, 7, 5, 6], [10, 10, 9, 8], [7, 5, 8, 9], [10, 10, 6, 7], [10, 8, 9, 10], [5, 10, 5, 5]] -> 9331

Rules

  • Standard Loopholes Apply
  • The input and output may be given and presented in any reasonable format
  • You must take the input as a list of presents, not 4 lists of the attributes.
  • This is a , so the shortest answer in bytes wins
  • No answers will be accepted

Hopefully this challenge is easier than the previous ones :P

Note: I drew inspiration for this challenge series from Advent Of Code. I have no affiliation with this site

You can see a list of all challenges in the series by looking at the 'Linked' section of the first challenge here.

\$\endgroup\$
  • \$\begingroup\$ Have we lost the "additional 1 meter for the ribbon" in c + 6 * (x * y + y * z + z * x) + 4 * (x + y + z) \$\endgroup\$ – Graham Dec 3 '17 at 21:58
  • \$\begingroup\$ @Graham Yeah, turns out I forgot to add that in. Removing from specifications. \$\endgroup\$ – HyperNeutrino Dec 3 '17 at 22:27
  • \$\begingroup\$ @cairdcoinheringaahing Sorry for the confusion. I decided to stick with the original idea and I have edited my test cases to reflect that as well. Thanks! \$\endgroup\$ – HyperNeutrino Dec 3 '17 at 22:30
  • 6
    \$\begingroup\$ I've been enjoying this series of challenges but (admittedly, after a good few beers!) this one just seems like which language can execute the closed formula in the fewest bytes without any room for creative golfing so, in this instance, no +1 from me. \$\endgroup\$ – Shaggy Dec 3 '17 at 22:40
  • 1
    \$\begingroup\$ To the extra close-voter after I clarified the existing commented points, what more should I clarify? \$\endgroup\$ – HyperNeutrino Dec 4 '17 at 1:26
5
\$\begingroup\$

JavaScript (ES6), 58 bytes

a=>a.reduce((p,[c,x,y,z])=>p+c+6*(y*z+x*(y+=z))+4*(x+y),0)

Test cases

let f =

a=>a.reduce((p,[c,x,y,z])=>p+c+6*(y*z+x*(y+=z))+4*(x+y),0)

console.log(f([[7, 8, 6, 7], [7, 7, 5, 5], [8, 9, 6, 7], [6, 5, 10, 10], [5, 9, 6, 7], [9, 9, 10, 6], [8, 10, 10, 6], [6, 5, 7, 9], [7, 10, 8, 8], [5, 9, 9, 10]])) // 11866
console.log(f([[5, 10, 8, 9], [8, 8, 5, 8], [8, 7, 7, 6], [5, 9, 9, 10], [9, 7, 5, 8], [9, 8, 9, 5], [7, 5, 6, 7], [5, 7, 6, 10]])) // 8854
console.log(f([[9, 8, 8, 8], [10, 9, 8, 5], [10, 7, 5, 5], [10, 10, 6, 6], [8, 5, 8, 7]])) // 4853
console.log(f([[7, 7, 8, 10], [8, 10, 7, 8], [9, 7, 7, 8], [8, 5, 10, 5], [6, 6, 6, 8], [8, 9, 7, 5], [8, 5, 6, 5], [7, 9, 8, 5], [10, 10, 10, 8]])) // 9717
console.log(f([[5, 8, 9, 7], [5, 8, 7, 10], [5, 7, 7, 6], [5, 5, 5, 6], [9, 9, 5, 7], [5, 6, 7, 8], [8, 5, 8, 7], [6, 9, 5, 5], [10, 10, 9, 10]])) // 9418
console.log(f([[9, 9, 7, 10], [5, 8, 7, 9], [5, 5, 9, 8], [10, 5, 9, 10], [8, 5, 10, 7], [8, 9, 5, 5], [5, 10, 6, 10]])) // 8178
console.log(f([[5, 9, 5, 8], [7, 8, 10, 6], [7, 10, 7, 10], [8, 9, 7, 5], [5, 7, 8, 6], [9, 9, 6, 10], [6, 5, 9, 9], [7, 9, 9, 9]])) // 9766
console.log(f([[7, 10, 5, 10], [8, 10, 8, 9], [8, 6, 7, 8], [6, 9, 8, 5], [6, 7, 10, 9], [7, 6, 5, 8]])) // 7118
console.log(f([[10, 6, 7, 5], [5, 9, 5, 9], [9, 7, 8, 5], [6, 6, 9, 9], [9, 9, 6, 9], [10, 5, 8, 9], [7, 5, 6, 10], [9, 10, 5, 5]])) // 8007
console.log(f([[8, 10, 7, 8], [9, 10, 5, 8], [6, 7, 5, 6], [10, 10, 9, 8], [7, 5, 8, 9], [10, 10, 6, 7], [10, 8, 9, 10], [5, 10, 5, 5]])) // 9331

How?

The only trick used here is to factorize (xy + xz) as x(y + z) and re-use the sum (y + z) in the last part of the formula.

a => a.reduce(                    // for each present in a:
  (s, [c, x, y, z]) =>            //   s = sum, [c, x, y, z] = present parameters
    s +                           //   add to s:
    c +                           //     c
    6 * (y * z + x * (y += z)) +  //     6(yz + x(y + z))
    4 * (x + y),                  //     4(x + (y + z))
  0                               //   initial sum = 0
)                                 // end of reduce()
\$\endgroup\$
3
\$\begingroup\$

Mathematica, 34 bytes

Tr[#+6#2(+##3)+6##3+4(+##2)&@@@#]&  

-10 bytes from @alephalpha

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Tr[#+6#2(+##3)+6##3+4(+##2)&@@@#]& \$\endgroup\$ – alephalpha Dec 5 '17 at 8:27
2
\$\begingroup\$

Jelly, 25 bytes

Ḣɓ;1ị$$ṡ2P€S×6+⁸S×4¤+
Ç€S

Try it online!

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

Python 3, 56 bytes

lambda*a:sum(c+(6*x+4)*(y+z)+6*y*z+4*x for(c,x,y,z)in a)

Try it online!

  • -2 bytes thanks to Mr. Xcoder!
  • -15 bytes thanks to notjagan!
  • -1 byte thanks to Alix Eisenhardt!
\$\endgroup\$
2
\$\begingroup\$

C (gcc), 104 100 99 93 bytes

t,x,y,z;f(A,a)int*A;{for(t=0;a--;)t+=*A+++6*((x=*A++)*(y=*A++)+(z=*A++)*(x+=y))+4*(x+z);t=t;}

Try it online!

Takes a list of present attributes (list length divisible by four) and an integer specifying the number of presents. Returns the cost of manufacturing all presents.

\$\endgroup\$
  • \$\begingroup\$ 100 bytes if it's not required to work more than one you can shave of j=t=0 , \$\endgroup\$ – PrincePolka Dec 4 '17 at 4:19
  • \$\begingroup\$ also in the calculation you can rearrange to save one byte like so \$\endgroup\$ – PrincePolka Dec 4 '17 at 4:20
  • \$\begingroup\$ @PrincePolka Thank you. Per consensus, a function has to work multiple times, so j=t=0 has to stay. I could not quite figure out how to rearrange the calculation to save a byte; it would help if you linked to a complete version of the code with your golf implemented. \$\endgroup\$ – Jonathan Frech Dec 4 '17 at 15:56
  • \$\begingroup\$ 99 bytes \$\endgroup\$ – PrincePolka Dec 4 '17 at 16:18
  • \$\begingroup\$ @PrincePolka Thanks a lot. \$\endgroup\$ – Jonathan Frech Dec 4 '17 at 16:25
1
\$\begingroup\$

05AB1E, 17 bytes

vyćsO4*y¦æ2ùPO6*O

Try it online!

Explanation

v                  # for each present y
 yć                # extract the head (cost)
   s               # swap the dimensions to the top
    O4*            # sum and multiply by 4
       y¦          # push y with the head (cost) removed
         æ         # compute the powerset
          2ù       # keep only elements of length 2
            PO     # product and sum
              6*   # multiply by 6
                O  # sum everything
\$\endgroup\$
0
\$\begingroup\$

Pyth, 39 bytes

u+G++hH*6++*@H1@H2*@H1@H3*@H2@H3*4stHQ0

Try it online!

Takes input as a string representation of a nested list and sums over the cost formula.

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

Clean, 64 bytes

import StdEnv
f l=sum[c+6*(x*y+y*z+z*x)+4*(x+y+z)\\[c,x,y,z]<-l]

Try it online!

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

Excel, 60 bytes

Input taken from Columns A to D, new row per present. Formula in any other column.

=SUMPRODUCT(A:A+6*(B:B*C:C+C:C*D:D+B:B*D:D)+4*(B:B+C:C+D:D))
\$\endgroup\$
  • \$\begingroup\$ You can drop 2 bytes by transferring this to Google Sheets, and dropping the terminal )) \$\endgroup\$ – Taylor Scott Dec 18 '17 at 15:18

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.