Guest User

Fifteen Puzzle solve.py

a guest
Feb 4th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. #!/usr/bin/python3
  2. def is_soluble (digits) :
  3.     print(digits)
  4.     inv = 0
  5.     for i in range(16):
  6.         if (digits[i] != 0):
  7.             for j in range(i):
  8.                 if (digits[j] > digits[i]):
  9.                     inv += 1
  10.     for i in range(16):
  11.         if (digits[i] == 0):
  12.             inv += 1 + i/4
  13.     if (int(inv) & 1):
  14.         return '1'
  15.     else :
  16.         return '0'
  17. file = open('Downloads/puzzles.txt')
  18. lines = file.readlines()
  19. flag = ''
  20. abcd = 0
  21. digits = []
  22. num = ''
  23. for i in range(len(lines)):
  24.     if (abcd == 4):
  25.         flag += is_soluble(digits)
  26.         digits = []
  27.         abcd = 0
  28.     if ('bit' in lines[i] or '-' in lines[i] or '-' in lines[i] or lines[i].replace(' ','') == '\n'):
  29.         continue
  30.     lines[i] = lines[i].replace('  ','0')
  31.     for sym in lines[i]:
  32.         try :
  33.             int(sym)
  34.         except :
  35.             if (num != ''):
  36.                 digits.append(int(num))
  37.                 num = ''
  38.         else :
  39.             num += sym
  40.     abcd += 1
  41. print(flag)
  42. flag = int(flag, 2)
  43. print('SharifCTF{%s}' % hex(flag).replace('0x',''))
Advertisement
Add Comment
Please, Sign In to add comment