Spaces:
Running
Running
| 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": "<vA<AA>>^AvAA<^A>A<v<A>>^AvA^A<vA>^A<v<A>^A>AAvA^A<v<A>A>^AAAvA<^A>A", | |
| "980A": "<v<A>>^AAAvA^A<vA<AA>>^AvAA<^A>A<v<A>A>^AAAvA<^A>A<vA>^A<A>A", | |
| "179A": "<v<A>>^A<vA<A>>^AAvAA<^A>A<v<A>>^AAvA^A<vA>^AA<A>A<v<A>A>^AAAvA<^A>A", | |
| "456A": "<v<A>>^AA<vA<A>>^AAvAA<^A>A<vA>^A<A>A<vA>^A<A>A<v<A>A>^AAvA<^A>A", | |
| "379A": "<v<A>>^AvA^A<vA<AA>>^AAvA<^A>AAvA^A<vA>^AA<A>A<v<A>A>^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() |