0
\$\begingroup\$

This is my first codegolf post so let me know if I have missed anything. Thanks :)

Description

You are given a list of numbers with 2 < n <= 6 length i.e. [1, 10] or [75, 9, 9, 8, 2, 100]. Given the basic operators + - / * you must try and generate all possible equations. The list of numbers to pull from is: [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 25, 50, 75, 100].

Defining this list as is for your program does not count towards the byte count, same goes for defining the list of operators. There are two separate winning conditions: 1) Smallest program; 2) Fastest program.

Generating all equations when n = 6 results in ~200 billion equations. It might be wise to verify that your code works with lower lengths values first, or a subset from the list of numbers.

Rules

  1. Equations must be in postfix notation
  2. No duplicate equations i.e. 1 1 2 + + and 1 1 2 + +
  3. Handle commutativity i.e. 1 10 + 2 4 + + and 10 2 1 4 + + +. Remove commutative equations so only one remains.

Extra Challenges

  • Calculate all equations, only accepting positive 3 digit solutions. If at any stage during the process the solution becomes negative 1 100 - or non-int 100 3 /, stop processing the entire equation and move onto the next equation.

Final Notes

Some of you may have recognised the list of numbers from Countdown or Letters and Numbers. I myself wrote a program (unoptimized, but functional) to calculate all possible 3 digit solutions under the shows rules. It took 90 minutes to run with multithreading, though the slowest part was calculating the equations, not generating them.

Good luck!

\$\endgroup\$
13
  • 5
    \$\begingroup\$ Welcome to the site! While your challenge shows promise, I do think you missed the Sandbox. Please use it to hammer out issues from a challenge before posting. I personally highly recommend answering a few existing challenges (even if your answers are not competitive) before posting your own challenges, to get a feel for what it takes to specify a challenge properly. \$\endgroup\$
    – Adám
    May 5, 2023 at 6:48
  • 5
    \$\begingroup\$ What exactly does "Defining this list as is for your program does not count towards the byte count, same goes for defining the list of operators." mean? Maybe solution will have these definitions intermingled with the rest of the code, making it hard-to-impossible to compute how much space is taken by the definitions. Giving the lists as additional parameters/arguments would make for easier comparison. You could also say the lists may be in pre-defined variables, but not all languages have those. \$\endgroup\$
    – Adám
    May 5, 2023 at 6:52
  • 4
    \$\begingroup\$ Regarding "Extra Challenges", is that another required rule, or is it optional? If required, just list it under "Rules". If optional, how does it affect one's score? If it doesn't, there's no point in even mentioning it, as it clearly will lengthen/slow down one's code, so nobody will do it. \$\endgroup\$
    – Adám
    May 5, 2023 at 6:55
  • 1
    \$\begingroup\$ Is the exact sample output necessary, or would less/more whitespace be acceptable? How about outputting as a list like [1,10,+,2,4,+,+] or ["1","10","+","2","4","+","+"]? Are alternative symbols for the four operators allowed, e.g. + - × ÷? \$\endgroup\$
    – Adám
    May 5, 2023 at 6:58
  • 2
    \$\begingroup\$ OK, so now you changed the original extra challenge into a proper rule, thus invalidating an existing answer. On top of that, you've added yet another extra challenge, without specifying if it is required or optional. I recommend deleting your challenge, addressing the raised issue, and then re-opening it, or alternatively, move it to the sanbox as officially recommended. \$\endgroup\$
    – Adám
    May 5, 2023 at 11:55

1 Answer 1

2
\$\begingroup\$

05AB1E, 31 bytes

榀œ€`"+-/*"Igãâε¬s`s¦∍S«ðý}ÙIK

Goes for the code-golf portion. Brute-force approach, so pretty slow.
Doesn't handle the Extra Challenge portion.

Try it online.

Explanation:

æ             # Get the powerset of the (implicit) input-list
 ¦            # Remove the leading empty sublist
  €œ          # Map over each powerset-list, and get all its permutations
    €`        # Flatten the list of lists of lists one level down
"+-/*"        # Push the string of operators
      Ig      # Push the input-length
        ã     # Get the cartesian power of the two
â             # Get the cartesian product of the two lists to create all possible pairs
 ε            # Map over each pair of [list, operator-string]:
  ¬           # Push the head (the list) (without popping)
   s          # Swap so the pair is at the top of the stack again
    `         # Pop and push the list and string separately to the stack
     s        # Swap so the list is at the top of the stack
      ¦       # Remove its first item
       ∍      # Shorten the operator string to the length of this list
        S     # Convert the operator string to a list of characters
         «    # Merge it to the list of integers
          ðý  # Join it by spaces
 }Ù           # After the map: uniquify the results
   IK         # Remove the singular input-integers, since they're not equations
              # (after which the result is output implicitly)

Since 05AB1E uses Reverse Polish/Postfix notation, here the program with additional footer to get the result of each equation: Try it online.

\$\endgroup\$
7
  • \$\begingroup\$ Never seen this language before. Though your solution is damn impressive. Well done! \$\endgroup\$
    – Kyle Sharp
    May 5, 2023 at 9:51
  • \$\begingroup\$ Note that your solution has become invalid by an edit to the OP. \$\endgroup\$
    – Adám
    May 5, 2023 at 12:09
  • 1
    \$\begingroup\$ "Since 05AB1E uses Polish/Prefix notation": are you sure? I'm pretty sure it uses reverse Polish / postfix notation, where operations come after the operands. \$\endgroup\$
    – The Thonnu
    May 5, 2023 at 15:13
  • \$\begingroup\$ @Adám I noticed indeed, but thanks for the heads up. I'll see when I have some time to fix it. \$\endgroup\$ May 5, 2023 at 15:21
  • \$\begingroup\$ Have you had time to fix this yet? \$\endgroup\$ Jun 3, 2023 at 16:21

Not the answer you're looking for? Browse other questions tagged or ask your own question.