def __brute_force(key, target): """ Brute-force the lowest value of `n` for which the md5 hash of `secret_key{n}` starts with the specified target value. """ md5_startswith_target = lambda k: md5(k).hexdigest().startswith(target) for n in int_stream(): if md5_startswith_target((key + str(n)).encode()): return n
def part_one(door_id): print('\nHacking door #1 password...') door_password = '' for index in int_stream(0): if (hash := _md5(door_id + str(index))).startswith('00000'): door_password += hash[5] print(door_password) if len(door_password) == 8: print('') # to make the "hacking" look nicer in the terminal return door_password
def part_two(door_id): print('\nHacking door #2 password...') door_password = list('________') for index in int_stream(0): if (hash := _md5(door_id + str(index))).startswith('00000'): try: position = int(hash[5]) char_value = hash[6] if position <= 7 and door_password[position] == '_': door_password[position] = char_value print(''.join(door_password)) except ValueError: continue if not '_' in door_password: print('') # to make the "hacking" look nicer in the terminal return ''.join(door_password)
def part_two(target_present_count): for n in int_stream(): if __get_presents_for_house_with_lazy_elves(n) >= target_present_count: return n
def part_one(target_present_count): for n in int_stream(): if __get_presents_for_house(n) >= target_present_count: return n
def _diagonal_coords_stream(): for target_sum in int_stream(2): for y in int_stream(1, target_sum-1): yield (target_sum - y, y)