import pandas as pd instruction_df = pd.read_csv('02.txt', sep=" ") # instruction_df = pd.read_csv('02.test', sep=" ") instruction_list = instruction_df.to_dict('records') x = 0 y = 0 for one_instruction in instruction_list: if one_instruction['direction'] == 'forward': x += one_instruction['amount'] if one_instruction['direction'] == 'down': y += one_instruction['amount'] if one_instruction['direction'] == 'up': y -= one_instruction['amount'] print(x, y, x*y) x = 0 y = 0 aim = 0 for one_instruction in instruction_list: if one_instruction['direction'] == 'forward': x += one_instruction['amount'] y += one_instruction['amount'] * aim if one_instruction['direction'] == 'down': aim += one_instruction['amount'] if one_instruction['direction'] == 'up': aim -= one_instruction['amount'] print(x, y, x*y)