Post History
Perl 5, 178 bytes Another brute-force approach. Reads line of space-separated digits from stdin. If possible, prints answer to stdout and exits 1. Otherwise prints nothing and exits 0. perl -aE...
#4: Post edited
- # [Perl 5], 178 bytes
- Another brute-force approach.
- Reads line of space-separated digits from stdin.
- If possible, prints answer to stdout and exits 1. Otherwise prints nothing and exits 0.
- ```
perl -aE'sub R{for my$i(0..$#_-1){for my$j($i+1..$#_){my@r=@_;$a=splice@r,$j,1;$b=splice@r,$i,1;map{@r?R(@r,"($_)"):24eq eval&&exit!say}map{("$a$_$b","$b$_$a")}qw|+ - * /|}}}R@F'- ```
- ```
- echo 2 7 8 9 | perl -aE'
- # recursive checker
- sub R {
- # get all choices of two arguments
- for my $i (0..$#_-1) {
- for my $j ($i+1..$#_) {
- # extract them and save the remainder
- my @r = @_;
- $a = splice @r,$j,1;
- $b = splice @r,$i,1;
- # build list of expressions that apply
- # an operation to the choice
- # for each expression:
- # if there is a remainder, recurse with them
- # else expression is complete
- # so if it equals 24, print it and exit(1)
- map {
- @r ? R(@r,"($_)")
: 24 eq eval && exit!say- } map {
- ("$a$_$b","$b$_$a")
- } qw|+ - * /|
- }
- }
- }
- # digits are loaded into @F by -a option
- # invoke the checker on them
- R @F
- # if we get here, exit(0)
- '
- ```
[Try it online!][TIO-mpqkgigt]- [Perl 5]: https://www.perl.org/
[TIO-mpqkgigt]: https://tio.run/##ZZBNc5swEIbv@hVbRU1EbSyDAEEYEk6e6dXXpsNAKidkzEcQbu1x6E8vlUjTaYl02X1mP953W9nt/fGgJOxk3h86CVjlJxwjdAG9VD085l0tlULpBhIgmy/rr8lPtmIPMbp/bKo2Rm1X1v0O8EfbXStIbgAvSRYjM1IdCgVX8lj2VzHSCZgQzvDQ9A18a2oJw2jw9rxrOqhOpKTr1YpcZLZjvaEnSsqFM1HrXJ3SLkmzmOSJavflvUy7JXlaOjEp/gGlBlXentPudks1wJRkFrauXU8@g/ye7y8vjZAP2uhg6igmOclIgZeYFDrIsTU8/3hZgA2fgL0Mw7BNN@PsHn@MA76r9bWMmeuRc1@AfQOc8qNvCws53AsMCBh1bM48C3EuphJB@YIzYUAYGhAyyu2Qcd3kOFPT56ptlCqLvdRIRHPkv@76D4l3yH1FVByFPipzkRME4V9JAQst5PvRNNzXmiMWIdf1PJO71D16C63Z1W82VzsN58gL5ttFGEXvGjmfTnTU3@a/mrYvm1qNdv4b "Perl 5 – Try It Online"
- # [Perl 5], 178 bytes
- Another brute-force approach.
- Reads line of space-separated digits from stdin.
- If possible, prints answer to stdout and exits 1. Otherwise prints nothing and exits 0.
- ```
- perl -aE'sub R{for my$i(0..$#_-1){for my$j($i+1..$#_){my@r=@_;$a=splice@r,$j,1;$b=splice@r,$i,1;map{@r?R(@r,"($_)"):24eq eval&&exit say}map{("$a$_$b","$b$_$a")}qw|+ - * /|}}}R@F'
- ```
- ```
- echo 2 7 8 9 | perl -aE'
- # recursive checker
- sub R {
- # get all choices of two arguments
- for my $i (0..$#_-1) {
- for my $j ($i+1..$#_) {
- # extract them and save the remainder
- my @r = @_;
- $a = splice @r,$j,1;
- $b = splice @r,$i,1;
- # build list of expressions that apply
- # an operation to the choice
- # for each expression:
- # if there is a remainder, recurse with them
- # else expression is complete
- # so if it equals 24, print it and exit(1)
- map {
- @r ? R(@r,"($_)")
- : 24 eq eval && exit say
- } map {
- ("$a$_$b","$b$_$a")
- } qw|+ - * /|
- }
- }
- }
- # digits are loaded into @F by -a option
- # invoke the checker on them
- R @F
- # if we get here, exit(0)
- '
- ```
- [Try it online!][TIO-mpql5d1o]
- [Perl 5]: https://www.perl.org/
- [TIO-mpql5d1o]: https://tio.run/##ZZBNc5swEIbv@hU7ipqI2rIMAgRhSDl5pldf2w4DqZyQMR9BuMXj0J9eKkjbaYl02X1mP953G9UevfGkFRxU1p1aBVhnZxwhdAWd0h08Zm2ltEbJDmIgu0/bL/EPvuEPEbp/rMsmQk1bVN0B8DvmbDXEd4DXJI3QNFKfcg03qi@6mwiZBKYQLvBQdzV8rSsFwzjh/eVQt1CeSUG3mw25Splt/UFPlBQre6bWpTwnbZykEcli3RyLe5W0a/K0tiOS/wMKA8qsuSTthz01AFOSWti6dVz1DOpbdry@noUYo8NURzHJSEpyvMYkN0GGreH5@8sKGLwH/jIMwz7ZjYt7/DYO@HNlrjWZuR2F8CSwOxBU9B6TFrKF60/A59RmgrsWEkLOJZKKleByAkEwgYBTwQIuTJNtz00fy6bWusiPyiAZLpH3uus/JN8g5xVR2UtzVO4g2/eDv5J8HljI88J5uGc0hzxEjuO6U@5Qp3dXRrNj3mKucRoskesvt8sgDN80CjGfqDefiZ910xV1pUeW/QI "Perl 5 – Try It Online"
#3: Post edited
# [Perl 5], 202 bytes- Another brute-force approach.
- Reads line of space-separated digits from stdin.
- If possible, prints answer to stdout and exits 1. Otherwise prints nothing and exits 0.
- ```
perl -nE'sub R{return 24eq eval"@_"if@_<2;for my$i(0..$#_-1){for my$j($i+1..$#_){my@r=@_;$a=splice@r,$j,1;$b=splice@r,$i,1;map R(@r,"($_)")&&exit say,"$a+$b $a-$b $b-$a $a*$b $a/$b $b/$a"=~/\S+/g}}}R@F'- ```
- ```
- echo 2 7 8 9 | perl -aE'
# digits are loaded into @F by -a option- sub R {
# base case: just one argument implies a full expression# so check if it gives the correct answerreturn 24 eq eval "@_"if @_<2;# otherwise get all choices of two arguments- for my $i (0..$#_-1) {
- for my $j ($i+1..$#_) {
- # extract them and save the remainder
- my @r = @_;
- $a = splice @r,$j,1;
- $b = splice @r,$i,1;
# build list of expressions that apply an# operation to the choice# recurse with the remainder and an expression# if success, print expression and exit(1)mapR(@r,"($_)") && exit say,"$a+$b $a-$b $b-$a $a*$b $a/$b $b/$a"=~/\S+/g- }
- }
- }
R@F- # if we get here, exit(0)
- '
- ```
[Try it online!][TIO-mpp3xeaz]- [Perl 5]: https://www.perl.org/
[TIO-mpp3xeaz]: https://tio.run/##ZZBfb5swFMXf/SmuHC@CEscBA4YyKp4i7TV7XCdkOielCn@GyZQoyj76mKHVtFJbsuyffM@957SqOwbDSSvYK9mfOgVYywtOEFpAr3QPz7KrldYo20IKZPtt8z39zdbskKCn56ZqE9R2Zd3vAX@i3kZD@gB4RfJk0KcCdtdOGc0aPF/9BPVLHnGW43Kf5Z@9ZN90UF1IaW3Wa7LIqWtf39CLRUrHnah9rS5Zl2Z5QmSq22P5pLJuRV5WbkKK/0BpQCVb2FnmhS2S29heLkcTo@Y7I8YeLJdwaPoGfjS1ggWoc9mD4QitMJEOKYBIOp4FJdLc7ybCJsKIxCaBx68OO9xut122HWZJvUWCH2sT49jhfuA8EEAfgFv8HFBhI5f74QhCZrmUM99GnIvpi7C4w5kYQRSNIGIWpxHjpsh1p6IvVdtoXRZHZZCI5yh47fUOiQ/Ie0WWOAsTPfOQG4bRv5FCFtkoCOJJPDAzxyxGnuf749uzvLPvmJk9s2a6xmk0R3447y6iOP5QyPkU0dlsyv80bV82tR6o/As "Perl 5 – Try It Online"
- # [Perl 5], 178 bytes
- Another brute-force approach.
- Reads line of space-separated digits from stdin.
- If possible, prints answer to stdout and exits 1. Otherwise prints nothing and exits 0.
- ```
- perl -aE'sub R{for my$i(0..$#_-1){for my$j($i+1..$#_){my@r=@_;$a=splice@r,$j,1;$b=splice@r,$i,1;map{@r?R(@r,"($_)"):24eq eval&&exit!say}map{("$a$_$b","$b$_$a")}qw|+ - * /|}}}R@F'
- ```
- ```
- echo 2 7 8 9 | perl -aE'
- # recursive checker
- sub R {
- # get all choices of two arguments
- for my $i (0..$#_-1) {
- for my $j ($i+1..$#_) {
- # extract them and save the remainder
- my @r = @_;
- $a = splice @r,$j,1;
- $b = splice @r,$i,1;
- # build list of expressions that apply
- # an operation to the choice
- # for each expression:
- # if there is a remainder, recurse with them
- # else expression is complete
- # so if it equals 24, print it and exit(1)
- map {
- @r ? R(@r,"($_)")
- : 24 eq eval && exit!say
- } map {
- ("$a$_$b","$b$_$a")
- } qw|+ - * /|
- }
- }
- }
- # digits are loaded into @F by -a option
- # invoke the checker on them
- R @F
- # if we get here, exit(0)
- '
- ```
- [Try it online!][TIO-mpqkgigt]
- [Perl 5]: https://www.perl.org/
- [TIO-mpqkgigt]: https://tio.run/##ZZBNc5swEIbv@hVbRU1EbSyDAEEYEk6e6dXXpsNAKidkzEcQbu1x6E8vlUjTaYl02X1mP953W9nt/fGgJOxk3h86CVjlJxwjdAG9VD085l0tlULpBhIgmy/rr8lPtmIPMbp/bKo2Rm1X1v0O8EfbXStIbgAvSRYjM1IdCgVX8lj2VzHSCZgQzvDQ9A18a2oJw2jw9rxrOqhOpKTr1YpcZLZjvaEnSsqFM1HrXJ3SLkmzmOSJavflvUy7JXlaOjEp/gGlBlXentPudks1wJRkFrauXU8@g/ye7y8vjZAP2uhg6igmOclIgZeYFDrIsTU8/3hZgA2fgL0Mw7BNN@PsHn@MA76r9bWMmeuRc1@AfQOc8qNvCws53AsMCBh1bM48C3EuphJB@YIzYUAYGhAyyu2Qcd3kOFPT56ptlCqLvdRIRHPkv@76D4l3yH1FVByFPipzkRME4V9JAQst5PvRNNzXmiMWIdf1PJO71D16C63Z1W82VzsN58gL5ttFGEXvGjmfTnTU3@a/mrYvm1qNdv4b "Perl 5 – Try It Online"
#2: Post edited
# [Perl 5], 206 bytes- Another brute-force approach.
- Reads line of space-separated digits from stdin.
- If possible, prints answer to stdout and exits 1. Otherwise prints nothing and exits 0.
- ```
perl -aE'sub R{return 24eq eval"@_"if@_<2;for my$i(0..$#_-1){for my$j($i+1..$#_){my@r=@_;my$x=splice@r,$j,1;my$y=splice@r,$i,1;map R(@r,"($_)")&&exit say,"$x+$y $x-$y $y-$x $x*$y $x/$y $y/$x"=~/\S+/g}}}R@F'- ```
- ```
- echo 2 7 8 9 | perl -aE'
- # digits are loaded into @F by -a option
- sub R {
- # base case: just one argument implies a full expression
- # so check if it gives the correct answer
- return 24 eq eval "@_"
- if @_<2;
- # otherwise get all choices of two arguments
- for my $i (0..$#_-1) {
- for my $j ($i+1..$#_) {
- # extract them and save the remainder
- my @r = @_;
my $x = splice @r,$j,1;my $y = splice @r,$i,1;- # build list of expressions that apply an
- # operation to the choice
- # recurse with the remainder and an expression
- # if success, print expression and exit(1)
- map
- R(@r,"($_)") && exit say,
"$x+$y $x-$y $y-$x $x*$y $x/$y $y/$x"=~/\S+/g- }
- }
- }
- R@F
- # if we get here, exit(0)
- '
- ```
[Try it online!][TIO-mpp25grx]- [Perl 5]: https://www.perl.org/
[TIO-mpp25grx]: https://tio.run/##ZZBRb5swFIXf/SuuHC8yI46DDRjGUvEUaa/Z4zoh2jkpVQIMkwkUZT99zNBqWqktWfYn33PvObVuTsFwMRoOOm8vjQZs8h4nCC2g1aaFp7wptTEo3cEWyO7b5vv2N1/zY4Ien6pznaC6Kcr2APgDExsD2zvAK5Ilg7k8wP7aaKtZgvD1T9C/8hNOM1wc0uyzSA5VA@eeFHSzXpNFxjzn@oqeKSlcb6LO9dynzTbNEsu7ralPxaNOmxV5Xnkj6v9DxYjyGvbUvjAlmYOd5XI0Muq@MWMtwnIJx6qt4EdValiA7ooWLEdohUnnkh5Ix8azZ6Sz948T4RPhpMM2hfuvLj/ebrd9uhtmab3Ggu9LG@XY4dMgZaCA3YGksguYcpAn/XAEIacek9x3kJRq@qKodCVXI4iiEUScShZxaYs8byr6cq4rY4qHk7ZIxXMUvPR6g9Q7JF4QVZ2y8XOBvDCM/o0U8shBQRBP4oGdOeYxEsL3x7egovNdO7Owa6ZrnUZz5Ifz7iqK43eFUk4RdXYz@aeq26IqzcDyvw "Perl 5 – Try It Online"
- # [Perl 5], 202 bytes
- Another brute-force approach.
- Reads line of space-separated digits from stdin.
- If possible, prints answer to stdout and exits 1. Otherwise prints nothing and exits 0.
- ```
- perl -nE'sub R{return 24eq eval"@_"if@_<2;for my$i(0..$#_-1){for my$j($i+1..$#_){my@r=@_;$a=splice@r,$j,1;$b=splice@r,$i,1;map R(@r,"($_)")&&exit say,"$a+$b $a-$b $b-$a $a*$b $a/$b $b/$a"=~/\S+/g}}}R@F'
- ```
- ```
- echo 2 7 8 9 | perl -aE'
- # digits are loaded into @F by -a option
- sub R {
- # base case: just one argument implies a full expression
- # so check if it gives the correct answer
- return 24 eq eval "@_"
- if @_<2;
- # otherwise get all choices of two arguments
- for my $i (0..$#_-1) {
- for my $j ($i+1..$#_) {
- # extract them and save the remainder
- my @r = @_;
- $a = splice @r,$j,1;
- $b = splice @r,$i,1;
- # build list of expressions that apply an
- # operation to the choice
- # recurse with the remainder and an expression
- # if success, print expression and exit(1)
- map
- R(@r,"($_)") && exit say,
- "$a+$b $a-$b $b-$a $a*$b $a/$b $b/$a"=~/\S+/g
- }
- }
- }
- R@F
- # if we get here, exit(0)
- '
- ```
- [Try it online!][TIO-mpp3xeaz]
- [Perl 5]: https://www.perl.org/
- [TIO-mpp3xeaz]: https://tio.run/##ZZBfb5swFMXf/SmuHC@CEscBA4YyKp4i7TV7XCdkOielCn@GyZQoyj76mKHVtFJbsuyffM@957SqOwbDSSvYK9mfOgVYywtOEFpAr3QPz7KrldYo20IKZPtt8z39zdbskKCn56ZqE9R2Zd3vAX@i3kZD@gB4RfJk0KcCdtdOGc0aPF/9BPVLHnGW43Kf5Z@9ZN90UF1IaW3Wa7LIqWtf39CLRUrHnah9rS5Zl2Z5QmSq22P5pLJuRV5WbkKK/0BpQCVb2FnmhS2S29heLkcTo@Y7I8YeLJdwaPoGfjS1ggWoc9mD4QitMJEOKYBIOp4FJdLc7ybCJsKIxCaBx68OO9xut122HWZJvUWCH2sT49jhfuA8EEAfgFv8HFBhI5f74QhCZrmUM99GnIvpi7C4w5kYQRSNIGIWpxHjpsh1p6IvVdtoXRZHZZCI5yh47fUOiQ/Ie0WWOAsTPfOQG4bRv5FCFtkoCOJJPDAzxyxGnuf749uzvLPvmJk9s2a6xmk0R3447y6iOP5QyPkU0dlsyv80bV82tR6o/As "Perl 5 – Try It Online"
#1: Initial revision
# [Perl 5], 206 bytes
Another brute-force approach.
Reads line of space-separated digits from stdin.
If possible, prints answer to stdout and exits 1. Otherwise prints nothing and exits 0.
```
perl -aE'sub R{return 24eq eval"@_"if@_<2;for my$i(0..$#_-1){for my$j($i+1..$#_){my@r=@_;my$x=splice@r,$j,1;my$y=splice@r,$i,1;map R(@r,"($_)")&&exit say,"$x+$y $x-$y $y-$x $x*$y $x/$y $y/$x"=~/\S+/g}}}R@F'
```
```
echo 2 7 8 9 | perl -aE'
# digits are loaded into @F by -a option
sub R {
# base case: just one argument implies a full expression
# so check if it gives the correct answer
return 24 eq eval "@_"
if @_<2;
# otherwise get all choices of two arguments
for my $i (0..$#_-1) {
for my $j ($i+1..$#_) {
# extract them and save the remainder
my @r = @_;
my $x = splice @r,$j,1;
my $y = splice @r,$i,1;
# build list of expressions that apply an
# operation to the choice
# recurse with the remainder and an expression
# if success, print expression and exit(1)
map
R(@r,"($_)") && exit say,
"$x+$y $x-$y $y-$x $x*$y $x/$y $y/$x"=~/\S+/g
}
}
}
R@F
# if we get here, exit(0)
'
```
[Try it online!][TIO-mpp25grx]
[Perl 5]: https://www.perl.org/
[TIO-mpp25grx]: https://tio.run/##ZZBRb5swFIXf/SuuHC8yI46DDRjGUvEUaa/Z4zoh2jkpVQIMkwkUZT99zNBqWqktWfYn33PvObVuTsFwMRoOOm8vjQZs8h4nCC2g1aaFp7wptTEo3cEWyO7b5vv2N1/zY4Ien6pznaC6Kcr2APgDExsD2zvAK5Ilg7k8wP7aaKtZgvD1T9C/8hNOM1wc0uyzSA5VA@eeFHSzXpNFxjzn@oqeKSlcb6LO9dynzTbNEsu7ralPxaNOmxV5Xnkj6v9DxYjyGvbUvjAlmYOd5XI0Muq@MWMtwnIJx6qt4EdValiA7ooWLEdohUnnkh5Ix8azZ6Sz948T4RPhpMM2hfuvLj/ebrd9uhtmab3Ggu9LG@XY4dMgZaCA3YGksguYcpAn/XAEIacek9x3kJRq@qKodCVXI4iiEUScShZxaYs8byr6cq4rY4qHk7ZIxXMUvPR6g9Q7JF4QVZ2y8XOBvDCM/o0U8shBQRBP4oGdOeYxEsL3x7egovNdO7Owa6ZrnUZz5Ifz7iqK43eFUk4RdXYz@aeq26IqzcDyvw "Perl 5 – Try It Online"
