Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Comments on 24 game automated solver

Parent

24 game automated solver

+2
−0

The goal is to write a program, whose byte size is minimized, that inputs four integers, not necessarily distinct, chosen from $1, 2, 3, ..., 9$ and then determines if it is possible to combine the inputted numbers using only the operations of addition, subtraction, multiplication and division to form a mathematical expression whose value is $24$. Parentheses are allowed.

If it is possible, the program displays an expression whose value is $24$.

Otherwise, the program displays the word "Impossible", false, 0 or something similar; the choice is yours as long as you are consistent.

By "display", I mean any output (such as displaying on the screen or the return value of a function).

Clarifications:

You may choose any input format you like as long as you use the same format for all test cases.

You may choose any output format you like for expressions such as normal mathematical notation OR Reverse Polish Notation OR …

The expression you form must contain each inputted number exactly once.

You may only use the four arithmetic operations mentioned above, so no square roots, no factorials, …

Although subtraction is allowed, the use of a negative sign is not allowed. Similarly, negation of a number or expression is not allowed.

Decimal points are not allowed.

Assume everything is done in base $10$.

Concatenation in any form is not allowed.

Test cases:

There may be multiple ways for the inputted numbers to form $24$. Your program only needs to display one such expression, not necessarily the ones listed below.

3357 -> 3(3x5-7)
1346 -> 6/(1-3/4)
3377 -> 7(3+3/7)
3388 -> 8/(3-8/3)
1116 -> Impossible
1179 -> Impossible
1557 -> Impossible
1577 -> Impossible
1277 -> (7x7-1)/2
1668 -> 6/(1-6/8)
5599 -> 5x5-9/9
2244 -> 2(2x4+4)
2222 -> Impossible
3358 -> Impossible
3467 -> Impossible
7899 -> Impossible
3333 -> 3x3x3-3


If you wish you can try my 24 Game solver available here:

https://openprocessing.org/sketch/2927650

You interact with my program with finger taps or mouse clicks.

History

2 comment threads

Output format (6 comments)
Display (2 comments)
Post
+2
−0

Perl 5, 228 bytes

Brute-force check.

Reads line of space-separated digits from stdin. Prints answer to stdout.

If empty output is permitted as "impossible", omit final say to save 10 bytes.

perl -aE'map{@o=/./g;for$f("15(26(374))","15((263)74)","(15(263))74","(152)6(374)","((152)63)74"){map{$_=sprintf$f=~s/\d/%$&\$s/gr,@F[@o],/./g;24-eval||exit!say}glob"{+,-,\\*,/}"x 3}}grep!/(.).*\1/,glob"{0,1,2,3}"x 4;say"false"'
echo 1 2 3 4 | perl -aE '
    # 0. the four numbers are loaded into @F by -a option

    # 2. loop over each permutation of offsets
    map {
        @o = /./g; # temp var as $_ is overwritten in inner map

        # 3. loop over the five ways to group operations
        #    $f is a compressed format string for sprintf
        for $f (
            "15(26(374))",
            "15((263)74)",
            "(15(263))74",
            "(152)6(374)",
            "((152)63)74"
        ){
            # 5. loop over each operator combination
            map {

                # 6. expand the format string
                #    substitute in the numbers and operators
                #    save resulting expression as $_ (for eval/say)
                $_ =
                    sprintf
                        $f =~ s/\d/%$&\$s/gr,
                        @F[@o], /./g
                ;

                # 7. execute the expression
                #    if result is 24, print expression and exit
                24-eval || exit !say

              # 4. generate all combinations of three operators
            } glob "{+,-,\\*,/}" x 3
        }

      # 1. generate all permutations of the offsets into @F
    } grep !/(.).*\1/, glob "{0,1,2,3}" x 4;

    # 8. if we get here, no solution was found
    say "false"
'

Try it online!

History

1 comment thread

Review (3 comments)
Review

Using your Try it online! facility, I confirmed that your code worked correctly for the test cases I gave in my post. I also tried some additional test cases and your code worked correctly for all these other test cases. Congratulations!

I am not fluent in Perl so I have no comment on your source code.

In your post you describe the input as space-separated digits but your code acts as if it requires the digits to be connected (no spaces).

jhnc‭ wrote 4 months ago

you would run the code in the post with space-separated input as something like echo 1 2 3 4 | perl -aE '...'. The -a option causes autosplitting into array @F.

However, the tio.run version is slightly modified (eg. doesn't use -E so has to enable the say feature; overrides the exit; and has header/footer that wraps code to allow parsing the test cases in the format you provided: @F = $F[0]=~/./g then replaces @F with the first field re-split into individual characters )

Okay, thanks for the explanation.