Skip to content

Instantly share code, notes, and snippets.

@feiz
Last active June 1, 2017 09:18
Show Gist options
  • Save feiz/2380cdd2901fa5f2b033b9c3d64021bb to your computer and use it in GitHub Desktop.
Save feiz/2380cdd2901fa5f2b033b9c3d64021bb to your computer and use it in GitHub Desktop.
:fuji::moto::you::ichi:
#!/usr/local/bin/python3
import sys
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('text', type=str, default=sys.stdin, nargs='?')
grp = parser.add_mutually_exclusive_group(required=True)
grp.add_argument('--decode', '-d', action='store_true', default=True)
grp.add_argument('--encode', '-e', action='store_true', default=False)
glyph = {
'fuji': 0,
'moto': 1,
'you': 2,
'ichi': 3,
}
reverse_glyph = {
v: k
for k, v in glyph.items()
}
def divmod(c):
result = []
while c:
m = c % len(glyph)
result = [m] + result
c //= len(glyph)
return ([0] * (4 - len(result))) + result
def encode(text):
if isinstance(text, str):
text = text.encode('utf8')
encoded_text = []
for c in text:
encoded_text += divmod(c)
return ''.join(map(lambda x: ':{}:'.format(reverse_glyph[x]), encoded_text))
def decode(fuji):
"""decode youichiencoded string"""
words = list(filter(bool, fuji.split(':')))
if set(words) != set(glyph.keys()):
raise ValueError("invalid fujimoto")
result = b''
s = 0
while s < len(words):
char = int(''.join(map(lambda x: '{:02b}'.format(glyph[x]), words[s:s+4])), 2)
result += char.to_bytes(1, 'big')
s += 4
return result.decode('utf8')
if __name__ == '__main__':
args = parser.parse_args()
intext = args.text
if not isinstance(intext, str):
intext = intext.read()
intext = intext.strip()
if args.encode:
print(encode(intext))
elif args.decode:
print(decode(intext))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment