Created
June 9, 2018 12:21
-
-
Save LiquidFenrir/b030be43c01b00ba8c9309ff7f5ccb38 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://www.reddit.com/r/dailyprogrammer/comments/8n8tog/20180530_challenge_362_intermediate_route/ | |
from os import system | |
from time import sleep | |
visualization = True | |
def print_grid(grid, x=-1, y=-1): | |
if x != -1 and y != -1: | |
system("cls") | |
print("\x1b[1;1H", end="") | |
for line in grid: | |
print(" " + " ".join(line) + " ") | |
if x != -1 and y != -1: | |
print(f"\x1b[{1+y};{1+x*4}H>") | |
def get_input_data(input_string): | |
input_strings = input_string.split() | |
clockwise = input_strings.pop() == "clockwise" | |
height = int(input_strings.pop()[:-1]) | |
width = int(input_strings.pop()[1:-1]) | |
actual_input = "".join(input_strings) | |
clean_input = [i.upper() for i in actual_input if i.isalpha()] | |
clean_input += ["X"]*((height*width)-len(clean_input)) | |
return ("".join(clean_input), width, height, clockwise) | |
def encrypt(input_string): | |
clean_input, width, height, clockwise = get_input_data(input_string) | |
grid = [] | |
for y in range(height): | |
line = [] | |
for x in range(width): | |
line.append(clean_input[x + y*width]) | |
grid.append(line) | |
output = "" | |
startX = width-1 | |
startY = 0 | |
y = startY | |
x = startX | |
outside_done = 0 | |
while len(output) != height*width: | |
oldx, oldy = x, y | |
char = grid[y][x] | |
output += char | |
direction = "" | |
if clockwise: | |
if outside_done != height//2 and y == outside_done and x == width - outside_done - 2: # If you reached the border | |
direction = "outside_done" | |
outside_done += 1 | |
y += 1 | |
elif y != height - outside_done - 1 and x == width - outside_done - 1: | |
direction = "down CW" | |
y += 1 | |
elif y == height - outside_done - 1 and x != outside_done: | |
direction = "left CW" | |
x -= 1 | |
elif y == outside_done and x != width - outside_done - 2: | |
direction = "right CW" | |
x += 1 | |
elif y != outside_done and x == outside_done: | |
direction = "up CW" | |
y -= 1 | |
else: | |
if outside_done != width//2 and y == outside_done + 1 and x == width - outside_done - 1: | |
direction = "outside_done" | |
outside_done += 1 | |
x -= 1 | |
elif y != height - outside_done - 1 and x == outside_done: | |
direction = "down CCW" | |
y += 1 | |
elif y == height - outside_done - 1 and x != width - outside_done - 1: | |
direction = "right CCW" | |
x += 1 | |
elif y != outside_done and x == width - outside_done - 1: | |
direction = "up CCW" | |
y -= 1 | |
elif y == outside_done: | |
direction = "left CCW" | |
x -= 1 | |
if visualization: | |
print_grid(grid, oldx, oldy) | |
print(f"\x1b[{1+height};{1}H{direction}") | |
sleep(0.2) | |
return output | |
def decrypt(encrypted, width, height, clockwise): | |
grid = [[" " for x in range(width)] for y in range(height)] | |
print_grid(grid) | |
def grid_complete(): | |
for line in grid: | |
for char in line: | |
if char == " ": | |
return False | |
return True | |
startX = width-1 | |
startY = 0 | |
y = startY | |
x = startX | |
outside_done = 0 | |
cnt = 0 | |
while not grid_complete(): | |
oldx, oldy = x, y | |
grid[y][x] = encrypted[cnt] | |
cnt += 1 | |
direction = "" | |
if clockwise: | |
if outside_done != height//2 and y == outside_done and x == width - outside_done - 2: # If you reached the border | |
direction = "outside_done" | |
outside_done += 1 | |
y += 1 | |
elif y != height - outside_done - 1 and x == width - outside_done - 1: | |
direction = "down CW" | |
y += 1 | |
elif y == height - outside_done - 1 and x != outside_done: | |
direction = "left CW" | |
x -= 1 | |
elif y == outside_done and x != width - outside_done - 2: | |
direction = "right CW" | |
x += 1 | |
elif y != outside_done and x == outside_done: | |
direction = "up CW" | |
y -= 1 | |
else: | |
if outside_done != width//2 and y == outside_done + 1 and x == width - outside_done - 1: | |
direction = "outside_done" | |
outside_done += 1 | |
x -= 1 | |
elif y != height - outside_done - 1 and x == outside_done: | |
direction = "down CCW" | |
y += 1 | |
elif y == height - outside_done - 1 and x != width - outside_done - 1: | |
direction = "right CCW" | |
x += 1 | |
elif y != outside_done and x == width - outside_done - 1: | |
direction = "up CCW" | |
y -= 1 | |
elif y == outside_done: | |
direction = "left CCW" | |
x -= 1 | |
if visualization: | |
print_grid(grid, oldx, oldy) | |
print(f"\x1b[{1+height};{1}H{direction}") | |
sleep(0.2) | |
output = "" | |
for line in grid: | |
output += "".join(line) | |
return output | |
def my_assert(A, B): | |
print(f"Comparing {A} to {B}") | |
assert(A == B) | |
sleep(1) | |
def main(): | |
inputs = [ | |
"\"WE ARE DISCOVERED. FLEE AT ONCE\" (9, 3) clockwise", | |
"\"why is this professor so boring omg\" (6, 5) counter-clockwise", | |
"\"Solving challenges on r/dailyprogrammer is so much fun!!\" (8, 6) counter-clockwise", | |
"\"For lunch let's have peanut-butter and bologna sandwiches\" (4, 12) clockwise", | |
"\"I've even witnessed a grown man satisfy a camel\" (9, 5) clockwise", | |
"\"Why does it say paper jam when there is no paper jam?\" (3, 14) counter-clockwise", | |
] | |
outputs = [ | |
"CEXXECNOTAEOWEAREDISLFDEREV", | |
"TSIYHWHFSNGOMGXIRORPSIEOBOROSS", | |
"CGNIVLOSHSYMUCHFUNXXMMLEGNELLAOPERISSOAIADRNROGR", | |
"LHSENURBGAISEHCNNOATUPHLUFORCTVABEDOSWDALNTTEAEN", | |
"IGAMXXXXXXXLETRTIVEEVENWASACAYFSIONESSEDNAMNW", | |
"YHWDSSPEAHTRSPEAMXJPOIENWJPYTEOIAARMEHENAR", | |
] | |
for input, output in zip(inputs, outputs): | |
my_assert(encrypt(input), output) | |
clean_input, width, height, clockwise = get_input_data(input) | |
my_assert(decrypt(output, width, height, clockwise), clean_input) | |
if __name__=="__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment