12
Inspired by https://puzzling.stackexchange.com/q/626
In your adventures you arrive at a series of 7 bridges you have to cross. Underneath each bridge lives a troll. In order to cross the bridge, you must first give the troll a number of cakes as a percentage of the number of cakes you are carrying. Because these are kind trolls, they will give back a certain number of cakes to you.
At the start of each day, the local troll king sets the percent cake tax that each traveler must pay, and the troll cake refund -- the number of cakes that each troll must give back to travelers.
Your job is to calculate the minimum number of cakes needed to pass all 7 troll bridges for the given conditions on that day.
Assume:
- Two input parameters: percent cake tax (integer from 0 to 100), and troll cake refund.
- No one, not even trolls, wants a cake partially eaten by another troll. If you are left with a fraction of a cake the troll gets it.
- If a troll accepts a cake tax, but then has to give you back all of the cakes (is left with the same or fewer cakes than before), it will become angry and eat you and your cakes.
- Every troll must keep at least one complete cake.
- You can only carry a maximum of 100 cakes.
- You need to end the day where you are currently located or on the other side of all 7 bridges.
Challenge:
Write a complete program to output the minimum number of cakes to travel for the current day or zero if it's not possible to safely travel today -- you will wait to see what the numbers are tomorrow.
Input should be passed as stdin, command line arguments or file input.
Shortest code (byte count) wins.
Example:
25% cake tax, 2 troll cake refund.
start with 19 cakes
before troll 1: (19 * 0.75) = 14.25
after troll 1: (14 + 2) = 16
before troll 2: (16 * 0.75) = 12
after troll 2: (12 + 2) = 14
etc.
19 cakes -> 16 -> 14 -> 12 -> 11 -> 10 -> 9 -> 8
18 cakes -> 15 -> 13 -> 11 -> 10 -> 9 -> 8 -> 8 (rule 3)
For 18 cakes, the last troll wouldn't get to keep any cakes. Therefore, the minimum number of cakes for a 25%/2 day is 19.
input: 25 2
output: 19
Example 2:
90% cake tax, 1 troll cake refund
100 cakes -> 11 -> 2 -> 1 (rule 4)
The third troll didn't get to keep any cake. Therefore it is not possible to travel on a 90%/1 day even starting with the maximum number of cakes.
input: 90 1
output: 0
Data
Put together a quick graph of input and output values. I was surprised this wasn't "smooth" (like a bell curve or similar); there are several noticeable islands.
Data for those interested. Columns are divided into 5% intervals, rows are units of 1 cake refund intervals (excel rotated the image). You can see there can't be a refund higher than 28 cakes.
27, 17, 13, 14, 15, 18, 20, 24, 53, 66, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
47, 27, 20, 19, 19, 19, 24, 39, 48, 68, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0
67, 37, 28, 24, 23, 28, 27, 29, 50, 70, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
87, 47, 33, 29, 27, 28, 31, 44, 37, 72, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 57, 40, 34, 31, 29, 34, 34, 62, 74, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 67, 48, 39, 35, 38, 37, 49, 57, 76, 92, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 77, 53, 44, 39, 38, 47, 39, 59, 78, 94, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 87, 60, 49, 43, 39, 40, 54, 46, 80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 97, 68, 54, 47, 48, 44, 44, 71, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 73, 59, 51, 48, 47, 59, 73, 84, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 80, 64, 55, 49, 51, 49, 68, 86, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 88, 69, 59, 58, 54, 64, 70, 88, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 93, 74, 63, 58, 57, 54, 57, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 100, 79, 67, 59, 67, 69, 82, 92, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 84, 71, 68, 60, 59, 77, 94, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 89, 75, 68, 64, 74, 79, 96, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 94, 79, 69, 67, 64, 66, 98, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 99, 83, 78, 71, 79, 91, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 87, 78, 74, 69, 93, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 91, 79, 77, 84, 88, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 95, 88, 87, 74, 90, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 99, 88, 80, 89, 77, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 89, 84, 79, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 98, 87, 94, 97, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 98, 91, 84, 99, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 99, 94, 99, 86, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 97, 89, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Good point. Make it a complete program for simplicity. Input can be specified however you see fit as long as it's not hard-coded (updated challenge). – None – 2014-10-10T18:13:55.963
This is a good puzzle to brute-force in CJam. I'll do that around tomorrow – None – 2014-10-10T18:28:26.107
bah, this is why c# can't have nice things – None – 2014-10-10T18:36:49.867
Rule 2 seems to exclude the possibility of a troll receiving only a fraction of a cake, which should make assumption 4 redundant. Yet you say in example 2 that the third troll would only get 0.8 cakes. – Dennis – 2014-10-12T19:29:56.937
There are conflicts in the spec. In the
25 2
11 cake case, you give the troll 2.75 cakes and get back 2 so the troll keeps .75(+.25) and you survive. In the90 1
2 cake case, you give the troll 1.8 and get back 1 so the troll keeps .8(+.2) but you die. – TwiNight – 2014-10-13T08:46:52.267@Dennis clarified troll didn't get to keep cake; fixed in edit. – None – 2014-10-13T15:05:23.613
@TwiNight there's a rounding issue. Since there's a fraction of a cake, it's rounded in the troll's favor. From your examples, it's give 3 receive 2 and give 2 receive 1. – None – 2014-10-13T15:07:48.677
This question got me thinking. If we take the two numbers as base-101 integers and combine them (refund*101 + tax), we can get a unique number per input case. Then, once we already know the right answer per input case, all we have to do is find a hash function that maps each input to the correct output. I wrote a C program to try to brute-force find such a hash, but it was not a very sophisticated attempt. Has anyone tried to search for a hash function that fits given input->output sets? I could not find any best-practices or tips for doing this. – R.T. – 2014-10-13T17:37:20.907