Skip to content

Instantly share code, notes, and snippets.

@YtvwlD
Last active April 6, 2018 17:22
Show Gist options
  • Save YtvwlD/f7cf0d71967a5909c14a0cda4e6ef392 to your computer and use it in GitHub Desktop.
Save YtvwlD/f7cf0d71967a5909c14a0cda4e6ef392 to your computer and use it in GitHub Desktop.
base64 convergence
#!/usr/bin/env python3
from base64 import b64encode
from time import sleep
from abc import abstractmethod
import curses
values = [
{
"input": b"a",
"current": None,
},
{
"input": b"b",
"current": None,
}
]
iterations = 0
same_index = 0
class DisplayError(Exception):
pass
class UI:
@abstractmethod
def __init__(self):
pass
@abstractmethod
def display(self, values, iterations):
pass
class StdoutUI(UI):
LINE = "----------------------"
def __init__(self):
pass
def display(self, values, iterations):
print(self.LINE)
print(f"Iterations: {iterations}")
print(f"Same up to index: {same_index}")
print("Currently same:", values[0]["current"][0:same_index].decode())
class TerminalUI(UI):
LINE = "----------------------"
def __init__(self):
self._clear()
def display(self, values, iterations):
self._clear()
print(f"Iterations: {iterations}")
print(f"Same up to index: {same_index}")
print(self.LINE)
print(values[0]["current"][0:same_index].decode())
def _clear(self):
print("\x1b[2J\x1b[H", end="")
class CursesUI(UI):
def __init__(self):
root_window = curses.initscr()
root_window.clear()
self.top_window = root_window.derwin(5, root_window.getmaxyx()[1], 0, 0)
self.top_window.box()
bottom_window = root_window.derwin(root_window.getmaxyx()[0] - self.top_window.getmaxyx()[0], root_window.getmaxyx()[1], self.top_window.getmaxyx()[0], 0)
line_width = bottom_window.getmaxyx()[1]//2
left_window = bottom_window.derwin(bottom_window.getmaxyx()[0], line_width, 0, 0)
left_window.box()
self.left_window = left_window.derwin(left_window.getmaxyx()[0]-2, left_window.getmaxyx()[1]-2, 1, 1)
right_window = bottom_window.derwin(bottom_window.getmaxyx()[0], line_width, 0, bottom_window.getmaxyx()[1]//2)
right_window.box()
self.right_window = right_window.derwin(right_window.getmaxyx()[0]-2, right_window.getmaxyx()[1]-2, 1, 1)
root_window.refresh()
def display(self, values, iterations):
display_error = False
self.top_window.addstr(2, 2, f"Iterations: {iterations}")
self.top_window.addstr(2, self.top_window.getmaxyx()[1] - 50, f"Same up to index: {same_index}")
self.top_window.refresh()
self.left_window.clear()
try:
self.left_window.addstr(values[0]["current"][0:same_index], curses.A_BOLD)
self.left_window.addstr(values[0]["current"][same_index:])
except curses.error:
display_error = True
self.left_window.refresh()
self.right_window.clear()
try:
self.right_window.addstr(values[1]["current"][0:same_index], curses.A_BOLD)
self.right_window.addstr(values[1]["current"][same_index:])
except curses.error:
display_error = True
line = 0
self.right_window.refresh()
if display_error:
raise DisplayError
def calculate_same():
same_index = 0
for i in range(min([len(v["current"]) for v in values])):
if not all([v["current"][i] == values[0]["current"][i] for v in values]):
break
same_index = i
return same_index
for v in values:
v["current"] = v["input"]
ui = CursesUI()
try:
while True:
ui.display(values, iterations)
for v in values:
v["current"] = b64encode(v["current"])
iterations += 1
same_index = calculate_same()
sleep(0.5)
except Exception as exc:
curses.endwin()
print("End:")
print(f"{iterations} Iterations\tSame up to index: {same_index}")
print(values[0]["current"][0:same_index])
raise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment