Guest User

Untitled

a guest
Dec 5th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. import unittest
  2.  
  3. from day05 import Checker
  4.  
  5. class CheckerTest(unittest.TestCase):
  6.  
  7. def test_hash_from_inputs(self):
  8. checker = Checker()
  9. self.assertEqual(
  10. checker._hex_hash_from_inputs('abc', 3231929),
  11. '00000155f8105dff7f56ee10fa9b9abd'
  12. )
  13.  
  14. def test_hex_hash_matches(self):
  15. checker = Checker()
  16. self.assertTrue(checker._hex_hash_matches('00000155f8105dff7f56ee10fa9b9abd'))
  17. self.assertFalse(checker._hex_hash_matches('0000155f8105dff7f56ee10fa9b9abd'))
  18.  
  19. @unittest.skip("Part One")
  20. def test_get_door_password_part_1(self):
  21.  
  22. checker = Checker()
  23. self.assertEqual(checker.get_door_password_part1('abc'), '18f47a30')
  24.  
  25. def test_update_door_password(self):
  26. checker = Checker()
  27. self.assertEqual(checker._update_door_password('aaaaaaaa', 'b', 4), 'aaaabaaa')
  28.  
  29. @unittest.skip("Part Two")
  30. def test_get_door_password_part_2(self):
  31.  
  32. checker = Checker()
  33. self.assertEqual(checker.get_door_password_part2('abc'), '05ace8e3')
  34.  
  35. import hashlib
  36. from datetime import datetime
  37.  
  38.  
  39. class Checker:
  40.  
  41. def _hex_hash_from_inputs(self, base, index):
  42. md5 = hashlib.md5()
  43. s = base + str(index)
  44. md5.update(s.encode('utf-8'))
  45. return md5.hexdigest()
  46.  
  47. def _hex_hash_matches(self, hex_hash):
  48. if '00000' == hex_hash[0:5]:
  49. return True
  50. return False
  51.  
  52. def get_door_password_part1(self, base):
  53. door_password = ''
  54. i = -1
  55. for char in range(8):
  56. looping = True
  57. while looping:
  58. i += 1
  59. h = self._hex_hash_from_inputs(base, i)
  60. if self._hex_hash_matches(h):
  61. door_password = door_password + h[5]
  62. looping = False
  63.  
  64. return door_password
  65.  
  66. def _update_door_password(self, door_password, new_val, new_index):
  67. new_index = int(new_index)
  68. return door_password[:new_index] + new_val + door_password[new_index + 1:]
  69.  
  70. def get_door_password_part2(self, base):
  71.  
  72. initial_char = '-'
  73. door_password = initial_char * 8
  74. i = -1
  75.  
  76. pw_index_index = 5
  77.  
  78. looping = True
  79. while looping:
  80. i += 1
  81. h = self._hex_hash_from_inputs(base, i)
  82. if self._hex_hash_matches(h):
  83. try:
  84. pw_index = int(h[pw_index_index])
  85. print('Match found for i=' + str(i) + ' and h=' + h + ' with door_password=' + door_password + ' and pw_index=' + str(pw_index))
  86. if door_password[pw_index] == initial_char:
  87. door_password = self._update_door_password(door_password, h[pw_index_index + 1], pw_index)
  88. print('Current door password:' + door_password + ' for i=' + str(i) + ' and hash=' + h)
  89.  
  90. except Exception as e:
  91. pass
  92.  
  93. if door_password.find(initial_char) == -1 or i == 10000000:
  94. return door_password
  95.  
  96.  
  97.  
  98. def main():
  99. input_s = 'ojvtpuvg'
  100.  
  101. c = Checker()
  102. t1 = datetime.now()
  103. print('Part one:' + c.get_door_password_part1(input_s))
  104. t2 = datetime.now()
  105. delta = t2 - t1
  106. print('Part one time:' + str(delta.total_seconds()))
  107.  
  108. t1 = datetime.now()
  109. print('Part two:' + c.get_door_password_part2(input_s))
  110. t2 = datetime.now()
  111. delta = t2 - t1
  112. print('Part two time:' + str(delta.total_seconds()))
  113.  
  114.  
  115. if __name__ == '__main__':
  116. main()
Add Comment
Please, Sign In to add comment