Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import unittest
- from day05 import Checker
- class CheckerTest(unittest.TestCase):
- def test_hash_from_inputs(self):
- checker = Checker()
- self.assertEqual(
- checker._hex_hash_from_inputs('abc', 3231929),
- '00000155f8105dff7f56ee10fa9b9abd'
- )
- def test_hex_hash_matches(self):
- checker = Checker()
- self.assertTrue(checker._hex_hash_matches('00000155f8105dff7f56ee10fa9b9abd'))
- self.assertFalse(checker._hex_hash_matches('0000155f8105dff7f56ee10fa9b9abd'))
- @unittest.skip("Part One")
- def test_get_door_password_part_1(self):
- checker = Checker()
- self.assertEqual(checker.get_door_password_part1('abc'), '18f47a30')
- def test_update_door_password(self):
- checker = Checker()
- self.assertEqual(checker._update_door_password('aaaaaaaa', 'b', 4), 'aaaabaaa')
- @unittest.skip("Part Two")
- def test_get_door_password_part_2(self):
- checker = Checker()
- self.assertEqual(checker.get_door_password_part2('abc'), '05ace8e3')
- import hashlib
- from datetime import datetime
- class Checker:
- def _hex_hash_from_inputs(self, base, index):
- md5 = hashlib.md5()
- s = base + str(index)
- md5.update(s.encode('utf-8'))
- return md5.hexdigest()
- def _hex_hash_matches(self, hex_hash):
- if '00000' == hex_hash[0:5]:
- return True
- return False
- def get_door_password_part1(self, base):
- door_password = ''
- i = -1
- for char in range(8):
- looping = True
- while looping:
- i += 1
- h = self._hex_hash_from_inputs(base, i)
- if self._hex_hash_matches(h):
- door_password = door_password + h[5]
- looping = False
- return door_password
- def _update_door_password(self, door_password, new_val, new_index):
- new_index = int(new_index)
- return door_password[:new_index] + new_val + door_password[new_index + 1:]
- def get_door_password_part2(self, base):
- initial_char = '-'
- door_password = initial_char * 8
- i = -1
- pw_index_index = 5
- looping = True
- while looping:
- i += 1
- h = self._hex_hash_from_inputs(base, i)
- if self._hex_hash_matches(h):
- try:
- pw_index = int(h[pw_index_index])
- print('Match found for i=' + str(i) + ' and h=' + h + ' with door_password=' + door_password + ' and pw_index=' + str(pw_index))
- if door_password[pw_index] == initial_char:
- door_password = self._update_door_password(door_password, h[pw_index_index + 1], pw_index)
- print('Current door password:' + door_password + ' for i=' + str(i) + ' and hash=' + h)
- except Exception as e:
- pass
- if door_password.find(initial_char) == -1 or i == 10000000:
- return door_password
- def main():
- input_s = 'ojvtpuvg'
- c = Checker()
- t1 = datetime.now()
- print('Part one:' + c.get_door_password_part1(input_s))
- t2 = datetime.now()
- delta = t2 - t1
- print('Part one time:' + str(delta.total_seconds()))
- t1 = datetime.now()
- print('Part two:' + c.get_door_password_part2(input_s))
- t2 = datetime.now()
- delta = t2 - t1
- print('Part two time:' + str(delta.total_seconds()))
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment