Earlier today, I was very bored. I set out to see if I could write a program that would solve 24 problems (see Wikipedia).

Now, I was bored, mind you, and I thought, "Can I solve it in 1 line of code?"

NOTE: I do not recomend this approach, it is not performant, readable, maintainable, or scalable, but it was fun.

using System;
using System.Collections.Generic;
using System.Linq;

namespace Linq24Solver
{
    class Program
    {
        private static Dictionary<string, Func<double, double, double>> ops 
            = new Dictionary<string, Func<double, double, double>>()
                                    {
                                        {"+", (num1, num2) => num1 + num2},
                                        {"-", (num1, num2) => num1 - num2},
                                        {"*", (num1, num2) => num1 * num2},
                                        {"/", (num1, num2) => num1 / num2},
                                        {"^", (num1, num2) => Math.Pow(num1, num2)}
                                    };

        static void Main(string[] args)
        {
            while (true)
            {
                Console.Write("Enter 4 numbers, separated by spaces: ");

                var enteredText = Console.ReadLine();

                if (enteredText.Contains('q'))
                    break;

                var splitStrings = enteredText.Split(' ');

                if (splitStrings.Length != 4)
                    continue;

                var ints = new List<double>();

                foreach (var numString in splitStrings)
                {
                    int i;

                    if (!int.TryParse(numString, out i))
                        break;

                    ints.Add(i);
                }

                if (ints.Count != 4)
                    continue;

                var solutions = ops.SelectMany(op1 => ops
                                    .SelectMany(op2 => ops
                                    .Select(op3 => new { op1, op2, op3 })))
                                    .SelectMany(oc => ints
                                    .SelectMany((a, i) => ints
                                    .SelectMany((b, j) => ints
                                    .SelectMany((c, k) => ints
                                    .Select((d, l) => new { a, b, c, d, i, j, k, l }))))
                                    .Where(x => x.i != x.j && x.i != x.k && x.i != x.l
                                        && x.j != x.k && x.j != x.l && x.k != x.l)
                                    .Select(ns => new { ns.a, ns.b, ns.c, ns.d, oc.op1, oc.op2, oc.op3 }))
                                    .Where(x => 
                                        x.op3.Value(x.op2.Value(x.op1.Value(x.a, x.b), x.c), x.d) == 24)
                                    .Select(x => String.Format("{0} {1} {2} {3} {4} {5} {6} = 24",
                                        x.a, x.op1.Key, x.b, x.op2.Key, x.c, x.op3.Key, x.d))
                                    .Distinct();

                foreach (var line in solutions)
                {
                    Console.WriteLine(line);
                };
            }
        }
    }
}

Now, this doesn't produce results that work if you consider orders of operations, i.e. "1 + 3 * 6 / 1" means "add three to one, multiply that by 6, and then divide that by 1."

Also, this doesn't generate solutions with parentheses like (8/(3-(8/3))=24, but I'm working on it.

Wow I have always thought of creating this while playing the game. Way to go man!

This article has been dead for over six months. Start a new discussion instead.