Advertisement
Guest User

ascii architect

a guest
Aug 20th, 2014
480
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.24 KB | None | 0 0
  1. #Von Ilya, codegolf.se
  2.  
  3. import re
  4. from itertools import izip_longest
  5.  
  6. PARSING_REGEX = r"\+?[1-9]-[1-9][a-j]|\+?-[1-9][a-j]|\+?[1-9][a-j]|\+?[a-j]"
  7. BUILDING_REGEX = r"[1-9]|-[1-9]|[a-j]"
  8. REFERENCE = "abcdefghij"
  9. MATERIAL = "++--***..."
  10.  
  11. def process(string):
  12.     """takes in a repr of a vertical line (without the +) and returns the line."""
  13.     #strip leading +
  14.     strip = string.lstrip("+")
  15.     tokens = re.findall(BUILDING_REGEX, strip)
  16.     if len(tokens) == 1: #single letter
  17.         letter = tokens[0]
  18.         return MATERIAL[0:REFERENCE.index(letter)+1]
  19.     elif len(tokens) == 2: #letter and integer whitespace offset, or shorthand for subtraction
  20.         offset = abs(int(tokens[0]))
  21.         letter = tokens[1]
  22.         if tokens[0][0] != "-": #it's an offset positive integer
  23.             return (" " * offset) + MATERIAL[0:REFERENCE.index(letter)+1]
  24.         else: #it's shorthand for a whiteout with 0 offset
  25.             return MATERIAL[offset:REFERENCE.index(letter)+1]
  26.     elif len(tokens) == 3: #letter, offset, and whiteout
  27.         offset = int(tokens[0])
  28.         whiteout = abs(int(tokens[1]))
  29.         letter = tokens[2]
  30.         return (" " * offset) + MATERIAL[whiteout:REFERENCE.index(letter)+1]
  31.  
  32. def build(string):
  33.     """calls the helper functions to do the work."""
  34.     #we prefix the string with a + so that the parsing loop doesn't skip the first one
  35.     #since we don't have anything down yet.
  36.     #basically a hack to tell make it treat the first one as a plus no matter what
  37.     build_list = re.findall(PARSING_REGEX, "+" + string)
  38.     lines = []
  39.     current_line = ""
  40.     for b in build_list:
  41.         vline = process(b)
  42.         if b[0] == "+":
  43.             current_line = current_line + vline
  44.         else:
  45.             lines.append(current_line)
  46.             current_line = vline
  47.     lines.append(current_line) #append the final one if it ends on a plusser
  48.     return lines
  49.  
  50. def display(lines):
  51.     """takes the lines we got and rotates them, making them look pretty."""
  52.     #The old ASCII architect code needed it in this format to work, so I convert
  53.     #it here
  54.     lists = [list(string) for string in lines]
  55.     #I don't know how this line works
  56.     return zip(*[list(n) for n in zip(*[list(tuple) for tuple in list(izip_longest(*lists, fillvalue = " "))])])[::-1]
  57.  
  58. def main():
  59.     for x in display(build(raw_input("String?"))):
  60.         print "".join(x)
  61.  
  62. if __name__ == "__main__":
  63.     while True:
  64.         main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement