#!/usr/bin/env python3 # -*- coding: utf-8 -*- file = "./input.txt" #file = "./ex.txt" from time import time start_time = time() keypad=[ ["-","-","1","-","-"], ["-","2","3","4","-"], ["5","6","7","8","9"], ["-","A","B","C","-"], ["-","-","D","-","-"]] def read_file(f:str) -> list: out = [] input_file = open(f, "r") for line in input_file: line = line.strip() if line == "": continue else: line = line.strip() out.append(line) input_file.close() return out def index_exists(array, i, j): try: if i>=0 and j>=0 and array[i][j] != "-": return True else: return False except IndexError: return False if __name__ == "__main__": x, y = 2, 0 part2 = True input_list = read_file(file) print("Solution: ",end="") for elem in input_list: for b in elem: if b == "R" and index_exists(keypad, x, y+1): y += 1 elif b == "L" and index_exists(keypad, x, y-1): y -= 1 elif b == "U" and index_exists(keypad, x-1, y): x -= 1 elif b == "D" and index_exists(keypad, x+1, y): x += 1 print(keypad[x][y],end="") print() # print(input_list) print(f'Runtime: {time()-start_time:.2f} s')