# Path: pycryptics/solve_clue.py
# class CrypticClueSolver(object):
#     def __init__(self):
#         self.answers_with_clues = None
#         self.clue_text = None
#         self.quiet = False
# 
#     def __enter__(self):
#         # self.start_go_server()
#         return self
# 
#     def __exit__(self, type, value, traceback):
#         self.stop()
#         # self.stop_go_server()
# 
#     def stop(self):
#         pass
# 
#     def setup(self, clue_text):
#         self.clue_text = clue_text
# 
#     def run(self):
#         self.clue_text = self.clue_text.encode('ascii', 'ignore')
#         constraints = parse_clue_text(self.clue_text)
#         return self.solve_all_phrasings(constraints)
#         # all_phrasings, lengths, pattern, answer = parse_clue_text(self.clue_text)
# 
#     def solve_all_phrasings(self, constraints):
#         all_phrasings = phrasings(constraints.phrases)
# 
#         self.answers_with_clues = []
# 
#         for p in all_phrasings:
#             constraints = constraints._replace(phrases=p)
#             # constraints = Constraints(p, lengths, pattern, answer)
#             if not self.quiet:
#                 print p
#             for ann_ans in self.solve_constraints(constraints):
#                 self.answers_with_clues.append(ann_ans)
#         if len(self.answers_with_clues) == 0 and constraints.pattern.replace('.', '') != "":
#             self.answers_with_clues = [PatternAnswer(x, all_phrasings[0]) for x in SYNONYMS if matches_pattern(x, constraints.pattern, constraints.lengths)]
#         self.answers_with_clues.sort(reverse=True)
#         return self.answers_with_clues
# 
#     def solve_constraints(self, constraints):
#         answers_with_clues = []
#         possible_clues = generate_clues(constraints)
# 
#         for i, clue in enumerate(possible_clues):
#             # print "solving:", clue
#             try:
#                 answers = clue.answers
#             except ClueUnsolvableError:
#                 answers = []
#             for answer in answers:
#                 answers_with_clues.append(AnnotatedAnswer(answer, clue))
#         return sorted(answers_with_clues, reverse=True)
# 
#     def collect_answers(self):
#         if self.answers_with_clues is not None:
#             return ClueSolutions(self.answers_with_clues)


import pycryptics.puzpy.puz as puz
import sys
import readline
from pycryptics.solve_clue import CrypticClueSolver
                            ndx = 0
                            for a in answers:
                                if a.answer not in ans_strings.values():
                                    ans_strings[ndx] = a.answer
                                    ndx += 1
                                ans_derivations.setdefault(a.answer, []).append(a)
                            for i in range(min(15, len(ans_strings.keys()))):
                                print i, ans_strings[i]
                            while True:
                                action = raw_input("[a]ccept/[d]erivations [number], or leave blank to cancel: ").strip()
                                if action != "":
                                    try:
                                        verb, noun = action.split(' ')
                                        choice = int(noun)
                                        if verb == "a":
                                            p.set_clue_fill(clue, ans_strings[choice])
                                            break
                                        if verb == "d":
                                            for d in ans_derivations[ans_strings[choice]]:
                                                print d
                                    except ValueError:
                                        pass
                        except KeyboardInterrupt:
                            solver.reset()
                    elif action == "b":
                        break
                    elif action == "g":
                        ans = raw_input("Proposed answer: ")
                        p.set_clue_fill(clue, ans)
                        break

# Path: pycryptics/solve_clue.py
# class CrypticClueSolver(object):
#     def __init__(self):
#         self.answers_with_clues = None
#         self.clue_text = None
#         self.quiet = False
# 
#     def __enter__(self):
#         # self.start_go_server()
#         return self
# 
#     def __exit__(self, type, value, traceback):
#         self.stop()
#         # self.stop_go_server()
# 
#     def stop(self):
#         pass
# 
#     def setup(self, clue_text):
#         self.clue_text = clue_text
# 
#     def run(self):
#         self.clue_text = self.clue_text.encode('ascii', 'ignore')
#         constraints = parse_clue_text(self.clue_text)
#         return self.solve_all_phrasings(constraints)
#         # all_phrasings, lengths, pattern, answer = parse_clue_text(self.clue_text)
# 
#     def solve_all_phrasings(self, constraints):
#         all_phrasings = phrasings(constraints.phrases)
# 
#         self.answers_with_clues = []
# 
#         for p in all_phrasings:
#             constraints = constraints._replace(phrases=p)
#             # constraints = Constraints(p, lengths, pattern, answer)
#             if not self.quiet:
#                 print p
#             for ann_ans in self.solve_constraints(constraints):
#                 self.answers_with_clues.append(ann_ans)
#         if len(self.answers_with_clues) == 0 and constraints.pattern.replace('.', '') != "":
#             self.answers_with_clues = [PatternAnswer(x, all_phrasings[0]) for x in SYNONYMS if matches_pattern(x, constraints.pattern, constraints.lengths)]
#         self.answers_with_clues.sort(reverse=True)
#         return self.answers_with_clues
# 
#     def solve_constraints(self, constraints):
#         answers_with_clues = []
#         possible_clues = generate_clues(constraints)
# 
#         for i, clue in enumerate(possible_clues):
#             # print "solving:", clue
#             try:
#                 answers = clue.answers
#             except ClueUnsolvableError:
#                 answers = []
#             for answer in answers:
#                 answers_with_clues.append(AnnotatedAnswer(answer, clue))
#         return sorted(answers_with_clues, reverse=True)
# 
#     def collect_answers(self):
#         if self.answers_with_clues is not None:
#             return ClueSolutions(self.answers_with_clues)


# Path: pycryptics/solve_puz.py
import pycryptics.puzpy.puz as puz
import sys
import readline
from pycryptics.solve_clue import CrypticClueSolver

                            ndx = 0
                            for a in answers:
                                if a.answer not in ans_strings.values():
                                    ans_strings[ndx] = a.answer
                                    ndx += 1
                                ans_derivations.setdefault(a.answer, []).append(a)
                            for i in range(min(15, len(ans_strings.keys()))):
                                print i, ans_strings[i]
                            while True:
                                action = raw_input("[a]ccept/[d]erivations [number], or leave blank to cancel: ").strip()
                                if action != "":
                                    try:
                                        verb, noun = action.split(' ')
                                        choice = int(noun)
                                        if verb == "a":
                                            p.set_clue_fill(clue, ans_strings[choice])
                                            break
                                        if verb == "d":
                                            for d in ans_derivations[ans_strings[choice]]:
                                                print d
                                    except ValueError:
                                        pass
                        except KeyboardInterrupt:
                            solver.reset()
                    elif action == "b":
                        break
                    elif action == "g":
                        ans = raw_input("Proposed answer: ")
                        p.set_clue_fill(clue, ans)
                        break

                    elif action == "e":