

#-----------------------------------------------
#lstrings.py - simple Python LStrings implementation
#Copyright (c) 2007, Imri Goldberg
#All rights reserved.
#
#Redistribution and use in source and binary forms,
#with or without modification, are permitted provided
#that the following conditions are met:
#
#    * Redistributions of source code must retain the
#		above copyright notice, this list of conditions
#		and the following disclaimer.
#    * Redistributions in binary form must reproduce the
#		above copyright notice, this list of conditions
#		and the following disclaimer in the documentation
#		and/or other materials provided with the distribution.
#    * Neither the name of the algorithm.co.il nor the names of
#		its contributors may be used to endorse or promote products
#		derived from this software without specific prior written permission.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
#AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
#LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
#DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
#SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
#OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#-----------------------------------------------

FACTOR = 2

UP = 0
LEFT = 1
DOWN = 2
RIGHT = 3

turn_right = lambda direction: (direction-1)%4
turn_left = lambda direction: (direction+1)%4

X_CHANGE = {UP: 0,
			LEFT: -1,
			DOWN: 0,
			RIGHT: 1}

Y_CHANGE = {UP: 1,
			LEFT: 0,
			DOWN: -1,
			RIGHT: 0}


def iterate(s, num):
	s = s.lower()
	orig = s
	for i in range(num):
		s = s.replace("f",orig)
	return s

def create_array(size):
	return [[" " for i in xrange(size*FACTOR)] for j in xrange(size*FACTOR)]

def draw_array(arr, output_filename = None):
	result = "\n".join(["".join(l) for l in arr])
	if output_filename is None:
		print result
	else:
		output_file = file(output_filename,"w")
		output_file.write(result)
		output_file.write("\n")
		output_file.close()

def plot_lstring(s, start_x, start_y, arr):
	cur_x = start_x
	cur_y = start_y
	cur_dir = LEFT
	arr[cur_y%len(arr)][cur_x%len(arr)] = "#"
	for ch in s:
		if ch == "l":
			cur_dir = turn_left(cur_dir)
		elif ch == "r":
			cur_dir = turn_right(cur_dir)
		else:
			new_x, new_y = cur_x, cur_y
			x_change, y_change = X_CHANGE[cur_dir], Y_CHANGE[cur_dir]
			for i in range(1,FACTOR+1):
				new_x,new_y = new_x + x_change, new_y + y_change
				arr[new_y%len(arr)][new_x%len(arr)] = "#"
			cur_x, cur_y = new_x, new_y
