Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Von Ilya, codegolf.se
- import re
- from itertools import izip_longest
- PARSING_REGEX = r"\+?[1-9]-[1-9][a-j]|\+?-[1-9][a-j]|\+?[1-9][a-j]|\+?[a-j]"
- BUILDING_REGEX = r"[1-9]|-[1-9]|[a-j]"
- REFERENCE = "abcdefghij"
- MATERIAL = "++--***..."
- def process(string):
- """takes in a repr of a vertical line (without the +) and returns the line."""
- #strip leading +
- strip = string.lstrip("+")
- tokens = re.findall(BUILDING_REGEX, strip)
- if len(tokens) == 1: #single letter
- letter = tokens[0]
- return MATERIAL[0:REFERENCE.index(letter)+1]
- elif len(tokens) == 2: #letter and integer whitespace offset, or shorthand for subtraction
- offset = abs(int(tokens[0]))
- letter = tokens[1]
- if tokens[0][0] != "-": #it's an offset positive integer
- return (" " * offset) + MATERIAL[0:REFERENCE.index(letter)+1]
- else: #it's shorthand for a whiteout with 0 offset
- return MATERIAL[offset:REFERENCE.index(letter)+1]
- elif len(tokens) == 3: #letter, offset, and whiteout
- offset = int(tokens[0])
- whiteout = abs(int(tokens[1]))
- letter = tokens[2]
- return (" " * offset) + MATERIAL[whiteout:REFERENCE.index(letter)+1]
- def build(string):
- """calls the helper functions to do the work."""
- #we prefix the string with a + so that the parsing loop doesn't skip the first one
- #since we don't have anything down yet.
- #basically a hack to tell make it treat the first one as a plus no matter what
- build_list = re.findall(PARSING_REGEX, "+" + string)
- lines = []
- current_line = ""
- for b in build_list:
- vline = process(b)
- if b[0] == "+":
- current_line = current_line + vline
- else:
- lines.append(current_line)
- current_line = vline
- lines.append(current_line) #append the final one if it ends on a plusser
- return lines
- def display(lines):
- """takes the lines we got and rotates them, making them look pretty."""
- #The old ASCII architect code needed it in this format to work, so I convert
- #it here
- lists = [list(string) for string in lines]
- #I don't know how this line works
- return zip(*[list(n) for n in zip(*[list(tuple) for tuple in list(izip_longest(*lists, fillvalue = " "))])])[::-1]
- def main():
- for x in display(build(raw_input("String?"))):
- print "".join(x)
- if __name__ == "__main__":
- while True:
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement