r/dailyprogrammer • u/Garth5689 • Mar 28 '18
[2018-03-28] Challenge #355 [Intermediate] Possible Number of Pies
Description
It's Thanksgiving eve and you're expecting guests over for dinner tomorrow. Unfortunately, you were browsing memes all day and cannot go outside to buy the ingredients needed to make your famous pies. You find some spare ingredients, and make do with what you have. You know only two pie recipes, and they are as follows:
Pumpkin Pie
- 1 scoop of synthetic pumpkin flavouring (Hey you're a programmer not a cook)
- 3 eggs
- 4 cups of milk
- 3 cups of sugar
Apple Pie
- 1 apple
- 4 eggs
- 3 cups of milk
- 2 cups of sugar
Your guests have no preference of one pie over another, and you want to make the maximum number of (any kind) of pies possible with what you have. You cannot bake fractions of a pie, and cannot use fractions of an ingredient (So no 1/2 cup of sugar or anything like that)
Input Format
You will be given a string of 4 numbers separated by a comma, such as 10,14,10,42,24. Each number is a non-negative integer. The numbers represent the number of synthetic pumpkin flavouring, apples, eggs, milk and sugar you have (In the units represented in the recipes).
For instance, in the example input 10,14,10,42,24, it would mean that you have
- 10 scoops of synthetic pumpkin flavouring
- 14 apples
- 10 eggs
- 42 cups of milk
- 24 cups of sugar
Output Format
Display the number of each type of pie you will need to bake. For the example input, an output would be
3 pumpkin pies and 0 apple pies
Challenge Inputs
10,14,10,42,24
12,4,40,30,40
12,14,20,42,24
Challenge Outputs
3 pumpkin pies and 0 apple pies
5 pumpkin pies and 3 apple pies
5 pumpkin pies and 1 apple pies
Hint
Credit
This challenge was suggested by user /u/Gavin_Song, many thanks! If you have an idea for a challenge please share it on /r/dailyprogrammer_ideas and there's a good chance we'll use it.
65
u/lukz 2 0 Mar 28 '18 edited Apr 23 '18
Game boy assembly
The input is included inside the program. After the program runs and the game boy halts, the solution can be read at memory locations FFFD and FFFE. Value at FFFD gives the number of pumpkin pies, value at FFFE gives the number of apple pies. Verified in the BGB emulator.
Screenshot
Program size is 120 bytes, including input.
Code:
Program outputs:
6
u/[deleted] Mar 30 '18
Java
The problem itself is an Integer Linear Programming problem. My solution uses the given total number of ingredients, and the two given recipes to define the constraints of the problem (including pumpkin pies >= 0 & apple pies >= 0).
The solution then relaxes the problem to a typical Linear Programming problem, finding the intercepts between each constraint as floats. In a typical Linear Programming problem, one would investigate each of the feasible intercepts to find the optimal solution.
The solution then uses these precise intercepts to produce 4 potential integer roundings (ceil and floor for x and y), and investigates each of these rounded outcomes for feasibility and if (x + y) for these outcomes exceeds the current optimal solution.
The solution then returns the first discovered optimal solution, which may be different from the designated challenge output, but the totals remain the same.
At this moment, the solution only accepts two pie recipes, but the recipe definitions can be changed.
Code:
Output:
8
u/[deleted] Mar 28 '18
I wrote a greedy algorithm that makes Pumpkin until it can't anymore, then makes the remaining possible number of Apple, then starts over and tries the reverse (Apple til you can't, then Pumpkin), and takes whichever result yielded more pies. Got the following for the three challenge inputs:
3 Pumpkin, 0 Apple
4 Pumpkin, 4 Apple
6 Pumpkin, 0 Apple
which look like valid answers, but I don't think it'd work in all cases. Can anyone come up with a case where that wouldn't work?
8
u/aranciokov Mar 28 '18
Minizinc model.
Data file.
Output challenges:
Pumpkin pies: 3, Apple pies: 0
Pumpkin pies: 4, Apple pies: 4
Pumpkin pies: 6, Apple pies: 0
4
u/chunes 1 2 Mar 29 '18 edited Mar 29 '18
A linear programming solution in Factor.
I spent all day pulling my hair out, researching linear programming and systems of equations, but it was fun! One thing I'm still unsure about is if there's a clever way to narrow down the feasible region.
Currently, what I am doing is finding the feasible region by testing each intersection against all inequalities, and then applying the objective function to each corner of the feasible region.
It spits out only one answer, because there's only one optimum in the feasible region. I'm guessing that's because it's the one that uses the least ingredients or something like that.
Output:
1
u/octolanceae Apr 02 '18
What constitutes correct optimization for this challenge?
For example, the second challenge input (12,4,40,30,40) has an answer of 5,3. However, if you do the math out by hand, graph the whole thing, etc, you get an answer of 4,4 - this is the combination that uses the least amount of ingredients (84). 6,2 used the most amount of ingredients (86). 5,3 is the only solution for this that doesn't consume all of any one ingredient (4,4 consumes all apple, 6,2 consumes all milk)
Is that the correct optimization? Which ever uses the fewest amount of ingredients and doesn't consume the entirety of any one ingredient? (in the 3rd challenge input, 4,2 uses fewer ingredients than 5,1 but consumes all eggs)
3
u/[deleted] Mar 28 '18 edited Mar 28 '18
I threw together a very simple to understand 10 line Python 3.6 solution using itertools.combinations. Takes a few seconds to run on the last challenge input(let's hope you don't have a ton of pumpkin packets and apples sitting around). However, it works for all inputs.
I computed all of the possible subsets of apple and pumpkin pies. Starting from the largest subset, going to the smallest, I check to see if that subset of pies actually has the quantity of ingredients necessary to produce it. If so, then it's an answer.
Feedback appreciated
3
u/mesalocal Mar 28 '18 edited Mar 28 '18
C++
To get all possible combinations, copy and paste the for loop in maxPies underneath the current for loop you just copied. Now move the cout from main into the if statement in the for loop you just created.
3
u/Arakhai Mar 29 '18
Python 3.6
Simple approach checking which type of pie offers the most repetitions, making one, and checking again until no more of either type can be made. It gets slightly different numbers of each type to the examples, but the same total numbers, and it handles /u/gs44's example correctly.
1
u/congratz_its_a_bunny Mar 28 '18
is there a reason for 10 14 10 42 24 you couldn't do 2 pumpkin and 1 apple?
total you would use 2 scoops of pumpkin flavor, 1 apple, 10 eggs, 11 cups of milk, and 8 cups of sugar, which is allowable given the number of ingredients you have.
2
u/bala_eth Mar 30 '18
We can do an optimized brute force as follows:
Python code:
2
u/gabyjunior 1 2 Mar 30 '18 edited Mar 30 '18
C
Solves the problem recursively and recipe by recipe, selecting at each step the one that has the least combinations possible.
Prints the new maximum found on the fly.
Sample input (the program is flexible on recipes and ingredients)
Finds different solutions than those posted in challenge descriptions but they seem ok.
Also tested /u/gs44 input
2
u/nikit9999 Mar 30 '18 edited Apr 02 '18
C# works with challenge inputs. Criticism is welcome.(need some additional comparison for edgy cases)
2
u/TheoreticallySpooked Apr 01 '18
Ruby
Pretty greedy and arbitrary. (But works!)
2
u/InSs4444nE Apr 01 '18 edited May 07 '18
Java
reading about immutable classes, so i played around with one
Output
2
u/___gg Apr 08 '18
F# My first submission here. It's a simple solution that doesn't use linear programming. It calculates the maximum number of pumpkin pies first. After that it calculates how many apple pies can be made for all possible numbers of pumpkin pies (0..maxPumpkins)
2
u/macgillebride Mar 28 '18
I'm not sure if it's a problem just with me, but the link in your hint doesn't work. Also, it seems the problem is an instance of integer linear programming, which is NP complete unlike linear programming
2
u/skeeto -9 8 Mar 28 '18 edited Mar 28 '18
C, just trying every possible solution recursively. My code is generalized to allow for more kinds of pies and ingredients.
2
u/gs44 1 0 Mar 28 '18 edited Mar 28 '18
Julia/JuMP, generalized to any kind of recipe/ingredients
output :
1
u/LegendK95 Mar 28 '18
Haskell Bruteforce solution - didn't give it much thought.
1
u/kevin_1994 Mar 29 '18
Python 3, simple recursive solution
Using python is so easy it feels like cheating. It's so nice without keeping track of which pie is which, I'm new to python... can anyone think of a really simple way to do it?
1
u/Tetsumi- 1 0 Mar 29 '18
Racket. Greedy.
1
u/benmandude Mar 29 '18
C++ recursive solution. I don't use recursion often so if anyone has some advice, I would love to hear it.
1
u/trowmeaway3 Mar 29 '18
Nim
1
u/zatoichi49 Mar 30 '18 edited Mar 30 '18
Method:
Create numpy arrays for the recipes and the total ingredients. Calculate how many pies of one-type can be made. For each n pies (from 0 to n), take the ingredients remaining at that point and calculate how many of the other type of pie can be made from this. Store each result (n, baked) in a list and sort by the sum of each tuple. The sum of the last element in the list will give the optimal total, so return this (pumpkin, apple) tuple. If multiple tuples have the same sum, return the combination that uses the least amount of ingredients.
Python 3:
Output:
1
u/HereBehindMyWall Mar 31 '18 edited Mar 31 '18
So here's a Node solution which doesn't do any trial and error (well, except a little tiny bit at the end).
1
u/[deleted] Mar 31 '18
Hello,
this is my first post and I solved it (I think so) with python 3.6
Am I allowed to use packages (imports) in these challenges?
1
u/[deleted] Mar 31 '18 edited Mar 31 '18
Python 3.6, feedback appreciated.
INPUT
OUTPUT
1
u/daffy_duck233 Apr 04 '18 edited Apr 06 '18
R
Reasoning:
Code:
1
u/ff8c00 Apr 04 '18
JavaScript Gist
1
u/asya_su Apr 04 '18 edited Apr 04 '18
c# so this is the first challenge I've done, it works but its kinda unnecessarily long. code:
using System;
namespace ConsoleApp6 { class Program { static void Main(string[] args) { string[] ingred = Console.ReadLine().Split(','); int pumpkin = Convert.ToInt32(ingred[0]); int apple = Convert.ToInt32(ingred[1]); int eggs = Convert.ToInt32(ingred[2]); int milk = Convert.ToInt32(ingred[3]); int sugar = Convert.ToInt32(ingred[4]); int pumpPie = 0; int appPie = 0; if (pumpkin <= apple && pumpkin <= eggs / 3 && pumpkin <= milk / 3 && pumpkin <= sugar / 2) { while (pumpkin > 0 && eggs > 2 && milk > 3 && sugar > 3) { pumpkin--; eggs = eggs - 3; milk = milk - 4; sugar = sugar - 3; pumpPie++; } while (apple > 0 && eggs > 3 && milk > 2 && sugar > 1) { apple--; eggs = eggs - 4; milk = milk - 3; sugar = sugar - 2; appPie++; } } else if (apple <= eggs / 3 && apple <= milk / 3 && apple <= sugar / 2) { while (apple > 0 && eggs > 3 && milk > 2 && sugar > 1) { apple--; eggs = eggs - 4; milk = milk - 3; sugar = sugar - 2; appPie++; } while (pumpkin > 0 && eggs > 2 && milk > 3 && sugar > 3) { pumpkin--; eggs = eggs - 3; milk = milk - 4; sugar = sugar - 3; pumpPie++; }
}
1
u/biowoulf Apr 04 '18
Hey guys can someone tell me if it is okey? Outputs are fine but still not sure.
1
u/[deleted] Apr 05 '18
(kinda verbose) OOP typescript solution
1
u/[deleted] Apr 05 '18 edited Apr 06 '18
Quick solution in Python 3. First calculates the maximum number of apple pies you can bake, then checks for each possibility how many pumpkin pies are possible.
EDIT: and here's the same solution in rust, because I need to practice:
1
u/shepherdjay Apr 06 '18 edited Apr 06 '18
Python 3.6
This is my first time participating in these challenges. I figured since I don't do this for a living it would give me an opportunity to work on my tests and code. Boy was this one more challenging than I expected. I initially researched linear programming and tried using scipy.optimize but couldn't get rounded integers. Not sure if it is even capable.
So instead I decided to brute force it, the solution is here:
https://github.com/shepherdjay/reddit_challenges/blob/master/challenges/challenge355.py
It does favor solutions that make a more even number of pies. 2,1 and 4,2 on the first and third output. As such I had to change my tests to match the total expected pies instead of the exact output, hoping that is still in the spirit as I couldn't figure out how to search for that exact solution space.
1
u/[deleted] Apr 07 '18 edited Apr 07 '18
F# No linear programming (I think??)
Took me way longer than usual, I really need to get back into this. I believe it is generalized enough to use any number/type of ingredients and any number of defined pies.
My solutions operates using an optimized brute force:
My solution uses operator overloading to make things look nice.
Output:
1
u/Zapakitu Apr 09 '18
C
I would appreciate feedback.
I'm basically creating the first pie as many times as possible and the secound one with the remainings. After, I save the output and do the same thing, but the other way around (create pie2 as many times as possible and pie1 with the remainings). After, I compare the results and select the method that made more pies.
1
u/fabikw Apr 09 '18
R
1
u/JKaps9 Apr 13 '18
Java. Would appreciate some feedback. I basically went brute force here trying every combination starting from the most of each type of pie down to 0 of each type.
1
u/VoteNixon2016 Apr 13 '18
Matlab woohoo
This gives me the same total number of pies, but in different proportions than what the challenge lists as the results.
Output:
1
u/primaryobjects Apr 13 '18
Excel
Gist | Screenshot
I solved this with an integer optimization model in Excel, using the OpenSolver add-in. Constraints are the amount of ingredient required for each type of pie multiplied by the number of pies to bake, which must be less than or equal to the amount of ingredient available. Objective is to have at least 1 pie.
Output
1
u/Gobbedyret 1 0 Apr 14 '18
Julia 0.6
Algorithm:
Find the maximal number of apple pies by looking at each ingredient and determining how many apple pies could be made from that. The minimum of each ingredient is the maximal number of apple pies.
For each potential number of apple pies from 0 to the maximal found above, calculate the materials remaining after baking that number of apple pies. Using the same method as above, then calculate the maximal number of pumpkin pies.
For each element in the previous loop, count the total number of pies. When loop is done, find the maximal number of pies.
Length: 11 lines
Speed: < 100 µs
Memory consumption: ~5 KB
1
u/5900 Apr 17 '18
Haskell
Brute force solution.
1
u/skawid Apr 17 '18
Rust. Not linear programming because that made my brain creak.
1
u/Th3D0ct0r Apr 17 '18
Python 2.7
Took me a couple days to get it right, but I finally did. I definitely learned some very useful Python constructs (which is my goal of doing these) such as lambda functions (which I never really understood until now, if even only partially), list iteration/construction (used in my 'maxpies' function), and the 'operator' library.
1
u/crigger61 Apr 18 '18
Python3.6
Im really late to the party but I wrote my solution up for this. I feel like I am doing something massively wrong as everyone seems be be looking into linear programming and other detailed optimization methods, but my code runs without any delay, recursively, and seems to always output the right solution. I would love feedback to see if I am missing something, or a case that proves my code isn't working right or optimally.
1
u/all_ofv Apr 25 '18
Lua 5.3
1
u/GreySkiesPinkShoes Apr 29 '18
Using python and GLPK.
1
u/TotesMessenger Apr 30 '18
I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:
If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / ^Contact)
1
u/BSISJ7 May 13 '18
Java 8
1
u/2kofawsome Jun 29 '18
python3.6
Different combos of course, but same totals.
1
u/zqvt Mar 28 '18 edited Mar 28 '18
Clojure constraint programming with
locoresults
1
u/congratz_its_a_bunny Mar 28 '18
I find multiple solutions for all the inputs
Python
Yes it's brute force over many suboptimal combinations. It could be more efficient. BUT unless we're talking about thousands of pies, this should be fast enough.
output:
0
u/[deleted] Mar 28 '18
[deleted] — view deleted comment
0
u/[deleted] Apr 05 '18
[deleted] — view deleted comment