Spaces:
Running
Running
File size: 1,766 Bytes
a4da721 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
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() |