#!/usr/bin/env python import socket import time import re s=socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(("diehard.shallweplayaga.me",4001)) toroom = "n\nn\nn\nn\nn\nn\ne\nn\nw\nn\n" getinfo = "l red jug\nl blue jug\nlook inscription\n" takeall = "get blue jug\nget red jug\n" # fetch data data = s.recv(2048) print data s.sendall(toroom) count = 0 goal = 0 while 1: print "COUNT: ", count data = s.recv(4096) print data if "You have failed!" in data: exit(0) if goal == 0 and ("A scale is sitting in the room." in data or "Streams of dust" in data): goal = 1 s.sendall(getinfo) continue for line in data.split("\n"): if "A key is sitting in the room." in line: s.sendall("look key\nget key\nl key\n") print "REQUEST SEND" data = s.recv(4096) print data break if "A red jug holds" in line: m = re.search('(?<= of )\w+', line) red = int(m.group(0)) if "A blue jug holds" in line: m = re.search('(?<= of )\w+', line) blue = int(m.group(0)) if "To get to the next stage put" in line: m = re.search('(?<= put )\w+', line) goal = int(m.group(0)) print "red=", red, "blue=", blue, "goal=", goal lred = 0 lblue = 0 solution = takeall if red > blue: while lred != goal: if lred == 0: solution += "fill red jug\n" lred = red solution += "pour red jug into blue jug\n" if lred > (blue-lblue): lred -= blue-lblue lblue = blue solution += "empty blue jug\n" lblue = 0 else: lblue = lred lred = 0 solution += "put red jug onto scale\n" solution += "drop blue jug\n" else: while lblue != goal: if lblue == 0: solution += "fill blue jug\n" lblue = blue solution += "pour blue jug into red jug\n" if lblue > (red-lred): lblue -= red-lred lred = red solution += "empty red jug\n" lred = 0 else: lred = lblue lblue = 0 solution += "put blue jug onto scale\n" solution += "drop red jug\n" solution += "n\n" print "Sending " + str(len(solution)) + " bytes..." s.sendall(solution) count += 1 goal = 0 s.close()