Skip to content

Instantly share code, notes, and snippets.

@ensky

ensky/24.py Secret

Created Aug 17, 2014
Embed
What would you like to do?
import re
import sys
import itertools
def solve(numbers):
ops = "+ - * / // **".split(" ")
wrap = ["", "("]
wclo = ["", ")"]
minu = ["", "-"]
def executes (number, m1,m2,m3,a,b,c):
for w1 in wrap:
e = w1 + m1 + str(number[0]) + ".0"
e += a
for w2 in wrap:
b1 = e
e += w2 + m2 + str(number[1]) + ".0"
for c1 in wclo:
b2 = e
if w1 == "(":
e += c1
e += b
for w3 in wrap:
b3 = e
e += w3 + m3 + str(number[2]) + ".0"
if w1 == "(" and c1 == "":
e += ")"
for c2 in wclo:
b4 = e
if w2 == "(":
e += c2
e += c
e += str(number[3]) + ".0"
if w2 == "(" and c2 == "":
e += ")"
if w3 == "(":
e += ")"
try:
if abs(eval(e) - 24) < 1e-15:
return e.replace(".0", "")
except:
pass
e = b4
e = b3
e = b2
e = b1
iterN = itertools.permutations(numbers, 4)
for number in iterN:
for m1 in minu:
for m2 in minu:
for m3 in minu:
for a in ops:
for b in ops:
for c in ops:
result = executes(number, m1,m2,m3,a,b,c)
if result: return result
while True:
r = raw_input("")
# print >> sys.stderr, r
m = re.search('\[(\d+), (\d+), (\d+), (\d+)\]', r)
if not m:
continue
numbers = [m.group(1), m.group(2), m.group(3), m.group(4)]
print >> sys.stderr, solve(numbers)
var spawn = require('child_process').spawn,
log = console.log,
main = function () {
var nc = spawn('nc', ['210.65.89.59', '2424']);
var p24 = spawn('python', ['24n.py']);
nc.stdout.pipe(p24.stdin);
p24.stderr.pipe(nc.stdin);
nc.stdout.on('data', function (data) {
log(data.toString());
});
p24.stderr.on('data', function (data) {
log(data.toString());
});
nc.on('close', function (code, signal) {
console.log('child process terminated due to receipt of signal '+signal);
main();
});
}
main();
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment