Advent Challenge 3: Time to remanufacture the presents!

9

1

<< 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.

HyperNeutrino

Posted 2017-12-03T21:00:05.917

Reputation: 26 575

Have we lost the "additional 1 meter for the ribbon" in c + 6 * (x * y + y * z + z * x) + 4 * (x + y + z) – Graham – 2017-12-03T21:58:55.847

@Graham Yeah, turns out I forgot to add that in. Removing from specifications. – HyperNeutrino – 2017-12-03T22:27:22.060

@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! – HyperNeutrino – 2017-12-03T22:30:53.530

@dzaima Yes, the test cases were done last-minute, sorry about that :P I have fixed them. – HyperNeutrino – 2017-12-03T22:31:07.237

@JonathanFrech Done, thanks. (See ^ and ^^) – HyperNeutrino – 2017-12-03T22:31:14.550

6I'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. – Shaggy – 2017-12-03T22:40:28.270

@Shaggy Glad you've been enjoying it up to this point! This one was admittedly not very well thought out (given also by the fact that I made at least two internal consistency errors); it was made very last-minute. Completely understandable :) Hopefully I'll plan them out better in the future :P I should probably start writing them out the night before :P – HyperNeutrino – 2017-12-03T22:42:45.503

1To the extra close-voter after I clarified the existing commented points, what more should I clarify? – HyperNeutrino – 2017-12-04T01:26:15.143

Answers

5

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()

Arnauld

Posted 2017-12-03T21:00:05.917

Reputation: 111 334

3

Mathematica, 34 bytes

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

-10 bytes from @alephalpha

Try it online!

J42161217

Posted 2017-12-03T21:00:05.917

Reputation: 15 931

Tr[#+6#2(+##3)+6##3+4(+##2)&@@@#]& – alephalpha – 2017-12-05T08:27:49.910

2

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!

caird coinheringaahing

Posted 2017-12-03T21:00:05.917

Reputation: 13 702

2

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.

Jonathan Frech

Posted 2017-12-03T21:00:05.917

Reputation: 6 681

100 bytes if it's not required to work more than one you can shave of j=t=0 , – PrincePolka – 2017-12-04T04:19:35.090

also in the calculation you can rearrange to save one byte like so – PrincePolka – 2017-12-04T04:20:34.817

@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. – Jonathan Frech – 2017-12-04T15:56:43.097

99 bytes – PrincePolka – 2017-12-04T16:18:26.153

@PrincePolka Thanks a lot. – Jonathan Frech – 2017-12-04T16:25:01.763

93 bytes I see now that j is unnecesary, and you can just use a – PrincePolka – 2017-12-05T16:23:25.377

@PrincePolka Thanks again; counting down the array length seems way more golfy than using an incrementing index. – Jonathan Frech – 2017-12-05T16:25:59.130

2

Jelly, 25 bytes

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

Try it online!

caird coinheringaahing

Posted 2017-12-03T21:00:05.917

Reputation: 13 702

1

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

Emigna

Posted 2017-12-03T21:00:05.917

Reputation: 50 798

0

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.

KSmarts

Posted 2017-12-03T21:00:05.917

Reputation: 1 830

0

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!

Οurous

Posted 2017-12-03T21:00:05.917

Reputation: 7 916

0

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

Wernisch

Posted 2017-12-03T21:00:05.917

Reputation: 2 534

You can drop 2 bytes by transferring this to Google Sheets, and dropping the terminal )) – Taylor Scott – 2017-12-18T15:18:53.513