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

Post History

66%
+2 −0
Challenges 24 game automated solver

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. per...

posted 4mo ago by jhnc‭  ·  edited 4mo ago by jhnc‭

Answer
#6: Post edited by user avatar jhnc‭ · 2026-05-26T05:31:16Z (4 months ago)
shorter success check
  • # [Perl 5], 235 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.
  • ```none
  • 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;if(24eq eval){say;exit}}glob"{+,-,\\*,/}"x 3}}grep!/(.).*\1/,glob"{0,1,2,3}"x 4;say"false"'
  • ```
  • ```bash
  • perl -aE '
  • # 0. four numbers have been loaded into @F by -a option
  • # 2. loop over each permutation of offsets
  • map {
  • @o = /./g; # temp var since $_ is reused in nested map
  • # 3. loop over the five ways to group operations
  • # $f is a compacted 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 $_
  • $_ =
  • sprintf
  • $f =~ s/\d/%$&\$s/gr,
  • @F[@o], /./g
  • ;
  • # 7. execute the expression
  • # if result is 24, print expression and exit
  • if (24 eq eval) {
  • say;
  • exit
  • }
  • # 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!][TIO-mpkf6ntj]
  • [Perl 5]: https://www.perl.org/
  • [TIO-mpkf6ntj]: https://tio.run/##ZZDRbpswFIbv/RSeSyu7MXHwAQxCVLmKtGcoVUU6kyKRwDCZmKL00cdsmCY1ta/@z/855z/udN9E09loXOlyOPcaE1P@JhlC2x3Osbd73rzkH2ItDhl6e2@PXYa6vj4NFSb3vtwYnD9hwr3XDJnzHlf4Mh3L7rJt87mkanuvoiSIqIwpqJAxwp2yEpiVVtH5EZiVi5JssTq1SGcl7OIae6@5@Tffq/IPI4of4t57KDwjDj3f7p637QufR9cVlaH@ifWvsmEXu5Nd6Q7bQHjQZsDvZX/SxqBe26VP@A7rsR4Qul4PTbsnlxX3eVE8cnElIwZLe919E3TN1o9FIPhi2vCASw7OEmbu16qyMZpMV/RQZRNApLD/hIHCGPmKoQDC2IFY0MAHETIEoGaLorACoRxIEgcSQcFPBNiiIJiLvh@71ph632iLVHqLomXWJ6S@ILkgqkblB0xIFMRx8j9SLBKGoiidm0c2cypSJGUYOi2pHMOVzSztuelrN01uURjfTldJmn4pBJi/aLTXhz9tN9TtyUx@@Rc "Perl 5 – Try It Online"
  • # [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.
  • ```none
  • 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"'
  • ```
  • ```bash
  • 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!][TIO-mpm6o7le]
  • [Perl 5]: https://www.perl.org/
  • [TIO-mpm6o7le]: https://tio.run/##ZZDRbpswFIbv/RQuo6vd2HGwAcMQUS66SLvZHqBUFdkMjUQCwmRiovTRl9kwTRo1N/4/zvnP79OotgquF61gofLu0iro6PyXkwDwAXZKd/Alb89Ka7DbwxS6@8fNU/rG1qxMwPeX@tQkoGmP566Azi3lGw3TLXSI@5wAa6kvBw3vVH/s7hJgBLRXOMCy7mr48O3rZzhOeLie8mbY1elkXNStWyDHCxAPkZA@xg6xykiBjTQKTT8FNnJWHM@lVs3Sljp4sMbuc6r/pnSL9E2z7Ae7dT9mrmZlS3b7x139RKbR3KfqZ169vtqgN2YRY1nVB2dYEUqy7J6w0emhGMeyVc0NQ2u8vs88RuaiDfEIJ8KW@IldYpFXWjnXxSJHukU4Afb5n65CBBLSLRRI9AGVGHjCDy0IGfKoYD4GQsipRCKxEkxaEEUWRAwJGjFhmjxvavpyamqtj4dKGSTjJQrmWf8h@Q7xGSHZS@phxoEXhtG/SCGLMAiCeDIPTOaYxYBz37eaI977K5OZm7PwNS@NlsgPl9NlFMfvGoWYVtSbj4rfddMd67O@0vwP "Perl 5 – Try It Online"
#5: Post edited by user avatar jhnc‭ · 2026-05-24T23:59:59Z (4 months ago)
less efficient but much shorter permutation generation
  • # [Perl 5], 327 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.
  • ```none
  • perl -aE'@o=0..3;for(;;){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;if(24eq eval){say;exit}}glob"{+,-,\\*,/}"x 3}$i=$#o;--$i while$o[$i-1]>$o[$i];$j=$i or last;push@o,reverse splice@o,$i;++$j while$o[$i-1]>$o[$j];@o[$i-1,$j]=@o[$j,$i-1]}say"false"'
  • ```
  • ```bash
  • perl -aE'
  • # Fischer-Krause ordered permutation generator (cf. perlfaq4)
  • # 0. four numbers have been loaded into @F
  • # initialise list of offsets into @F that will be permuted
  • @o = 0..3;
  • # 1. loop over each permutation
  • for (;;) {
  • # 2. loop over the five ways to group operations
  • # $f is a compacted format string for sprintf
  • for $f (
  • "15(26(374))",
  • "15((263)74)",
  • "(15(263))74",
  • "(152)6(374)",
  • "((152)63)74"
  • ){
  • # 4. loop over each operator combination
  • map {
  • # 5. expand the format string
  • # substitute in the digits and operators
  • # save resulting expression as $_
  • $_ =
  • sprintf
  • $f =~ s/\d/%$&\$s/gr,
  • @F[@o], /./g
  • ;
  • # 6. execute the expression
  • # if result is 24, print expression and exit
  • if (24 eq eval) {
  • say;
  • exit
  • }
  • # 3. generate all combinations of three operators
  • } glob "{+,-,\\*,/}" x 3
  • }
  • # 7. permute the offsets
  • $i = $#o;
  • --$i while $o[$i-1]>$o[$i];
  • $j = $i or last;
  • push @o, reverse splice @o, $i;
  • ++$j while $o[$i-1]>$o[$j];
  • @o[$i-1,$j] = @o[$j,$i-1]
  • }
  • # 8. if we get here, no solution was found
  • say "false"
  • ```
  • [Try it online!][TIO-mpke96xq]
  • [Perl 5]: https://www.perl.org/
  • [TIO-mpke96xq]: https://tio.run/##bZFdb5swFIbv/Sssx63sBccB8ymLiKtK@w1JVJHOJI5IYJh0TIj@9KUGpElLBxec9zkffo@pVVMG95tRsFB5e2sURCb/jSQA2QtMIX7ZrvfpB1/xowRvp@pSS1A3@toWED0xb21guoHIwa8SmNsBFrC/Z1W6Xq2ELKqGSEl7@8UFQW5AvJCIyKcUOaOyUlArrSJTUlArZ@XRuRQ5AJFZj7WI9pe87vFramYTuEg/DN/94E/4eYcNPzZO9rLNqr0zOdYF8Xz1E6r3vKS93cuutYCq0y1olF32ChfQ2oOtMi085c1VGQPAMBzL6oD6pcOc3e6bwwfUQTFgneJFJQFjWMNfJ10qXG2xZu5@MwV7ic@pTdl5ZW5aWd/MKaucRr2rxl6vqUv9pizAWi6X@PyfEee9zGbt2Dgd47MzpYfxnxR5aRS6D@C5kHchggiyDRREdAGLKHCFH44g5MRlgvsUCBFNJRERS8GjEcTxCGJOBIu5sE2uOzV9v9SVMfpQKoui5BEF81n/oOgL8mZEoi5iLuUecMMw/msp5DEFQZBMwwPrOeEJ8DzfH7VHvM5fWs@efR7m2k3jR@SHj6dHcZJ8aRRiuqLOvkz8qepWV1dzZ/kn "Perl 5 – Try It Online"
  • # [Perl 5], 235 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.
  • ```none
  • 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;if(24eq eval){say;exit}}glob"{+,-,\\*,/}"x 3}}grep!/(.).*\1/,glob"{0,1,2,3}"x 4;say"false"'
  • ```
  • ```bash
  • perl -aE '
  • # 0. four numbers have been loaded into @F by -a option
  • # 2. loop over each permutation of offsets
  • map {
  • @o = /./g; # temp var since $_ is reused in nested map
  • # 3. loop over the five ways to group operations
  • # $f is a compacted 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 $_
  • $_ =
  • sprintf
  • $f =~ s/\d/%$&\$s/gr,
  • @F[@o], /./g
  • ;
  • # 7. execute the expression
  • # if result is 24, print expression and exit
  • if (24 eq eval) {
  • say;
  • exit
  • }
  • # 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!][TIO-mpkf6ntj]
  • [Perl 5]: https://www.perl.org/
  • [TIO-mpkf6ntj]: https://tio.run/##ZZDRbpswFIbv/RSeSyu7MXHwAQxCVLmKtGcoVUU6kyKRwDCZmKL00cdsmCY1ta/@z/855z/udN9E09loXOlyOPcaE1P@JhlC2x3Osbd73rzkH2ItDhl6e2@PXYa6vj4NFSb3vtwYnD9hwr3XDJnzHlf4Mh3L7rJt87mkanuvoiSIqIwpqJAxwp2yEpiVVtH5EZiVi5JssTq1SGcl7OIae6@5@Tffq/IPI4of4t57KDwjDj3f7p637QufR9cVlaH@ifWvsmEXu5Nd6Q7bQHjQZsDvZX/SxqBe26VP@A7rsR4Qul4PTbsnlxX3eVE8cnElIwZLe919E3TN1o9FIPhi2vCASw7OEmbu16qyMZpMV/RQZRNApLD/hIHCGPmKoQDC2IFY0MAHETIEoGaLorACoRxIEgcSQcFPBNiiIJiLvh@71ph632iLVHqLomXWJ6S@ILkgqkblB0xIFMRx8j9SLBKGoiidm0c2cypSJGUYOi2pHMOVzSztuelrN01uURjfTldJmn4pBJi/aLTXhz9tN9TtyUx@@Rc "Perl 5 – Try It Online"
#4: Post edited by user avatar jhnc‭ · 2026-05-24T23:20:10Z (4 months ago)
alternate format string compaction scheme allows shorter expansion code
  • # [Perl 5], 338 bytes
  • Brute-force check.
  • Reads line of space-separated digits from stdin. Prints answer to stdout.
  • ```none
  • perl -aE'@o=0..3;for(;;){for$f("d5(d6(d7d))","d5((d6d)7d)","(d5(d6d))7d","(d5d)6(d7d)","((d5d)6d)7d"){map{$_=sprintf$f=~s/d/%d/gr=~s/\d/%$&\$s/gr,@F[@o],/./g;if(24eq eval){say;exit}}glob"{+,-,\\*,/}"x 3}$i=$#o;--$i while$o[$i-1]>$o[$i];$j=$i or last;push@o,reverse splice@o,$i;++$j while$o[$i-1]>$o[$j];@o[$i-1,$j]=@o[$j,$i-1]}say"false"'
  • ```
  • ```bash
  • perl -aE'
  • # Fischer-Krause ordered permutation generator (cf. perlfaq4)
  • # 0. four numbers have been loaded into @F
  • # initialise list of offsets into @F that will be permuted
  • @o = 0..3;
  • # 1. loop over each permutation
  • for (;;) {
  • # 2. loop over the 5 possible ways to group operations
  • # $f is a compacted format string for sprintf
  • for $f (
  • "d5(d6(d7d))",
  • "d5((d6d)7d)",
  • "(d5(d6d))7d",
  • "(d5d)6(d7d)",
  • "((d5d)6d)7d"
  • ){
  • # 4. loop over each operator combination
  • map {
  • # 5. expand the format string
  • # substitute in the digits and operators
  • # save resulting expression as $_
  • $_ =
  • sprintf
  • $f =~ s/d/%d/gr
  • =~ s/\d/%$&\$s/gr,
  • @F[@o], /./g
  • ;
  • # 6. execute the expression and test if if equals 24
  • # if it does, print it and exit
  • if (24 eq eval) {
  • say;
  • exit
  • }
  • # 3. generate all combinations of three operators
  • } glob "{+,-,\\*,/}" x 3
  • }
  • # 7. permute the offsets
  • $i = $#o;
  • --$i while $o[$i-1]>$o[$i];
  • $j = $i or last;
  • push @o, reverse splice @o, $i;
  • ++$j while $o[$i-1]>$o[$j];
  • @o[$i-1,$j] = @o[$j,$i-1]
  • }
  • # 8. if we get here, no solution was found
  • say "false"
  • '
  • ```
  • [Try it online!][TIO-mpkdcgsy]
  • [Perl 5]: https://www.perl.org/
  • [TIO-mpkdcgsy]: https://tio.run/##bZHfjqIwFMbv@xRN7UzKSq1QoJAGw5XJPoOaCQ5FMSgsxVk2hHn0dQskm6yzcMH5fudPv1Nq1ZT@464VzFXa3hsFkU5/IQlAsoUxxNvd@hB/shU7SfB@rq61BHVT3NocohfqrjWMNxDZ@E0CfT/CHPaPpIrXqxWXedUQKa3efHFOUOaTLCCZyCwL2aMyMrOMNIpMSZMR2awyay5FNkBk1mMtsvprWvf4LdazCZzHn5pl7CVjp2YM9ybGr3usjbaT7S6pDvZkvsiJ66kfUH2kpdWbFc2GC6i6ogWNMnvf4AIap7BVuoXntLkprQEYhlNZHVG/tKm933@z2YA6yAdcxHhRSUApLuDPc1EqXO1wQZ3DZgoOEl9ikzLzylS3sr7rc1LZjfpQjblpXZfFuzIAF3K5xJf/jLgcZDJr28TxGF/sKT2MvydPS63QYwCvuXxw7gtIN5AT3vlUWMDhXjCCgBGHcuZZgHMxlQjCl5yJEYThCEJGOA0ZN02OMzV9v9aV1sWxVAaJ6Bn581n/IPEFuTMiohPUsZgLnCAI/1oKWGgB34@m4b7xHLEIuK7njdolbuctjWfXPE9zzabhM/KC59NFGEVfGjmfrqgzL@W/q7otqpt@0PQP "Perl 5 – Try It Online"
  • # [Perl 5], 327 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.
  • ```none
  • perl -aE'@o=0..3;for(;;){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;if(24eq eval){say;exit}}glob"{+,-,\\*,/}"x 3}$i=$#o;--$i while$o[$i-1]>$o[$i];$j=$i or last;push@o,reverse splice@o,$i;++$j while$o[$i-1]>$o[$j];@o[$i-1,$j]=@o[$j,$i-1]}say"false"'
  • ```
  • ```bash
  • perl -aE'
  • # Fischer-Krause ordered permutation generator (cf. perlfaq4)
  • # 0. four numbers have been loaded into @F
  • # initialise list of offsets into @F that will be permuted
  • @o = 0..3;
  • # 1. loop over each permutation
  • for (;;) {
  • # 2. loop over the five ways to group operations
  • # $f is a compacted format string for sprintf
  • for $f (
  • "15(26(374))",
  • "15((263)74)",
  • "(15(263))74",
  • "(152)6(374)",
  • "((152)63)74"
  • ){
  • # 4. loop over each operator combination
  • map {
  • # 5. expand the format string
  • # substitute in the digits and operators
  • # save resulting expression as $_
  • $_ =
  • sprintf
  • $f =~ s/\d/%$&\$s/gr,
  • @F[@o], /./g
  • ;
  • # 6. execute the expression
  • # if result is 24, print expression and exit
  • if (24 eq eval) {
  • say;
  • exit
  • }
  • # 3. generate all combinations of three operators
  • } glob "{+,-,\\*,/}" x 3
  • }
  • # 7. permute the offsets
  • $i = $#o;
  • --$i while $o[$i-1]>$o[$i];
  • $j = $i or last;
  • push @o, reverse splice @o, $i;
  • ++$j while $o[$i-1]>$o[$j];
  • @o[$i-1,$j] = @o[$j,$i-1]
  • }
  • # 8. if we get here, no solution was found
  • say "false"
  • ```
  • [Try it online!][TIO-mpke96xq]
  • [Perl 5]: https://www.perl.org/
  • [TIO-mpke96xq]: https://tio.run/##bZFdb5swFIbv/Sssx63sBccB8ymLiKtK@w1JVJHOJI5IYJh0TIj@9KUGpElLBxec9zkffo@pVVMG95tRsFB5e2sURCb/jSQA2QtMIX7ZrvfpB1/xowRvp@pSS1A3@toWED0xb21guoHIwa8SmNsBFrC/Z1W6Xq2ELKqGSEl7@8UFQW5AvJCIyKcUOaOyUlArrSJTUlArZ@XRuRQ5AJFZj7WI9pe87vFramYTuEg/DN/94E/4eYcNPzZO9rLNqr0zOdYF8Xz1E6r3vKS93cuutYCq0y1olF32ChfQ2oOtMi085c1VGQPAMBzL6oD6pcOc3e6bwwfUQTFgneJFJQFjWMNfJ10qXG2xZu5@MwV7ic@pTdl5ZW5aWd/MKaucRr2rxl6vqUv9pizAWi6X@PyfEee9zGbt2Dgd47MzpYfxnxR5aRS6D@C5kHchggiyDRREdAGLKHCFH44g5MRlgvsUCBFNJRERS8GjEcTxCGJOBIu5sE2uOzV9v9SVMfpQKoui5BEF81n/oOgL8mZEoi5iLuUecMMw/msp5DEFQZBMwwPrOeEJ8DzfH7VHvM5fWs@efR7m2k3jR@SHj6dHcZJ8aRRiuqLOvkz8qepWV1dzZ/kn "Perl 5 – Try It Online"
#3: Post edited by user avatar jhnc‭ · 2026-05-24T22:52:15Z (4 months ago)
no point allowing for arbitrary length of @F when code assumes exactly 4 elements
  • # [Perl 5], 340 bytes
  • Brute-force check.
  • Reads line of space-separated digits from stdin. Prints answer to stdout.
  • ```none
  • perl -aE'@o=0..$#F;for(;;){for$f("d5(d6(d7d))","d5((d6d)7d)","(d5(d6d))7d","(d5d)6(d7d)","((d5d)6d)7d"){map{$_=sprintf$f=~s/d/%d/gr=~s/\d/%$&\$s/gr,@F[@o],/./g;if(24eq eval){say;exit}}glob"{+,-,\\*,/}"x 3}$i=$#o;--$i while$o[$i-1]>$o[$i];$j=$i or last;push@o,reverse splice@o,$i;++$j while$o[$i-1]>$o[$j];@o[$i-1,$j]=@o[$j,$i-1]}say"false"'
  • ```
  • ```bash
  • perl -aE'
  • # Fischer-Krause ordered permutation generator (cf. perlfaq4)
  • # 0. four numbers have been loaded into @F
  • # initialise list of offsets into @F that will be permuted
  • @o = 0..$#F;
  • # 1. loop over each permutation
  • for (;;) {
  • # 2. loop over the 5 possible ways to group operations
  • # $f is a compacted format string for sprintf
  • for $f (
  • "d5(d6(d7d))",
  • "d5((d6d)7d)",
  • "(d5(d6d))7d",
  • "(d5d)6(d7d)",
  • "((d5d)6d)7d"
  • ){
  • # 4. loop over each operator combination
  • map {
  • # 5. expand the format string
  • # substitute in the digits and operators
  • # save resulting expression as $_
  • $_ =
  • sprintf
  • $f =~ s/d/%d/gr
  • =~ s/\d/%$&\$s/gr,
  • @F[@o], /./g
  • ;
  • # 6. execute the expression and test if if equals 24
  • # if it does, print it and exit
  • if (24 eq eval) {
  • say;
  • exit
  • }
  • # 3. generate all combinations of three operators
  • } glob "{+,-,\\*,/}" x 3
  • }
  • # 7. permute the offsets
  • $i = $#o;
  • --$i while $o[$i-1]>$o[$i];
  • $j = $i or last;
  • push @o, reverse splice @o, $i;
  • ++$j while $o[$i-1]>$o[$j];
  • @o[$i-1,$j] = @o[$j,$i-1]
  • }
  • # 8. if we get here, no solution was found
  • say "false"
  • '
  • ```
  • [Try it online!][TIO-mpju627a]
  • [Perl 5]: https://www.perl.org/
  • [TIO-mpju627a]: https://tio.run/##bZHfjqIwFMbv@xRN7UzKSq1QoJAGw5XJPoOaCQ5FMSgsxVk2hHn0dQskm6yzcMH5fudPv1Nq1ZT@464VzFXa3hsFkU5/IQlAsoUxxNvd@hB/shU7SfB@rq61BHVT3NocohfqrjWMNxDZ@E0CfT/CHPaPpIrXqxVebGVeNURKqzdfnBOU@SQLSCYyy0L2qIzMLCONIlPSZEQ2q8yaS5ENEJn1WIus/prWPX6L9WwD5/GnZhl7ydipGcO9ifHrHmuj7WS7S6qDPdkvcuJ66gdUH2lp9WZJs@MCqq5oQaPM5je4gMYpbJVu4TltbkprAIbhVFZH1C9tau/332w2oA7yARcxXlQSUIoL@PNclApXO1xQ57CZgoPEl9ikzLwy1a2s7/qcVHajPlRj7lrXZfGuDMCFXC7x5T8jLgeZzNo2cTzGF3tKD@MPytNSK/QYwGsuH5z7AtIN5IR3PhUWcLgXjCBgxKGceRbgXEwlgvAlZ2IEYTiCkBFOQ8ZNk@NMTd@vdaV1cSyVQSJ6Rv581j9IfEHujIjoBHUs5gInCMK/lgIWWsD3o2m4bzxHLAKu63mjdonbeUvj2TXP01yzafiMvOD5dBFG0ZdGzqcr6sxL@e@qbovqph80/QM "Perl 5 – Try It Online"
  • # [Perl 5], 338 bytes
  • Brute-force check.
  • Reads line of space-separated digits from stdin. Prints answer to stdout.
  • ```none
  • perl -aE'@o=0..3;for(;;){for$f("d5(d6(d7d))","d5((d6d)7d)","(d5(d6d))7d","(d5d)6(d7d)","((d5d)6d)7d"){map{$_=sprintf$f=~s/d/%d/gr=~s/\d/%$&\$s/gr,@F[@o],/./g;if(24eq eval){say;exit}}glob"{+,-,\\*,/}"x 3}$i=$#o;--$i while$o[$i-1]>$o[$i];$j=$i or last;push@o,reverse splice@o,$i;++$j while$o[$i-1]>$o[$j];@o[$i-1,$j]=@o[$j,$i-1]}say"false"'
  • ```
  • ```bash
  • perl -aE'
  • # Fischer-Krause ordered permutation generator (cf. perlfaq4)
  • # 0. four numbers have been loaded into @F
  • # initialise list of offsets into @F that will be permuted
  • @o = 0..3;
  • # 1. loop over each permutation
  • for (;;) {
  • # 2. loop over the 5 possible ways to group operations
  • # $f is a compacted format string for sprintf
  • for $f (
  • "d5(d6(d7d))",
  • "d5((d6d)7d)",
  • "(d5(d6d))7d",
  • "(d5d)6(d7d)",
  • "((d5d)6d)7d"
  • ){
  • # 4. loop over each operator combination
  • map {
  • # 5. expand the format string
  • # substitute in the digits and operators
  • # save resulting expression as $_
  • $_ =
  • sprintf
  • $f =~ s/d/%d/gr
  • =~ s/\d/%$&\$s/gr,
  • @F[@o], /./g
  • ;
  • # 6. execute the expression and test if if equals 24
  • # if it does, print it and exit
  • if (24 eq eval) {
  • say;
  • exit
  • }
  • # 3. generate all combinations of three operators
  • } glob "{+,-,\\*,/}" x 3
  • }
  • # 7. permute the offsets
  • $i = $#o;
  • --$i while $o[$i-1]>$o[$i];
  • $j = $i or last;
  • push @o, reverse splice @o, $i;
  • ++$j while $o[$i-1]>$o[$j];
  • @o[$i-1,$j] = @o[$j,$i-1]
  • }
  • # 8. if we get here, no solution was found
  • say "false"
  • '
  • ```
  • [Try it online!][TIO-mpkdcgsy]
  • [Perl 5]: https://www.perl.org/
  • [TIO-mpkdcgsy]: https://tio.run/##bZHfjqIwFMbv@xRN7UzKSq1QoJAGw5XJPoOaCQ5FMSgsxVk2hHn0dQskm6yzcMH5fudPv1Nq1ZT@464VzFXa3hsFkU5/IQlAsoUxxNvd@hB/shU7SfB@rq61BHVT3NocohfqrjWMNxDZ@E0CfT/CHPaPpIrXqxWXedUQKa3efHFOUOaTLCCZyCwL2aMyMrOMNIpMSZMR2awyay5FNkBk1mMtsvprWvf4LdazCZzHn5pl7CVjp2YM9ybGr3usjbaT7S6pDvZkvsiJ66kfUH2kpdWbFc2GC6i6ogWNMnvf4AIap7BVuoXntLkprQEYhlNZHVG/tKm933@z2YA6yAdcxHhRSUApLuDPc1EqXO1wQZ3DZgoOEl9ikzLzylS3sr7rc1LZjfpQjblpXZfFuzIAF3K5xJf/jLgcZDJr28TxGF/sKT2MvydPS63QYwCvuXxw7gtIN5AT3vlUWMDhXjCCgBGHcuZZgHMxlQjCl5yJEYThCEJGOA0ZN02OMzV9v9aV1sWxVAaJ6Bn581n/IPEFuTMiohPUsZgLnCAI/1oKWGgB34@m4b7xHLEIuK7njdolbuctjWfXPE9zzabhM/KC59NFGEVfGjmfrqgzL@W/q7otqpt@0PQP "Perl 5 – Try It Online"
#2: Post edited by user avatar jhnc‭ · 2026-05-24T22:47:43Z (4 months ago)
some explanatory comments
  • # [Perl 5], 340 bytes
  • Brute-force check.
  • Reads line of space-separated digits from stdin. Prints answer to stdout.
  • ```none
  • perl -aE'@o=0..$#F;for(;;){for$f("d5(d6(d7d))","d5((d6d)7d)","(d5(d6d))7d","(d5d)6(d7d)","((d5d)6d)7d"){map{$_=sprintf$f=~s/d/%d/gr=~s/\d/%$&\$s/gr,@F[@o],/./g;if(24eq eval){say;exit}}glob"{+,-,\\*,/}"x 3}$i=$#o;--$i while$o[$i-1]>$o[$i];$j=$i or last;push@o,reverse splice@o,$i;++$j while$o[$i-1]>$o[$j];@o[$i-1,$j]=@o[$j,$i-1]}say"false"'
  • ```
  • ```bash
  • perl -aE'
  • # Fischer-Krause ordered permutation generator (cf. perlfaq4)
  • @o = 0..$#F;
  • for (;;) {
  • for $f (
  • "d5(d6(d7d))",
  • "d5((d6d)7d)",
  • "(d5(d6d))7d",
  • "(d5d)6(d7d)",
  • "((d5d)6d)7d"
  • ){
  • map {
  • $_ =
  • sprintf
  • $f =~ s/d/%d/gr
  • =~ s/\d/%$&\$s/gr,
  • @F[@o], /./g
  • ;
  • if (24 eq eval) {
  • say;
  • exit
  • }
  • } glob "{+,-,\\*,/}" x 3
  • }
  • $i = $#o;
  • --$i while $o[$i-1]>$o[$i];
  • $j = $i or last;
  • push @o, reverse splice @o, $i;
  • ++$j while $o[$i-1]>$o[$j];
  • @o[$i-1,$j] = @o[$j,$i-1]
  • }
  • say "false"
  • '
  • ```
  • [Try it online!][TIO-mpju627a]
  • [Perl 5]: https://www.perl.org/
  • [TIO-mpju627a]: https://tio.run/##bZHfjqIwFMbv@xRN7UzKSq1QoJAGw5XJPoOaCQ5FMSgsxVk2hHn0dQskm6yzcMH5fudPv1Nq1ZT@464VzFXa3hsFkU5/IQlAsoUxxNvd@hB/shU7SfB@rq61BHVT3NocohfqrjWMNxDZ@E0CfT/CHPaPpIrXqxVebGVeNURKqzdfnBOU@SQLSCYyy0L2qIzMLCONIlPSZEQ2q8yaS5ENEJn1WIus/prWPX6L9WwD5/GnZhl7ydipGcO9ifHrHmuj7WS7S6qDPdkvcuJ66gdUH2lp9WZJs@MCqq5oQaPM5je4gMYpbJVu4TltbkprAIbhVFZH1C9tau/332w2oA7yARcxXlQSUIoL@PNclApXO1xQ57CZgoPEl9ikzLwy1a2s7/qcVHajPlRj7lrXZfGuDMCFXC7x5T8jLgeZzNo2cTzGF3tKD@MPytNSK/QYwGsuH5z7AtIN5IR3PhUWcLgXjCBgxKGceRbgXEwlgvAlZ2IEYTiCkBFOQ8ZNk@NMTd@vdaV1cSyVQSJ6Rv581j9IfEHujIjoBHUs5gInCMK/lgIWWsD3o2m4bzxHLAKu63mjdonbeUvj2TXP01yzafiMvOD5dBFG0ZdGzqcr6sxL@e@qbovqph80/QM "Perl 5 – Try It Online"
  • # [Perl 5], 340 bytes
  • Brute-force check.
  • Reads line of space-separated digits from stdin. Prints answer to stdout.
  • ```none
  • perl -aE'@o=0..$#F;for(;;){for$f("d5(d6(d7d))","d5((d6d)7d)","(d5(d6d))7d","(d5d)6(d7d)","((d5d)6d)7d"){map{$_=sprintf$f=~s/d/%d/gr=~s/\d/%$&\$s/gr,@F[@o],/./g;if(24eq eval){say;exit}}glob"{+,-,\\*,/}"x 3}$i=$#o;--$i while$o[$i-1]>$o[$i];$j=$i or last;push@o,reverse splice@o,$i;++$j while$o[$i-1]>$o[$j];@o[$i-1,$j]=@o[$j,$i-1]}say"false"'
  • ```
  • ```bash
  • perl -aE'
  • # Fischer-Krause ordered permutation generator (cf. perlfaq4)
  • # 0. four numbers have been loaded into @F
  • # initialise list of offsets into @F that will be permuted
  • @o = 0..$#F;
  • # 1. loop over each permutation
  • for (;;) {
  • # 2. loop over the 5 possible ways to group operations
  • # $f is a compacted format string for sprintf
  • for $f (
  • "d5(d6(d7d))",
  • "d5((d6d)7d)",
  • "(d5(d6d))7d",
  • "(d5d)6(d7d)",
  • "((d5d)6d)7d"
  • ){
  • # 4. loop over each operator combination
  • map {
  • # 5. expand the format string
  • # substitute in the digits and operators
  • # save resulting expression as $_
  • $_ =
  • sprintf
  • $f =~ s/d/%d/gr
  • =~ s/\d/%$&\$s/gr,
  • @F[@o], /./g
  • ;
  • # 6. execute the expression and test if if equals 24
  • # if it does, print it and exit
  • if (24 eq eval) {
  • say;
  • exit
  • }
  • # 3. generate all combinations of three operators
  • } glob "{+,-,\\*,/}" x 3
  • }
  • # 7. permute the offsets
  • $i = $#o;
  • --$i while $o[$i-1]>$o[$i];
  • $j = $i or last;
  • push @o, reverse splice @o, $i;
  • ++$j while $o[$i-1]>$o[$j];
  • @o[$i-1,$j] = @o[$j,$i-1]
  • }
  • # 8. if we get here, no solution was found
  • say "false"
  • '
  • ```
  • [Try it online!][TIO-mpju627a]
  • [Perl 5]: https://www.perl.org/
  • [TIO-mpju627a]: https://tio.run/##bZHfjqIwFMbv@xRN7UzKSq1QoJAGw5XJPoOaCQ5FMSgsxVk2hHn0dQskm6yzcMH5fudPv1Nq1ZT@464VzFXa3hsFkU5/IQlAsoUxxNvd@hB/shU7SfB@rq61BHVT3NocohfqrjWMNxDZ@E0CfT/CHPaPpIrXqxVebGVeNURKqzdfnBOU@SQLSCYyy0L2qIzMLCONIlPSZEQ2q8yaS5ENEJn1WIus/prWPX6L9WwD5/GnZhl7ydipGcO9ifHrHmuj7WS7S6qDPdkvcuJ66gdUH2lp9WZJs@MCqq5oQaPM5je4gMYpbJVu4TltbkprAIbhVFZH1C9tau/332w2oA7yARcxXlQSUIoL@PNclApXO1xQ57CZgoPEl9ikzLwy1a2s7/qcVHajPlRj7lrXZfGuDMCFXC7x5T8jLgeZzNo2cTzGF3tKD@MPytNSK/QYwGsuH5z7AtIN5IR3PhUWcLgXjCBgxKGceRbgXEwlgvAlZ2IEYTiCkBFOQ8ZNk@NMTd@vdaV1cSyVQSJ6Rv581j9IfEHujIjoBHUs5gInCMK/lgIWWsD3o2m4bzxHLAKu63mjdonbeUvj2TXP01yzafiMvOD5dBFG0ZdGzqcr6sxL@e@qbovqph80/QM "Perl 5 – Try It Online"
#1: Initial revision by user avatar jhnc‭ · 2026-05-24T14:09:40Z (4 months ago)
# [Perl 5], 340 bytes

Brute-force check.

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

```none
perl -aE'@o=0..$#F;for(;;){for$f("d5(d6(d7d))","d5((d6d)7d)","(d5(d6d))7d","(d5d)6(d7d)","((d5d)6d)7d"){map{$_=sprintf$f=~s/d/%d/gr=~s/\d/%$&\$s/gr,@F[@o],/./g;if(24eq eval){say;exit}}glob"{+,-,\\*,/}"x 3}$i=$#o;--$i while$o[$i-1]>$o[$i];$j=$i or last;push@o,reverse splice@o,$i;++$j while$o[$i-1]>$o[$j];@o[$i-1,$j]=@o[$j,$i-1]}say"false"'
```


```bash
perl -aE'
    # Fischer-Krause ordered permutation generator (cf. perlfaq4)
    @o = 0..$#F;
    for (;;) {
        for $f (
            "d5(d6(d7d))",
            "d5((d6d)7d)",
            "(d5(d6d))7d",
            "(d5d)6(d7d)",
            "((d5d)6d)7d"
        ){
            map {
                $_ =
                    sprintf
                        $f =~ s/d/%d/gr
                           =~ s/\d/%$&\$s/gr,
                        @F[@o], /./g
                ;
                if (24 eq eval) {
                    say;
                    exit
                }
            } glob "{+,-,\\*,/}" x 3
        }
        $i = $#o;
        --$i while $o[$i-1]>$o[$i];
        $j = $i or last;
        push @o, reverse splice @o, $i;
        ++$j while $o[$i-1]>$o[$j];
        @o[$i-1,$j] = @o[$j,$i-1]
    }
    say "false"
'
```


[Try it online!][TIO-mpju627a]

[Perl 5]: https://www.perl.org/
[TIO-mpju627a]: https://tio.run/##bZHfjqIwFMbv@xRN7UzKSq1QoJAGw5XJPoOaCQ5FMSgsxVk2hHn0dQskm6yzcMH5fudPv1Nq1ZT@464VzFXa3hsFkU5/IQlAsoUxxNvd@hB/shU7SfB@rq61BHVT3NocohfqrjWMNxDZ@E0CfT/CHPaPpIrXqxVebGVeNURKqzdfnBOU@SQLSCYyy0L2qIzMLCONIlPSZEQ2q8yaS5ENEJn1WIus/prWPX6L9WwD5/GnZhl7ydipGcO9ifHrHmuj7WS7S6qDPdkvcuJ66gdUH2lp9WZJs@MCqq5oQaPM5je4gMYpbJVu4TltbkprAIbhVFZH1C9tau/332w2oA7yARcxXlQSUIoL@PNclApXO1xQ57CZgoPEl9ikzLwy1a2s7/qcVHajPlRj7lrXZfGuDMCFXC7x5T8jLgeZzNo2cTzGF3tKD@MPytNSK/QYwGsuH5z7AtIN5IR3PhUWcLgXjCBgxKGceRbgXEwlgvAlZ2IEYTiCkBFOQ8ZNk@NMTd@vdaV1cSyVQSJ6Rv581j9IfEHujIjoBHUs5gInCMK/lgIWWsD3o2m4bzxHLAKu63mjdonbeUvj2TXP01yzafiMvOD5dBFG0ZdGzqcr6sxL@e@qbovqph80/QM "Perl 5 – Try It Online"