Skip to content

Instantly share code, notes, and snippets.

@maestroviktorin
Created January 9, 2023 19:16
Show Gist options
  • Save maestroviktorin/3330f7c8b3d78353713eaf71a36b8596 to your computer and use it in GitHub Desktop.
Save maestroviktorin/3330f7c8b3d78353713eaf71a36b8596 to your computer and use it in GitHub Desktop.
Solution to the problem #5834 by E. Usov from the website of K. Polyakov.
# Helpful hashmaps.
HEX_DEC = dict(zip('0123456789abcdef', tuple(range(16)))) # Pairs of digits - `hexadecimal`: `decimal`
DEC_HEX = dict(zip(tuple(range(16)), '0123456789abcdef')) # Pairs of digits - `decimal`: `hexadecimal`
# Implementation of the algorithm described in the problem.
def detective(n: int) -> str:
hex_n = hex(n)[2:]
hex_n += '0' if n % 2 else 'f'
for _ in range(2):
hex_n += DEC_HEX[sum(HEX_DEC[x] for x in hex_n) % 16]
return hex_n
# The main program.
number = 1
while True:
tmp = detective(number)
if tmp.count(max(tmp, key=HEX_DEC.get)) == tmp.count(min(tmp, key=HEX_DEC.get)) * 5:
print(number)
break
number += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment