fork(1) download
  1. '''
  2. Validator for PPCG challenge.
  3.  
  4. Provide input and output filenames as arguments or input and output as stdin (2 lines input and then 1line output).
  5. '''
  6. import sys
  7.  
  8. if len(sys.argv)==3:
  9. with open(sys.argv[1]) as f:
  10. si=f.readline().rstrip()
  11. so=f.readline().rstrip()
  12. with open(sys.argv[2]) as f:
  13. moves=f.readline().rstrip()
  14. else:
  15. si=input()
  16. so=input()
  17. moves=input()
  18. if sorted(si)!=sorted(so):
  19. print('incompatible start and goal strings')
  20. exit()
  21. d={'u':(0,-1),'d':(0,1),'l':(-1,0),'r':(1,0)}
  22. p=[]
  23. for i in range(len(si)):
  24. p+=[(i,0)]
  25. for i,move in enumerate(moves.split()):
  26. try:
  27. poss,dir=move.split(':')
  28. pos=int(poss)
  29. p[pos]=tuple(a+b for a,b in zip(p[pos],d[dir]))
  30. if len(set(p))<len(si):
  31. print('letters at the same position at move',i+1)
  32. exit()
  33. except:
  34. print('format error at move',i+1)
  35. exit()
  36. step_count=i+1
  37. r=set()
  38. for i,pe in enumerate(p):
  39. if pe[1]==0 and 0<=pe[0]<len(so) and si[i]==so[pe[0]]:
  40. r.add(pe[0])
  41. else:
  42. print('goal string not reached')
  43. exit()
  44. if len(r)!=len(so):
  45. print('goal string not reached')
  46. else:
  47. print('move sequence verified with step count',step_count)
  48.  
Success #stdin #stdout 0.02s 8688KB
stdin
abc
acb
2:d 1:r 2:l 2:u
stdout
move sequence verified with step count 4