#! /usr/bin/python3 # This is an simple path-finder program married to the NetLogo PathFinderVisual.nlogo program # This assumes that there are NO WALLS! and will not read wall locations in the input file import sys, json InFile = '' OutFile = '' def main(infile='', outfile=''): global InFile, OutFile if infile == '': InFile = sys.argv[1] OutFile = sys.argv[2] else: InFile = infile OutFile = outfile try: fin = open(InFile,'r') slines = fin.read().split('\n') except: if len(slines) < 2: print ('Error: too few lines in %s' % InFile) return fin.close() # convert each point's coords into list of ints start = json.loads(slines[0].replace(' ',', ')) end = json.loads(slines[1].replace(' ',', ')) answer = [] while start[0] != end[0]: if start[0] < end[0]: answer.append('r') # move right start[0] += 1 else: answer.append('l') # move left start[0] -= 1 while start[1] != end[1]: if start[1] < end[1]: answer.append('u') # move up start[1] += 1 else: answer.append('d') # move down start[1] -= 1 # remove last movement answer = answer[:-1] try: fout = open(OutFile,'w') s = json.dumps(answer).replace(', ',' ') fout.write(s+'\n') fout.close() except: print ('Error: Cannot write to %s' % OutFile) return print ('Done') return main()