Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.59 KB | None | 0 0
  1. # This is a sample Code Challenge
  2. # Learn more: https://stepik.org/lesson/9173
  3. # Ask your questions via support@stepik.org
  4.  
  5. def generate():
  6.     return []
  7.  
  8. class Robot():
  9.     loc = 0
  10.     dir = " "
  11.     pre_loc = 0
  12.     pre_dir = " "
  13.  
  14.     def __init__(self, loc, dir):
  15.         self.loc = loc
  16.         self.dir = dir
  17.         #self.pre_loc = loc
  18.         #self.pre_dir = dir
  19.  
  20.  
  21. def check(reply, clue):
  22.     height = 8
  23.     width = 8
  24.     adj = [[-1, -1, 8, -1], [-1, 2, 9, -1], [-1, 3, 10, 1], [-1, -1, 11, 2], [-1, 5, 12, -1], [-1, 6, -1, 4],
  25.            [-1, 7, -1, 5], [-1, -1, 15, 6], [0, -1, 16, -1], [1, 10, -1, -1], [2, -1, -1, 9], [3, -1, 19, -1],
  26.            [4, 13, 20, -1], [-1, -1, -1, 12], [-1, 15, 22, -1], [7, -1, -1, 14], [8, 17, 24, -1], [-1, 18, -1, 16],
  27.            [-1, 19, 26, 17], [11, -1, -1, 18], [12, -1, 28, -1], [-1, 22, 29, -1], [14, -1, 30, 21], [-1, -1, 31, -1],
  28.            [16, -1, 32, -1], [-1, 26, 33, -1], [18, 27, 34, 25], [-1, 28, -1, 26], [20, 29, -1, 27], [21, 30, -1, 28],
  29.            [22, -1, 38, 29], [23, -1, 39, -1], [24, -1, 40, -1], [25, 34, 41, -1], [26, 35, -1, 33], [-1, -1, 43, 34],
  30.            [-1, 37, 44, -1], [-1, -1, 45, 36], [30, -1, 46, -1], [31, -1, 47, -1], [32, -1, 48, -1], [33, -1, -1, -1],
  31.            [-1, -1, 50, -1], [35, -1, 51, -1], [36, -1, 52, -1], [37, -1, 53, -1], [38, -1, 54, -1], [39, -1, 55, -1],
  32.            [40, 49, 56, -1], [-1, 50, -1, 48], [42, -1, -1, 49], [43, -1, 59, -1], [44, -1, -1, -1], [45, 54, 61, -1],
  33.            [46, 55, 62, 53], [47, -1, 63, 54], [48, 57, -1, -1], [-1, 58, -1, 56], [-1, 59, -1, 57], [51, 60, -1, 58],
  34.            [-1, 61, -1, 59], [53, -1, -1, 60], [54, 63, -1, -1], [55, -1, -1, 62]]
  35.     N = 0
  36.     DEBUGG = 0
  37.     Robots = []
  38.     input = []
  39.     solution = []
  40.     student_sol = []
  41.  
  42.     N = int(clue[0])
  43.     reply = reply.splitlines()
  44.     clue = clue.splitlines()
  45.     for i in range(N):
  46.         input.append(clue[i + 1].split())
  47.         input[i][0] = int(input[i][0])
  48.         input[i][1] = int(input[i][1])
  49.         input[i][3] = int(input[i][3])
  50.         input[i][4] = int(input[i][4])
  51.  
  52.     for i in range(N):
  53.         solution.append(clue[1 + N + i])
  54.     for i in range(len(reply)):
  55.         student_sol.append(reply[i])
  56.  
  57.     for i in range(N):
  58.         one_robot = Robot(input[i][0] + 8 * input[i][1], input[i][2])
  59.         Robots.append(one_robot)
  60.         Robots[i].loc = input[i][0] + 8 * input[i][1]
  61.         Robots[i].dir = input[i][2]
  62.  
  63.     max_size = 0
  64.     for i in range(len(student_sol)):
  65.         if len(student_sol[i]) > max_size:
  66.             max_size = len(student_sol[i])
  67.  
  68.     for i in range(max_size):
  69.         if DEBUGG:
  70.             print(i)
  71.  
  72.         for j in range(N):
  73.             Robots[j].old_dir = (Robots[j].dir)
  74.  
  75.             Robots[j].old_loc = (Robots[j].loc)
  76.  
  77.         for j in range(N):
  78.             if i >= len(student_sol[j]):
  79.                 continue
  80.             if student_sol[j][i] == 'F':
  81.                 Robots[j].loc = adj[Robots[j].loc][intdir(Robots[j].dir)]
  82.             if (student_sol[j][i] == 'R'):
  83.                 Robots[j].dir = chardir((intdir(Robots[j].dir) + 1) % 4)
  84.                 Robots[j].loc = adj[Robots[j].loc][intdir(Robots[j].dir)]
  85.  
  86.             if (student_sol[j][i] == 'L'):
  87.                 Robots[j].dir = chardir((intdir(Robots[j].dir) + 3) % 4)
  88.                 Robots[j].loc = adj[Robots[j].loc][intdir(Robots[j].dir)]
  89.  
  90.         for j in range(N):
  91.             if (Robots[j].loc == -1):
  92.                 if DEBUGG : print ("onto wall", j," robot" , i , "step")
  93.                 return False
  94.  
  95.         for j in range(N):
  96.             for z in range(j + 1, N):
  97.  
  98.                 if (Robots[j].loc == Robots[z].loc):
  99.                     if (DEBUGG):
  100.                         print("in one place", j, z, "robots", i, "step")
  101.  
  102.                     return False
  103.                 if (Robots[j].loc == Robots[z].pre_loc):
  104.                     if (Robots[j].dir != Robots[z].dir):
  105.                         if DEBUGG :
  106.                             print("on tail", j, z, "robots", i, "step")
  107.                         return False
  108.                 if (Robots[z].loc == Robots[j].pre_loc):
  109.                     if (Robots[z].dir != Robots[j].dir):
  110.                         if DEBUGG :
  111.                             print("on tail", z, j, "robots", i, "step")
  112.                         return False
  113.  
  114.     for j in range(N):
  115.         if (Robots[j].loc != input[j][4] * 8 + input[j][3]):
  116.             if (DEBUGG):
  117.                 print(j, Robots[j].loc, input[j][4] * 8 + input[j][3])
  118.             return False
  119.     if (DEBUGG):
  120.         print("ok")
  121.     len_of_correct_ans = 0
  122.     len_of_student_ans = 0
  123.     for i in range (N):
  124.         len_of_correct_ans += len(solution[i])
  125.         len_of_student_ans += len(student_sol[i])
  126.     if (len_of_student_ans > len_of_correct_ans):
  127.         return False
  128.     return True
  129.  
  130.  
  131.  
  132. def intdir(c):
  133.     if (c == 'U'): return 0;
  134.     if (c == 'R'): return 1;
  135.     if (c == 'D'): return 2;
  136.     if (c == 'L'): return 3;
  137.  
  138.  
  139. def chardir(c):
  140.     if (c == 0): return 'U';
  141.     if (c == 1): return 'R';
  142.     if (c == 2): return 'D';
  143.     if (c == 3): return 'L';
  144.  
  145.  
  146. from hashlib import sha1
  147.  
  148. def solve(dataset):
  149.     solver = {'5ff29d92f002cd222154312740a68907dfa06019': '3\n1 2 D 2 4\n2 2 D 2 3\n3 2 L 1 4\nRLFFFFLFFLFFL\nFFRRRFFFFLLLRFF\nRFLFLLLRRFRLRL', '9196a0c01b4078153158cdbbc8a9f29e15b6a8b3': '2\n6 7 U 0 2\n7 7 U 1 2\nFLLRFFFFRFFFF\nFLRFFFLLRFFRL', '5ccfc1856d77aab9a87f56ec1079ece70daa426b': '2\n0 0 D 2 5\n0 1 R 2 1\nFFFFFFLFL\nRFFFFFLFFLFFLRFRLFLL', 'f68bef09e6437e08ceca39dedc09ccb6e1d4243c': '2\n0 0 D 2 5\n0 2 U 0 0\nFFFFFFLFL\nRFRFRRRLLFRF', 'b15280a79fa6a9a419428e6e4bb9a543489e5f08': '3 \n1 1 U 4 2\n2 7 D 6 1\n2 5 L 6 0\nFRFRFRLLFFFLLLRR\nLFFLRLFFFF\nLRFRFFFRFRLFLFFRF', '9a22b75342011d347f327c0c81999f3900686be7': '3\n4 6 U 7 2\n4 4 U 7 3\n5 5 R 7 4\nFFRRFLFLFFF\nRRFLFRRRRLFF\nRLLFFFLLLRFFLLF', '207d292defe4615f105c2f30829fdcb0471fdd81': '3\n6 4 U 3 5\n7 1 D 7 0\n4 0 U 7 1\nFLFFFFLLFR\nRLFRFRFFRFF\nRFFR', '1615a81b757dfe51090d68044bd71f28145e60d5': '1\n0 0 D 7 7\nFFLFRLFFFRFFFL', 'f75a18c0f40fcddce4bcd56c5a3abb9da1d1f1d2': '2\n3 4 D 3 7\n3 7 U 3 4\nFFF\nLFFRFFFFRFRFL', '4bba076d3d22d536d140deaaf4619cfed364920f': '3\n4 6 U 5 5\n4 4 U 4 4\n5 5 R 4 6\nFFRRFFRFRFFLRRFFFRFFRR\nRRFFRFRFFLRRFFFRFFRRFL\nRFRFRFFLRRFFFRFFRRFLLF', 'dfe6dabbbcc022a4d765111ffd403b9b04aa291c': '3\n1 1 R 7 6\n2 0 U 7 7\n0 0 D 6 7\nFLRRFRLFLRFFLFLRF\nRRFRLLFFFRFFFL\nFFFFFFFLFFFFLRR'}
  150.     message = "".join(dataset.split()[0:50])
  151.     digest = str(sha1(message.encode("utf-8")).hexdigest())
  152.     try:
  153.         return str(solver[digest])
  154.     except:
  155.         return digest
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement