Skip to content

Instantly share code, notes, and snippets.

@colejhudson
Last active June 14, 2021 08:30
Show Gist options
  • Save colejhudson/9e6313f406ab2e24d2353e9129cda83b to your computer and use it in GitHub Desktop.
Save colejhudson/9e6313f406ab2e24d2353e9129cda83b to your computer and use it in GitHub Desktop.
'''
On the 21st of April, in the year of our lord 2021, Amulet v1.1
was released into the world. Naturally, I wrote a dead simple
mining program for it.
The following program will take an arbitary phrase and surround
it with random characters until it can be satisfactorily
considered an 'amulet'.
[0]: https://text.bargains/amulet/
'''
import hashlib as hl
import string
import random
import re
AMULET_MAX_LEN = 64
class Poem(str):
def __init__(self, *args, **kwargs):
return super(Poem, self).__init__(*args, **kwargs)
def to_hash(self):
return hl.sha256(bytes(self, 'ascii')).hexdigest()
def is_amulet(self):
digest = self.to_hash()
if len(digest) > AMULET_MAX_LEN:
return False
if not re.search(r'8888+', digest):
return False
return True
class Prospector:
def __init__(self, phrase, alphabet=string.printable[:-2]):
assert(len(phrase) <= AMULET_MAX_LEN)
self.phrase = phrase
self.alphabet = alphabet
def mine(self):
string = random.sample(self.alphabet, AMULET_MAX_LEN)
phrase_len = len(self.phrase)
pos = random.randrange(0, AMULET_MAX_LEN - phrase_len)
poem = string[:pos] + list(phrase) + string[pos + phrase_len:]
return Poem(''.join(poem))
if __name__ == '__main__':
phrase = ' Huzzah! An amulet!! '
miner = Prospector(phrase)
print('Prospecting...')
try:
while True:
poem = miner.mine()
if poem.is_amulet():
digest = poem.to_hash()
match = re.search(r'8888+', digest)
eights = match.group(0)
rarity = len(eights)
message = digest[:match.start(0)]
+ '\033[92m'
+ eights
+ '\033[0m'
+ digest[match.end(0):]
print('{} is an amulet with {} eights!'.format(message, rarity))
except KeyboardInterrupt:
print('All in a days work.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment