Created
June 15, 2013 08:14
-
-
Save flankerhqd/5787366 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import socket | |
| import re | |
| import os | |
| HOST = 'diehard.shallweplayaga.me' | |
| PORT = 4001 | |
| s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
| def print_prompt(): | |
| print s.recv(409600) | |
| def connect(): | |
| s.connect((HOST, PORT)) | |
| def put_string(str): | |
| s.send(str + '\n') | |
| print s.recv(409600) | |
| def put_raw(str): | |
| s.send(str) | |
| print s.recv(40960000) | |
| def walk_north(): | |
| s.send('n\n') | |
| print s.recv(409600) | |
| def walk_south(): | |
| s.send('s\n') | |
| print s.recv(409600) | |
| def walk_west(): | |
| s.send('w\n') | |
| print s.recv(409600) | |
| def walk_east(): | |
| s.send('e\n') | |
| print s.recv(409600) | |
| def get_jug(jug_color): | |
| jug_color = process_single_char(jug_color) | |
| s.send('get ' + jug_color + ' jug\n') | |
| print s.recv(409600) | |
| def drop_jug(jug_color): | |
| jug_color = process_single_char(jug_color) | |
| s.send('drop ' + jug_color + ' jug\n') | |
| print s.recv(409600) | |
| def fill_jug(jug_color): | |
| jug_color = process_single_char(jug_color) | |
| s.send('fill ' + jug_color + ' jug\n') | |
| print s.recv(409600) | |
| def empty_jug(jug_color): | |
| jug_color = process_single_char(jug_color) | |
| s.send('empty ' + jug_color + ' jug\n') | |
| print s.recv(409600) | |
| def process_single_char(c): | |
| if c == 'r': | |
| c = 'red' | |
| else: | |
| c = 'blue' | |
| return c | |
| def get_volume(jug_color): | |
| jug_color = process_single_char(jug_color) | |
| print("fuck") | |
| print('look ' + jug_color + ' jug\n') | |
| s.send('look ' + jug_color + ' jug\n') | |
| reg = None | |
| while reg is None: | |
| data = s.recv(409600000) | |
| print data | |
| regObj = re.compile(r'[\s\S]*(\d+)\sof\s(\d+)[\s\S]*', re.S) | |
| reg = regObj.match(data) | |
| return (reg.group(1), reg.group(2)) | |
| def pour_A_into_B(A, B): | |
| A = process_single_char(A) | |
| B = process_single_char(B) | |
| s.send('pour ' + A + ' jug' + ' into ' + B + ' jug' + '\n') | |
| def put_jug_on_scale(jug_color): | |
| jug_color = process_single_char(jug_color) | |
| print 'put ' + jug_color + ' jug onto scale\n' | |
| s.send('put ' + jug_color + ' jug onto scale\n') | |
| data = s.recv(409600) | |
| return checkWeight(data) | |
| LOW = -1 | |
| HIGH = 1 | |
| OK = 0 | |
| ERROR = 2 | |
| def checkWeight(s): | |
| if s.find("The scale moves down but not enough to open the door.") != -1: | |
| return LOW | |
| elif s.find("The scale moves down too far and the door quickly closes shut.") != -1: | |
| return HIGH | |
| elif s.find("The scale balances perfectly and a door opens to the next room!") != -1: | |
| return OK | |
| else: | |
| return ERROR # unknow number | |
| ''' | |
| Get the gallons of jugs | |
| Search for the proper weight | |
| ''' | |
| def proceedRaw(jug_color): | |
| fill_jug('r') | |
| put_jug_on_scale('r') | |
| if jug_color == 'r': | |
| drop_jug('b') | |
| else: | |
| drop_jug('r') | |
| def gao(rv, bv, target): | |
| empty_jug('r') | |
| empty_jug('w') | |
| get_jug('r') | |
| get_jug('w') | |
| print ("%d %d %d\n" % (rv, bv, target)) | |
| handle = os.popen("./a2 %d %d %d" % (rv, bv, target)) | |
| solution = handle.read() | |
| print solution | |
| put_raw(solution) | |
| (current_red_jug_volume, red_jug_volume) = get_volume('r') | |
| (current_blue_jug_volume, blue_jug_volume) = get_volume('b') | |
| if current_red_jug_volume == target: | |
| ret = put_jug_on_scale('r') | |
| else: | |
| ret = put_jug_on_scale('w') | |
| return ret | |
| def look_inscription(): | |
| s.send("look inscription\n") | |
| tmp = None | |
| while tmp is None: | |
| data = s.recv(1024000) | |
| print data | |
| tmp = re.search("put (\d+) gallons on the scale", data) | |
| return int(tmp.group(1)) | |
| def solve_jug_problem(): | |
| red_jug_volume = 0 | |
| blue_jug_volume = 0 | |
| current_red_jug_volume = 0 | |
| current_blue_jug_volume = 0 | |
| (current_red_jug_volume, red_jug_volume) = get_volume('r') | |
| (current_blue_jug_volume, blue_jug_volume) = get_volume('b') | |
| red_jug_volume = int(red_jug_volume) | |
| blue_jug_volume = int(blue_jug_volume) | |
| down = 1 | |
| get_jug('r') | |
| get_jug('b') | |
| empty_jug('r') | |
| empty_jug('b') | |
| gao(red_jug_volume, blue_jug_volume, look_inscription()) | |
| def main(): | |
| connect() | |
| print_prompt() | |
| # Walk to the room | |
| for i in range(0, 6): | |
| walk_north() | |
| walk_east() | |
| walk_north() | |
| for i in range(0, 1): | |
| walk_west() | |
| cnt = 0 | |
| while(True): | |
| walk_north() | |
| # We are in the room! | |
| solve_jug_problem() | |
| drop_jug('r') | |
| drop_jug('b') | |
| cnt = cnt + 1 | |
| print(str(cnt) + " seq") | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment