Transparent Origami - Advent of Code 2021 Day 13

11st Diner in South Beach is an excellent Diner for breakfast. Would recommend the Cortado
11th Diner in South Beach is an excellent Diner for breakfast. Would recommend the Cortado
11th Street Diner, Miami Beach

In today's advent of code problem, we are given coordinates of points on a transparent paper and a series of folds that need to be made. Once all the folds are complete, the points will form text that is the solution to the problem.

If we are to draw the problem, we will note that the points move with each fold. We will also note that only points that lie past the fold point need to be moved or transposed

The function below accepts the coordinates of a point along with the type of fold and the fold position. If it meets the criteria it is transposed, otherwise the point is return unchanged. We can iterate through all the folds and apply the function on all the set of points.

def transpose(fold,x,y,foldAxis):
    
    if foldAxis == 'x' and x > fold:
        newX = fold - (x - fold)
        return (newX,y)
    elif foldAxis == 'y' and y > fold:
        newY = fold - (y - fold)
        return (x,newY)
    else:
        return (x,y)


Once, we have processed all the folds, we can display the points as shown in the following function, which simply builds each line in an array and prints it when complete

# flipped this since the output appears to be a mirror image
for y in range(0,lastFoldY):
    line = []
    for x in range(0,lastFoldX):
        if (x,y) in coordinates:
            line.append('#')
        else:
            line.append(' ')
    print("".join(line))


The solution -

#     ##  #  # ####  ##  #  # ####   ##
#    #  # #  # #    #  # #  # #       #
#    #    #### ###  #    #  # ###     #
#    # ## #  # #    # ## #  # #       #
#    #  # #  # #    #  # #  # #    #  #
####  ### #  # ####  ###  ##  ####  ##


The source code can be found here -

https://github.com/jasoncoelho/adventOfCode2021/blob/main/13/run.py