Program to move in a 2d array given instructions, array and starting position Original problem (not in English): https://pokval21.kattis.com/problems/pokval21.robotdammsugaren
How can my code be optimized?
import sys
j = 0
instructions = []
room = []
for i in sys.stdin:
    inp = i.split()
    if(j == 0):
        r = int(inp[0])
        c = int(inp[1])
        k = int(inp[2])
    if(j==1):
        instructions = list(inp[0])
    if(j &gt;= 2):
        room.append(list(inp[0]))
    if(len(room) == r):
        break
    j += 1
def findStartPos(c, room):
    for y, sublist in enumerate(room):
        pos = []
        if c in sublist:
            x = room[y].index(c)
            pos.append(x)
            pos.append(y)
            return pos
    return -1
botPos = findStartPos(&quot;O&quot;, room) # [x, y]
x = botPos[0]
y = botPos[1]
i = 0
visited = [botPos]
clean = 1
append = visited.append
for i in instructions:
    if(i == &quot;&gt;&quot;):
        while room[y][x+1] != &quot;#&quot;:
            if([x+1, y] in visited):
                pass
            else:
                append([x+1, y])
                botPos = [x+1, y]
                clean += 1
            x += 1
        botPos = [x, y]
    elif(i == &quot;&lt;&quot;):
        while room[y][x-1] != &quot;#&quot;:
            if([x-1, y] in visited):
                pass
            else:
                append([x-1, y])
                botPos = [x-1, y]
                clean += 1
            x -= 1
        botPos = [x, y]
    elif(i== &quot;^&quot;):
        while room[y-1][x] != &quot;#&quot;:
            if([x, y-1] in visited):
                pass
            else:
                append([x, y-1])
                botPos = [x, y-1]
                clean += 1
            y -= 1
        botPos = [x, y]
    elif(i == &quot;v&quot;):
        while room[y+1][x] != &quot;#&quot;:
            if([x, y+1] in visited):
                pass
            else:
                append([x, y+1])
                botPos = [x, y+1]
                clean += 1
            y += 1
        botPos = [x, y]
print(clean)

What I'm doing is taking an input where the first line is r c k.
r is the max y value in the 2d array starting from 1, c is the max x value starting from 1 arr[y][x] and k is the amount of instructions.
the second is a line of instructions eg. &lt;&gt;^v for left, right, up and down.
In the function findStartPos I am iterating though the 2d array of strings I created from the rest of the input eg,
[[#, #, #, #], [#, O, #, ., #], [#, . ., ., #], [#, #, ., ., #], [#, #, #, #]]
to find the starting cordinates of O.
In the rest of the code I go though the instructions and moving the O around to count how many unique points it has been on. It can't be on a #. eg.
###### instuction: &lt;
#....# 
#.#.O#
###### instuction: ^
#....# 
#.#O.#
###### instuction: &lt;
#..O.# 
#.#..#
###### instuction: None
#O...# 
#.#..#
been on 5 unique &quot;squares&quot;

Samples:
5 5 4
v&gt;^v
#####
#O#.#
#...#
##..#
#####
returns: 6


8 10 14
&lt;v&gt;^&lt;v&gt;v&lt;^^&gt;&lt;&gt;
##########
#.#......#
#....#...#
##......O#
#........#
#..#.....#
#....#...#
##########
returns: 33

Hope the code is readable enough to be understood.
A loop that does different things based on how many times the loop has executed is a code smell.  It's harder to understand then breaking the loop into discrete steps.
And put it in a function.  Pass in the file, so you can use a file or io.StringIO instead of sys.stdin for testing.
def load_problem(file_input):
    r, c, k = map(int, file_input.readline().strip().split())

    instructions = file_input.readline().strip()

    room = [file_input.readline() for _ in range(r)]

    return room, instructions

Simplified find_start() a bit:
def find_start(room):
    for r,row in enumerate(room):
        c = row.find('O')
        if c &gt;= 0:
            return r,c

Code to solve the problem can go in another function.
A set() is great for keeping track of places you've visited.
The duplication of code can be eliminated by using the instruction to select an x-step (dc) and y-step (dr) to be added to the current position.
STEP = {
    '&gt;':( 0, 1),
    'v':( 1, 0),
    '&lt;':( 0,-1),
    '^':(-1, 0)
}

def solve_problem(room, instructions):
    r, c = find_start(room)
    
    visited = set()
    visited.add((r, c))
    
    for instruction in instructions:
        dr, dc = STEP[instruction]
        
        while room[r + dr][c + dc] != '#':
            r += dr
            c += dc
            visited.add((r, c))

    return len(visited)

Drive the whole thing:
room, instructions = load_problem(sys.stdin)
result = solve_problem(room, instructions)
print(result)