Skip to content

Instantly share code, notes, and snippets.

@jondoesntgit
Created December 8, 2017 01:54
Show Gist options
  • Save jondoesntgit/a3229431395838bb3ede16e04e41ca28 to your computer and use it in GitHub Desktop.
Save jondoesntgit/a3229431395838bb3ede16e04e41ca28 to your computer and use it in GitHub Desktop.
Script for finding all solutions to the card game "24"
import numpy as np
def combine(*args):
if len(args) == 2:
return np.concatenate([(x*y, x+y, abs(x-y), x/y, y/x) for x in args[0] for y in args[1]])
return np.concatenate([combine(combine(*np.roll(args, i)[1:]), np.roll(args,i)[0]) for i in range(len(args))])
vals = (np.arange(10) + 1).astype(np.float64)
for x in ("%i %i %i %i, %s" % (a,b,c,d, 24 in combine([a], [b], [c], [d])) for a in vals for b in vals for c in vals for d in vals if a <= b and b <= c and c <= d ):
print(x)
import numpy as np
def combine(*args):
if len(args) == 2:
return np.concatenate([(x*y, x+y, abs(x-y), x/y, y/x) for x in args[0] for y in args[1]])
return np.concatenate([combine(combine(*np.roll(args, i)[1:]), np.roll(args,i)[0]) for i in range(len(args))])
vals = (np.arange(10) + 1).astype(np.float64)
for x in ("%i %i %i %i, %s" % (a,b,c,d, 24 in combine([a], [b], [c], [d])) for a in vals for b in vals for c in vals for d in vals if a <= b and b <= c and c <= d ):
print(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment