diff options
| author | Mirek Kratochvil <miroslav.kratochvil@matfyz.cuni.cz> | 2026-11-23 16:12:42 +0100 |
|---|---|---|
| committer | Mirek Kratochvil <miroslav.kratochvil@matfyz.cuni.cz> | 2026-11-23 16:12:42 +0100 |
| commit | b4d8b9dcd86d2a886ccad761e0a5f03ccdcaa53d (patch) | |
| tree | 03182bde946616910e1a1cfcafe133d80134dc2d | |
| parent | c1bd86114837a8db3bc502a065d26de1f0768951 (diff) | |
| download | ls47-b4d8b9dcd86d2a886ccad761e0a5f03ccdcaa53d.tar.gz ls47-b4d8b9dcd86d2a886ccad761e0a5f03ccdcaa53d.tar.bz2 | |
| -rwxr-xr-x | ls47.py | 31 |
1 files changed, 20 insertions, 11 deletions
@@ -5,13 +5,22 @@ # # Originally written by Mirek Kratochvil (2017) # Python3 port by Bernhard Esslinger (Feb 2018) +# Vastly redone for the arrow board again by Mirek in 2026 import argparse import random import sys letters = "_ABCDEFGHIJKLMNOPQRSTUVWXYZ.,:?!'()0123456789+-*/" -tiles = list( zip(letters, map(lambda x: ((48 - x) // 7, (49 - x) % 7), range(7 * 7)))) + + +def ix_arr(ix): + if ix >= 0 and ix < 7 * 7: + return ((7 - ix // 7) % 7, (49 - ix) % 7) + raise ValueError(f"Index {ix} too big!") + + +tiles = list(zip(letters, map(ix_arr, range(7 * 7)))) padding_size_min = 5 padding_size_max = 15 @@ -30,19 +39,19 @@ def check_key(key): raise ValueError(f"Letter {c} duplicated in key!") -def find_ix(letter): +def find_arr(letter): m = [l for l in tiles if l[0] == letter] if len(m) != 1: raise ValueError(f"Letter {letter} not in LS47!") - for l, pos in m: - return pos + for l, arr in m: + return arr def find_pos(key, letter): p = key.find(letter) - if p >= 0 and p < 7 * 7: - return (p // 7, p % 7) - raise ValueError(f"Letter {letter} not in key!") + if p < 0 or p >= 7 * 7: + raise ValueError(f"Letter {letter} not in key!") + return (p // 7, p % 7) def add_pos(a, b): @@ -91,7 +100,7 @@ def encrypt(key, plaintext): ciphertext = "" for p in plaintext: pp = find_pos(key, p) - mix = find_ix(find_at_pos(key, mp)) + mix = find_arr(find_at_pos(key, mp)) cp = add_pos(pp, mix) c = find_at_pos(key, cp) ciphertext += c @@ -99,7 +108,7 @@ def encrypt(key, plaintext): key = rotate_right(key, pp[0], 1) cp = find_pos(key, c) key = rotate_down(key, cp[1], 1) - mp = add_pos(mp, find_ix(c)) + mp = add_pos(mp, find_arr(c)) return ciphertext @@ -109,7 +118,7 @@ def decrypt(key, ciphertext): plaintext = "" for c in ciphertext: cp = find_pos(key, c) - mix = find_ix(find_at_pos(key, mp)) + mix = find_arr(find_at_pos(key, mp)) pp = sub_pos(cp, mix) p = find_at_pos(key, pp) plaintext += p @@ -117,7 +126,7 @@ def decrypt(key, ciphertext): key = rotate_right(key, pp[0], 1) cp = find_pos(key, c) key = rotate_down(key, cp[1], 1) - mp = add_pos(mp, find_ix(c)) + mp = add_pos(mp, find_arr(c)) return plaintext |
