A higher resolution is required to access the IDE
- 8
Learning Opportunities
This puzzle can be solved using the following concepts. Practice using these concepts and improve your skills.
Statement
Goal
In the 24 Game, a given set of four numbers must be combined using only the arithmetic operationshttps://www.codingame.com/training/medium/24-game by @Smelty
For this puzzle, your program must provide the number of possible solutions (ns) and all those possible solutions, or
Solutions must be printed as expressions without spaces, for example
EXPRESSIONS, TERMS AND SUBEXPRESSIONS
To understand the rules for rewriting expressions, we must distinguish expressions, terms and subexpressions.
expression: Terms separated by mathematical operators that can be evaluated from left to right. For example:
term: A constant or a subexpression. The expression
subexpression: A subexpression is an expression that must be evaluated before the parent expression can be evaluated. The expression
REWRITING EXPRESSIONS
Only rewrite an expression such that it remains mathematically the same.
Rewrite expressions according to the following three rules:
1. Use as few parentheses as possible.
- Remove unnecessary parentheses:
- Rewrite an expression, if necessary:
2. Sort operators:
- Change the order of terms such that for their connecting operators
- Do this within a parent expression:
- Do this within a subexpression:
- This does not apply to operators in two different subexpressions: in
3. For cases not decided by rule 2, sort terms of expressions and subexpressions
a) Sort constants in ascending order:
b) Place constants before subexpressions:
c) Sort two subexpressions according to their numerical value:
In the case of equal value, sort the subexpressions as strings, based on the ASCII value of characters:
EXAMPLE
With values 2,2,3,5, there are 8 non-unique solutions, but after applying above described rules, only one unique solution remains.
SORTING MULTIPLE UNIQUE SOLUTIONS
After applying above rules, you might still end up with multiple solutions. Output them in the following order:
- Solutions without parentheses (
- Solutions with one set of parentheses (
- Solutions with two sets of parentheses (
Multiple solutions within the same category should be sorted as strings, based on the ASCII value of the characters.
Credits to @Timinator for co-authoring the goal statement and some of the test cases
Input
Line 1: four space separated integers a, b, c, d
Output
Line 1: not possible or number of unique solutions (ns)
Next ns lines: all unique solutions as expressions without spaces
Next ns lines: all unique solutions as expressions without spaces
Constraints
0 <= a, b, c, d <= 100
Example
Input
5 3 2 2
Output
1 3*(2*5-2)
A higher resolution is required to access the IDE