# AHO_cipher.py
# AHO暗号のエンコード / デコード
PAIR_TO_CHAR = {
"🟥🟦": "A", "🟥🟨": "G", "🟥🟩": "M", "🟥🟧": "S", "🟥🟪": "Y",
"🟦🟥": "B", "🟦🟨": "H", "🟦🟩": "N", "🟦🟧": "T", "🟦🟪": "Z",
"🟨🟥": "C", "🟨🟦": "I", "🟨🟩": "O", "🟨🟧": "U", "🟨🟪": "!",
"🟩🟥": "D", "🟩🟦": "J", "🟩🟨": "P", "🟩🟧": "V", "🟩🟪": "?",
"🟧🟥": "E", "🟧🟦": "K", "🟧🟨": "Q", "🟧🟩": "W", "🟧🟪": "(",
"🟪🟥": "F", "🟪🟦": "L", "🟪🟨": "R", "🟪🟩": "X", "🟪🟧": ")",
}
# 逆引き辞書(文字 -> 色ペア)
CHAR_TO_PAIR = {v: k for k, v in PAIR_TO_CHAR.items()}
def decode(color_sequence: str, sep_whitespace=True) -> str:
"""
色ペア列をデコードして文字列を返す。
color_sequence: "🟦🟨 🟥🟦 🟩🟨" のようにスペース区切りで渡すのが標準。
sep_whitespace=True のとき空白で分割。False のとき2絵文字ずつ切る(空白が無い場合に試す)。
"""
# トリム
s = color_sequence.strip()
pairs = []
if sep_whitespace and any(ch.isspace() for ch in s):
# 空白で分割(普通はこちら)
parts = s.split()
for p in parts:
pairs.append(p)
else:
# 空白無しなら2絵文字(2グリフ)ずつ切る試み
# ここでは絵文字が1コードポイントとは限らないので安全策として既知の色絵文字セットでスキャン
colors = ["🟥","🟦","🟨","🟩","🟧","🟪"]
i = 0
while i < len(s):
# まず1グリフ取得(絵文字は1か2コードだがここでは色絵文字に限定)
if s[i] in colors and i+1 < len(s) and s[i+1] in colors:
pairs.append(s[i:i+2])
i += 2
else:
# 不明な文字があれば飛ばす
i += 1
# 各ペアを文字に変換
result_chars = []
for p in pairs:
ch = PAIR_TO_CHAR.get(p, "?") # 未定義は "?" に
result_chars.append(ch)
return "".join(result_chars)
def encode(text: str, sep=" ") -> str:
"""
文字列を色ペア列にエンコードして返す。
未定義の文字は "?" を返すか、skip=Falseにすればスキップできる(ここでは ? を使用)。
"""
out_pairs = []
for ch in text.upper():
if ch == " ":
out_pairs.append("/") # スペースは区切りマーク(任意) -- 表示しやすくするため
continue
pair = CHAR_TO_PAIR.get(ch, None)
if pair is None:
out_pairs.append("?")
else:
out_pairs.append(pair)
# 出力はスペース区切り(デフォルト)
return sep.join(out_pairs)
# --- テスト/例 ---
if __name__ == "__main__":
# デコード例(あなたの例)
example1 = "🟦🟨 🟥🟦 🟩🟨 🟩🟨 🟥🟪"
example2 = "🟦🟨 🟧🟥 🟪🟦 🟩🟨"
print("例1 デコード:", decode(example1)) # HAPPY
print("例2 デコード:", decode(example2)) # HELP
# エンコード例
print("エンコード HAPPY:", encode("HAPPY"))
print("エンコード HELP:", encode("HELP"))
To embed this project on your website, copy the following code and paste it into your website's HTML: