


"""simple docstring"""







from __future__ import annotations

a__				:							Any						=     [
    [-1, 0],  # left
    [0, -1],  # down
    [1, 0],  # right
    [0, 1],  # up
]




def    UpperCAmelCase__       (lowerCAmelCase_  ,						lowerCAmelCase_  ,						lowerCAmelCase_  ,						lowerCAmelCase_  ,						lowerCAmelCase_  ,						):




					'''simple docstring'''


					__SCREAMING_SNAKE_CASE						   =				[
					    [0 for col in range(len(grid[0]   )   )] for row in range(len(lowerCAmelCase_   )   )
					]  # the reference grid
					__SCREAMING_SNAKE_CASE						   =				1
					__SCREAMING_SNAKE_CASE						   =				[
					    [0 for col in range(len(grid[0]   )   )] for row in range(len(lowerCAmelCase_   )   )
					]  # the action grid

					__SCREAMING_SNAKE_CASE						   =				init[0]
					__SCREAMING_SNAKE_CASE						   =				init[1]
					__SCREAMING_SNAKE_CASE						   =				0
					__SCREAMING_SNAKE_CASE						   =				g + heuristic[x][y]  # cost from starting cell to destination cell
					__SCREAMING_SNAKE_CASE						   =				[[f, g, x, y]]

					__SCREAMING_SNAKE_CASE						   =				False  # flag that is set when search is complete
					__SCREAMING_SNAKE_CASE						   =				False  # flag set if we can't find expand

					while not found and not resign:
										if len(lowerCAmelCase_   ) == 0:
															raise ValueError("Algorithm is unable to find solution"   )
										else:  # to choose the least costliest action so as to move closer to the goal
															cell.sort()
															cell.reverse()
															__SCREAMING_SNAKE_CASE						   =				cell.pop()
															__SCREAMING_SNAKE_CASE						   =				next_cell[2]
															__SCREAMING_SNAKE_CASE						   =				next_cell[3]
															__SCREAMING_SNAKE_CASE						   =				next_cell[1]

															if x == goal[0] and y == goal[1]:
																				__SCREAMING_SNAKE_CASE						   =				True
															else:
																				for i in range(len(lowerCAmelCase_   )   ):  # to try out different valid actions
																									__SCREAMING_SNAKE_CASE						   =				x + DIRECTIONS[i][0]
																									__SCREAMING_SNAKE_CASE						   =				y + DIRECTIONS[i][1]
																									if xa >= 0 and xa < len(lowerCAmelCase_   ) and ya >= 0 and ya < len(grid[0]   ):
																														if closed[xa][ya] == 0 and grid[xa][ya] == 0:
																																			__SCREAMING_SNAKE_CASE						   =				g + cost
																																			__SCREAMING_SNAKE_CASE						   =				ga + heuristic[xa][ya]
																																			cell.append([fa, ga, xa, ya]   )
																																			__SCREAMING_SNAKE_CASE						   =				1
																																			__SCREAMING_SNAKE_CASE						   =				i
					__SCREAMING_SNAKE_CASE						   =				[]
					__SCREAMING_SNAKE_CASE						   =				goal[0]
					__SCREAMING_SNAKE_CASE						   =				goal[1]
					invpath.append([x, y]   )  # we get the reverse path from here
					while x != init[0] or y != init[1]:
										__SCREAMING_SNAKE_CASE						   =				x - DIRECTIONS[action[x][y]][0]
										__SCREAMING_SNAKE_CASE						   =				y - DIRECTIONS[action[x][y]][1]
										__SCREAMING_SNAKE_CASE						   =				xa
										__SCREAMING_SNAKE_CASE						   =				ya
										invpath.append([x, y]   )

					__SCREAMING_SNAKE_CASE						   =				[]
					for i in range(len(lowerCAmelCase_   )   ):
										path.append(invpath[len(lowerCAmelCase_   ) - 1 - i]   )
					return path, action


if __name__ == "__main__":
		a__				:							Tuple						=     [
		    [0, 1, 0, 0, 0, 0],
		    [0, 1, 0, 0, 0, 0],  # 0 are free path whereas 1's are obstacles
		    [0, 1, 0, 0, 0, 0],
		    [0, 1, 0, 0, 1, 0],
		    [0, 0, 0, 0, 1, 0],
		]

		a__				:							List[str]						=     [0, 0]
		# all coordinates are given in format [y,x]
		a__				:							str						=     [len(grid) - 1, len(grid[0]) - 1]
		a__				:							int						=     1

		# the cost map which pushes the path closer to the goal
		a__				:							Optional[int]						=     [[0 for row in range(len(grid[0]))] for col in range(len(grid))]
		for i in range(len(grid)):
				for j in range(len(grid[0])):
						a__				:							Tuple						=     abs(i - goal[0]) + abs(j - goal[1])
						if grid[i][j] == 1:
								# added extra penalty in the heuristic map
								a__				:							List[Any]						=     9_9

		a__			,     a__				:							List[Any]						=     search(grid, init, goal, cost, heuristic)

		print('''ACTION MAP''')
		for i in range(len(action)):
				print(action[i])

		for i in range(len(path)):
				print(path[i])




"""simple docstring"""







from __future__ import annotations

import pandas as pd




def    UpperCAmelCase__       (lowerCAmelCase_  ,						lowerCAmelCase_  ,						lowerCAmelCase_   ):




					'''simple docstring'''


					__SCREAMING_SNAKE_CASE						   =				[0] * no_of_processes
					__SCREAMING_SNAKE_CASE						   =				[0] * no_of_processes
					# Copy the burst time into remaining_time[]
					for i in range(lowerCAmelCase_   ):
										__SCREAMING_SNAKE_CASE						   =				burst_time[i]

					__SCREAMING_SNAKE_CASE						   =				0
					__SCREAMING_SNAKE_CASE						   =				0
					__SCREAMING_SNAKE_CASE						   =				9_9999_9999
					__SCREAMING_SNAKE_CASE						   =				0
					__SCREAMING_SNAKE_CASE						   =				False

					# Process until all processes are completed
					while complete != no_of_processes:
										for j in range(lowerCAmelCase_   ):
															if arrival_time[j] <= increment_time and remaining_time[j] > 0:
																				if remaining_time[j] < minm:
																									__SCREAMING_SNAKE_CASE						   =				remaining_time[j]
																									__SCREAMING_SNAKE_CASE						   =				j
																									__SCREAMING_SNAKE_CASE						   =				True

										if not check:
															increment_time += 1
															continue
										remaining_time[short] -= 1

										__SCREAMING_SNAKE_CASE						   =				remaining_time[short]
										if minm == 0:
															__SCREAMING_SNAKE_CASE						   =				9_9999_9999

										if remaining_time[short] == 0:
															complete += 1
															__SCREAMING_SNAKE_CASE						   =				False

															# Find finish time of current process
															__SCREAMING_SNAKE_CASE						   =				increment_time + 1

															# Calculate waiting time
															__SCREAMING_SNAKE_CASE						   =				finish_time - arrival_time[short]
															__SCREAMING_SNAKE_CASE						   =				finar - burst_time[short]

															if waiting_time[short] < 0:
																				__SCREAMING_SNAKE_CASE						   =				0

        # Increment time
										increment_time += 1
					return waiting_time




def    UpperCAmelCase__       (lowerCAmelCase_  ,						lowerCAmelCase_  ,						lowerCAmelCase_   ):




					'''simple docstring'''


					__SCREAMING_SNAKE_CASE						   =				[0] * no_of_processes
					for i in range(lowerCAmelCase_   ):
										__SCREAMING_SNAKE_CASE						   =				burst_time[i] + waiting_time[i]
					return turn_around_time




def    UpperCAmelCase__       (lowerCAmelCase_  ,						lowerCAmelCase_  ,						lowerCAmelCase_   ):




					'''simple docstring'''


					__SCREAMING_SNAKE_CASE						   =				0
					__SCREAMING_SNAKE_CASE						   =				0
					for i in range(lowerCAmelCase_   ):
										__SCREAMING_SNAKE_CASE						   =				total_waiting_time + waiting_time[i]
										__SCREAMING_SNAKE_CASE						   =				total_turn_around_time + turn_around_time[i]
					print(f"""Average waiting time = {total_waiting_time / no_of_processes:.5f}"""   )
					print("Average turn around time ="  ,						total_turn_around_time / no_of_processes   )


if __name__ == "__main__":
		print('''Enter how many process you want to analyze''')
		a__				:							Optional[Any]						=     int(input())
		a__				:							Optional[int]						=     [0] * no_of_processes
		a__				:							int						=     [0] * no_of_processes
		a__				:							List[Any]						=     list(range(1, no_of_processes + 1))

		for i in range(no_of_processes):
				print('''Enter the arrival time and burst time for process:--''' + str(i + 1))
				a__			,     a__				:							Tuple						=     map(int, input().split())

		a__				:							int						=     calculate_waitingtime(arrival_time, burst_time, no_of_processes)

		a__				:							Dict						=     burst_time
		a__				:							Any						=     no_of_processes
		a__				:							Optional[int]						=     waiting_time
		a__				:							Union[str, Any]						=     calculate_turnaroundtime(bt, n, wt)

		calculate_average_times(waiting_time, turn_around_time, no_of_processes)

		a__				:							str						=     pd.DataFrame(
		    list(zip(processes, burst_time, arrival_time, waiting_time, turn_around_time)),
		    columns=[
		        '''Process''',
		        '''BurstTime''',
		        '''ArrivalTime''',
		        '''WaitingTime''',
		        '''TurnAroundTime''',
		    ],
		)

		# Printing the dataFrame
		pd.set_option('''display.max_rows''', fcfs.shape[0] + 1)
		print(fcfs)
