advent24-llm / day21 /solution_gemini-1.5-pro.py
jerpint's picture
Add solution files
a4da721
def solve():
keypad = ["789", "456", "123", " 0A"]
keypad_map = {}
for r in range(len(keypad)):
for c in range(len(keypad[r])):
keypad_map[keypad[r][c]] = (r, c)
directional_keypad = [" ^A", "<v>", " ^A", "<v>"]
directional_keypad_map = {}
for r in range(len(directional_keypad)):
for c in range(len(directional_keypad[r])):
directional_keypad_map[directional_keypad[r][c]] = (r, c)
def get_code(instructions, start_pos, keypad_layout, keypad_mapping):
code = ""
curr_pos = start_pos
for instruction in instructions:
next_pos = (curr_pos[0] + (1 if instruction == 'v' else (-1 if instruction == '^' else 0)),
curr_pos[1] + (1 if instruction == '>' else (-1 if instruction == '<' else 0)))
if 0 <= next_pos[0] < len(keypad_layout) and 0 <= next_pos[1] < len(keypad_layout[next_pos[0]]) and keypad_layout[next_pos[0]][next_pos[1]] != ' ':
curr_pos = next_pos
if instruction == 'A':
code += keypad_layout[curr_pos[0]][curr_pos[1]]
return code
def part1(instructions_list):
total_complexity = 0
for instructions in instructions_list:
code = get_code(instructions, keypad_map['A'], keypad, keypad_map)
complexity = len(instructions) * int(code[:-1] or "0")
total_complexity += complexity
return total_complexity
def part2(instructions_list):
total_complexity = 0
for instructions in instructions_list:
intermediate_instructions = instructions
for _ in range(25):
intermediate_instructions = get_code(intermediate_instructions, directional_keypad_map['A'], directional_keypad, directional_keypad_map)
code = get_code(intermediate_instructions, keypad_map['A'], keypad, keypad_map)
complexity = len(instructions) * int(code[:-1] or "0")
total_complexity += complexity
return total_complexity
with open("./input.txt") as f:
instructions_list = [line.strip() for line in f]
print(part1(instructions_list))
print(part2(instructions_list))
solve()