r/dailyprogrammer • u/Godspiral 3 3 • Feb 10 '16
[2016-02-10] Challenge #253 [Intermediate] Countdown (numbers game)
Countdown is a British ripoff of a French TV show where given 6 starting numbers, the 4 basic arithmetic operators are used to manipulate the given 6 numbers to arrive at a given total.
It's just the first count down (tedudu do)
A simplified ruleset is to test for solutions that don't require parentheses on one side of an operator, and no operator precedence. All of the problems here have such an exact solution.
sample input
50 8 3 7 2 10 makes 556
sample output
((((50 - 7) × 3) + 10) × 8) ÷ 2
= 556
challenge input
25 50 75 100 3 6 makes 952
(You may also simplify the solution by assuming - and ÷ are only applied in one direction/order)
Must shout a second count down
RPN notation and a mini stack language can permit parenthesized group operations without using parentheses
1 5 100 5 - × 9 - 10 + +
= 477
equivalent to: 1+(((5×(100-5))-9)+10)
challenge: Allow for parenthesized grouped operations or RPN formatted expressions in determining solution.
Its the final count down
Use either program 1 or 2 to test which target totals from 0 to 1000 cannot be obtained by combining the 4 basic operators, or alternatively, find the lower target total that fails for the input:
25 50 75 100 3 6
3
u/Godspiral 3 3 Feb 10 '16
in J, solution#1
(J has no operator precedence and evaluates right to left. So similar to the autoparentheses pattern for challenge #1). the
~adverb swaps ("revereses") the operator parameters.4
u/__MadHatter Feb 10 '16 edited Feb 11 '16
In C. First countdown only. Runs the sample and challenge inputs "instantly" despite having no smart algorithm. The algorithm only tests random numbers and operators. Written purposely with almost zero effort for documentation/legibility. Comments/criticism/questions are welcome.
Output without parenthesis. An example calculation for 1 + 2 * 3 / 4 is considered to be (((1 + 2) * 3) / 4) in the following output:
Edit (2016/02/11): fixed an output bug that would display bogus extra numbers from previous attempts if a solution did not use all 6 numbers (array printing issue). Fixed version e.g.
Edit (2016/02/11)#2: fixed bugs that showed up with input
100 25 8 3 1 1 makes 984.3
u/Specter_Terrasbane Feb 10 '16 edited Feb 10 '16
Python 2.7 (First & Final Countdowns, no Second)
EDIT: Bah! Didn't read the linked rules closely enough ... "At no intermediate step in the process can the current running total become negative or involve a fraction." ... fix in progress. :)
EDIT 2: Fixed! Gave me my first excuse to use Python's
while ... elsesyntax, and simplified my Final Countdown solution. :)Output
2
u/KeinBaum Feb 10 '16
Scala, first and second count down
I made small alterations to the challenge:
The program generates all possible (valid and invalid) RPNs and checks the result of the valid ones. Since the search space is fairly small it's quite fast.
Sample output for input
50 8 3 7 2 10 55616
u/[deleted] Feb 10 '16
Too funny! I'm the author of the blog page http://datagenetics.com/blog/august42014/index.html and just saw a small spike in traffic to this (older) page and investigated the referring URL to here. It's great to read the impressive coded solutions posted here. //ick
4
u/JereTheJuggler Feb 10 '16 edited Feb 10 '16
Solutions for first countdown and final countdown in Java. Uses a recursive method to check for solutions with all combinations of numbers and operations that never produce fractions or negative numbers.
The recursive method builds up an expression adding 1 number and 1 operation each time. It saves time by not trying anything after it finds that what it has built up so far results in a fraction or negative number. So if it builds up to "25+100" and the next bit that it adds is "/50", it realizes that is not a whole number and does not continue with "25+100/50" effectively skipping every possible combination of numbers and operators that start with "25+100/50". This is extremely important because it allows the program to skip a massive amount of trials if the very first operation is not a positive integer.
Here is the output:
4
u/Syrak Feb 11 '16
Haskell, all three challenges, no intermediate fractions.
To enumerate all possibilities: choose an operator at the root of the expression, and for every way to partition the numbers to go to the left and to the right of it, recursively enumerate the possible operands.
To avoid duplicates due to the commutativity of + and ×, I instead separately add the flipped version of the operators - and /, and when partitioning, every subset of the numbers appears at most once, either to the left or to the right of the chosen operator. This can be done by putting all subsets containing the first element to the right for instance.
Example output :
Code :
1
u/Specter_Terrasbane Feb 10 '16
Quick nitpick: Your sample output for "first count down" is missing a left parenthesis.
1
u/heap42 Feb 10 '16
Can someone explain an algorithm in words?
2
u/fibonacci__ 1 0 Feb 10 '16 edited Feb 10 '16
Python
Output
2
u/hutsboR 3 0 Feb 10 '16
Elixir: I am proud of how unreadable and terrible this is.
Usage:
2
u/CleverError Feb 10 '16 edited Feb 10 '16
In Swift, Solution #1
Output
2
u/whaleway Feb 11 '16
Solutions using Common Lisp with no loops. I'm pretty new at writing Lisp so my implementation seems a bit slow. I don't think I did anything fancy but I did use inspiration from the other comments. Feedback would be appreciated.
Code
Outputs
2
u/WeAreAllApes Feb 12 '16 edited Feb 12 '16
I finally found time for one of these!
This is my first Python script ever (and I think my first submission here), so any feedback is appreciated. I have a lot to learn about this language.
I implemented one solution finder, interpreting the first challenge as equivalent to limiting the RPN stack to 2 numbers.
Code
That last step one is slow, probably because of the unlimited stack, but it might be searching unnecessary branches, too. It definitely shows some solutions that can't be written without a deeper stack / parentheses, so there are fewer impossible targets with the full rule set. I am going to look up an example and confirm this explanation.
Output
Edit: So 242 == 25 75 3 * + 100 50 / - 6 - = 25 + (75 * 3) - (100 / 50) - 6 is an example of one that can't be done on a calculator without a stack or memory. You have to compute both 75 * 3 and 100 / 50 before combining them.
Edit2: Clearly, (((75 + 6) * 100) + 50) / 25 == 326, so I think we have different interpretations here. My understanding was that not all the numbers had to be used.
2
u/gabyjunior 1 2 Feb 12 '16 edited Feb 12 '16
C, backtracking on each eligible operation, applying restrictions given in "full rules and ideas" link. The program can manage different size of number set. Search done with a linked list, the first number used in the operation is removed from the list and the result is stored in the second number.
It makes a complete scan of the search space, finding all exact solutions or the nearest ones.
The output gives the list of operations for each solution, not in the format required for first/second countdown, more like a human would give a solution.
Final countdown done using a shell-script that calls the C program in a loop for each target value from 101 to 1000.
C program
End of output
Shell-script for the final countdown
Output
1
u/Diesbert_Dinkelkraut Feb 11 '16 edited Feb 12 '16
Python 3.5.1
I'm not that pleased with my approach, but I would love to get some feedback. I was trying to use a classic search algorithm (AI class inspired) but the amount of nodes that were created led to some RecursionErrors.
The sample example works tho due to the code terminating after finding the best solution with 4 digits and 3 operators.
Any tips are welcome!
Output for the sample example:
1
u/JulianDeclercq Feb 20 '16
First count down C++ solution
1
u/jmccaffrey42 Mar 29 '16
I didn't see a golang solution yet, this isn't the best but I'm learning Go and I always try these challenges to learn a new language.
Output:
Code:
0
u/[deleted] Feb 10 '16
[deleted] — view deleted comment