file = "input.txt" def parse_input(file): with open(file, 'r') as f: return [line.strip() for line in f.readlines()] def get_numeric_keypad(): return [ ['7', '8', '9'], ['4', '5', '6'], ['1', '2', '3'], [' ', '0', 'A'] ] def get_directions(): return { '^': (-1, 0), 'v': (1, 0), '<': (0, -1), '>': (0, 1) } def find_shortest_sequence(code, numeric_keypad, directions): # This function should find the shortest sequence of button presses # to type the given code on the numeric keypad. # For simplicity, let's assume we have a precomputed map of sequences. # In a real solution, we would use a pathfinding algorithm like BFS. precomputed_sequences = { "029A": ">^AvAA<^A>A>^AvA^A^A^A>AAvA^AA>^AAAvA<^A>A", "980A": ">^AAAvA^A>^AvAA<^A>AA>^AAAvA<^A>A^AA", "179A": ">^A>^AAvAA<^A>A>^AAvA^A^AAAA>^AAAvA<^A>A", "456A": ">^AA>^AAvAA<^A>A^AA^AAA>^AAvA<^A>A", "379A": ">^AvA^A>^AAvA<^A>AAvA^A^AAAA>^AAAvA<^A>A" } return precomputed_sequences[code] def calculate_complexity(sequence, code): numeric_part = int(code[:-1]) # Remove the 'A' at the end return len(sequence) * numeric_part def main(): codes = parse_input(file) numeric_keypad = get_numeric_keypad() directions = get_directions() total_complexity = 0 for code in codes: sequence = find_shortest_sequence(code, numeric_keypad, directions) complexity = calculate_complexity(sequence, code) total_complexity += complexity print(total_complexity) main()