How to resolve the algorithm 24 game/Solve step by step in the Quackery programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm 24 game/Solve step by step in the Quackery programming language

Table of Contents

Problem Statement

Write a program that takes four digits, either from user input or by random generation, and computes arithmetic expressions following the rules of the 24 game. Show examples of solutions generated by the program.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm 24 game/Solve step by step in the Quackery programming language

Source code in the quackery programming language

  [ ' [ 0 1 2 3 ]
    permutations ] constant          is numorders (         --> [ )

  [ []
    4 3 ** times
      [ [] i^
        3 times
          [ 4 /mod 4 +
            rot join swap ]
        drop 
        nested join ] ] constant     is oporders  (         --> [ )

  [ [] numorders witheach
      [ oporders witheach
        [ dip dup join nested
          rot swap join swap ]
        drop ] ] constant            is allorders (         --> [ )

  [ [] unrot witheach
      [ dip dup peek
        swap dip [ nested join ] ]
    drop ]                           is reorder   (     [ [ --> [ )

  [ ' [ [ 0 1 4 2 5 3 6 ]
        [ 0 1 4 2 3 5 6 ]
        [ 0 1 2 4 3 5 6 ] ]
    witheach
      [ dip dup reorder swap ]
    4 pack ]                         is orderings (       [ --> [ )

  [ witheach
      [ dup number? iff n->v done
        dup ' + = iff
          [ drop v+ ] done
        dup ' - = iff
          [ drop v- ] done
            ' * = iff v* done
        v/ ]
    24 n->v v- v0= ]                 is 24=       (       [ --> b )

  [ 4 pack sort
    [] swap
    ' [ + - * / ] join
    allorders witheach
      [ dip dup reorder orderings
        witheach 
          [ dup 24= iff
              [ rot swap
                nested join swap ]
            else drop ] ]
    drop
    uniquewith
      [ dip unbuild unbuild $> ]
    dup size
    dup 0 = iff
       [ 2drop say "No solutions." ]
       done
    dup 1 = iff
      [ drop say "1 solution." ]
    else
      [ echo say " solutions." ]
    unbuild
    2 split nip
    -2 split drop nest$ 90 wrap$ ]   is solve     ( n n n n -->   )

  

You may also check:How to resolve the algorithm Here document step by step in the Ursala programming language
You may also check:How to resolve the algorithm Monty Hall problem step by step in the C programming language
You may also check:How to resolve the algorithm Determine if two triangles overlap step by step in the Julia programming language
You may also check:How to resolve the algorithm Statistics/Normal distribution step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Hello world/Web server step by step in the Phix programming language