Skip to content

Instantly share code, notes, and snippets.

@nickponline
Created December 21, 2024 22:58
Show Gist options
  • Select an option

  • Save nickponline/67a8452c4d3e6187018b541c2655438d to your computer and use it in GitHub Desktop.

Select an option

Save nickponline/67a8452c4d3e6187018b541c2655438d to your computer and use it in GitHub Desktop.
#https://adventofcode.com/2024/day/21
from functools import cache
import collections
NUM_BOTS = 25
K = collections.defaultdict(list)
K.update({('7', '8'): ['>'], ('7', '4'): ['v'], ('7', '9'): ['>>'], ('7', '5'): ['>v', 'v>'], ('7', '6'): ['>>v', '>v>', 'v>>'], ('7', '1'): ['vv'], ('7', '2'): ['>vv', 'v>v', 'vv>'], ('7', '3'): ['>>vv', '>v>v', 'v>>v', '>vv>', 'v>v>', 'vv>>'], ('7', '0'): ['>vvv', 'v>vv', 'vv>v'], ('7', 'A'): ['>>vvv', '>v>vv', 'v>>vv', '>vv>v', 'v>v>v', 'vv>>v', '>vvv>', 'v>vv>', 'vv>v>'], ('8', '7'): ['<'], ('8', '4'): ['<v', 'v<'], ('8', '9'): ['>'], ('8', '5'): ['v'], ('8', '6'): ['>v', 'v>'], ('8', '1'): ['<vv', 'v<v', 'vv<'], ('8', '2'): ['vv'], ('8', '3'): ['>vv', 'v>v', 'vv>'], ('8', '0'): ['vvv'], ('8', 'A'): ['>vvv', 'v>vv', 'vv>v', 'vvv>'], ('4', '7'): ['^'], ('4', '8'): ['>^', '^>'], ('4', '9'): ['>>^', '>^>', '^>>'], ('4', '5'): ['>'], ('4', '6'): ['>>'], ('4', '1'): ['v'], ('4', '2'): ['>v', 'v>'], ('4', '3'): ['>>v', '>v>', 'v>>'], ('4', '0'): ['>vv', 'v>v'], ('4', 'A'): ['>>vv', '>v>v', 'v>>v', '>vv>', 'v>v>'], ('9', '7'): ['<<'], ('9', '8'): ['<'], ('9', '4'): ['<<v', '<v<', 'v<<'], ('9', '5'): ['<v', 'v<'], ('9', '6'): ['v'], ('9', '1'): ['<<vv', '<v<v', 'v<<v', '<vv<', 'v<v<', 'vv<<'], ('9', '2'): ['<vv', 'v<v', 'vv<'], ('9', '3'): ['vv'], ('9', '0'): ['<vvv', 'v<vv', 'vv<v', 'vvv<'], ('9', 'A'): ['vvv'], ('5', '7'): ['<^', '^<'], ('5', '8'): ['^'], ('5', '4'): ['<'], ('5', '9'): ['>^', '^>'], ('5', '6'): ['>'], ('5', '1'): ['<v', 'v<'], ('5', '2'): ['v'], ('5', '3'): ['>v', 'v>'], ('5', '0'): ['vv'], ('5', 'A'): ['>vv', 'v>v', 'vv>'], ('6', '7'): ['<<^', '<^<', '^<<'], ('6', '8'): ['<^', '^<'], ('6', '4'): ['<<'], ('6', '9'): ['^'], ('6', '5'): ['<'], ('6', '1'): ['<<v', '<v<', 'v<<'], ('6', '2'): ['<v', 'v<'], ('6', '3'): ['v'], ('6', '0'): ['<vv', 'v<v', 'vv<'], ('6', 'A'): ['vv'], ('1', '7'): ['^^'], ('1', '8'): ['>^^', '^>^', '^^>'], ('1', '4'): ['^'], ('1', '9'): ['>>^^', '>^>^', '^>>^', '>^^>', '^>^>', '^^>>'], ('1', '5'): ['>^', '^>'], ('1', '6'): ['>>^', '>^>', '^>>'], ('1', '2'): ['>'], ('1', '3'): ['>>'], ('1', '0'): ['>v'], ('1', 'A'): ['>>v', '>v>'], ('2', '7'): ['<^^', '^<^', '^^<'], ('2', '8'): ['^^'], ('2', '4'): ['<^', '^<'], ('2', '9'): ['>^^', '^>^', '^^>'], ('2', '5'): ['^'], ('2', '6'): ['>^', '^>'], ('2', '1'): ['<'], ('2', '3'): ['>'], ('2', '0'): ['v'], ('2', 'A'): ['>v', 'v>'], ('3', '7'): ['<<^^', '<^<^', '^<<^', '<^^<', '^<^<', '^^<<'], ('3', '8'): ['<^^', '^<^', '^^<'], ('3', '4'): ['<<^', '<^<', '^<<'], ('3', '9'): ['^^'], ('3', '5'): ['<^', '^<'], ('3', '6'): ['^'], ('3', '1'): ['<<'], ('3', '2'): ['<'], ('3', '0'): ['<v', 'v<'], ('3', 'A'): ['v'], ('0', '7'): ['^<^^', '^^<^', '^^^<'], ('0', '8'): ['^^^'], ('0', '4'): ['^<^', '^^<'], ('0', '9'): ['>^^^', '^>^^', '^^>^', '^^^>'], ('0', '5'): ['^^'], ('0', '6'): ['>^^', '^>^', '^^>'], ('0', '1'): ['^<'], ('0', '2'): ['^'], ('0', '3'): ['>^', '^>'], ('0', 'A'): ['>'], ('A', '7'): ['<^<^^', '^<<^^', '<^^<^', '^<^<^', '^^<<^', '<^^^<', '^<^^<', '^^<^<', '^^^<<'], ('A', '8'): ['<^^^', '^<^^', '^^<^', '^^^<'], ('A', '4'): ['<^<^', '^<<^', '<^^<', '^<^<', '^^<<'], ('A', '9'): ['^^^'], ('A', '5'): ['<^^', '^<^', '^^<'], ('A', '6'): ['^^'], ('A', '1'): ['<^<', '^<<'], ('A', '2'): ['<^', '^<'], ('A', '3'): ['^'], ('A', '0'): ['<']})
A = collections.defaultdict(list)
A.update({('^', 'A'): ['>'], ('^', 'v'): ['v'], ('^', '>'): ['>v', 'v>'], ('^', '<'): ['v<'], ('A', '^'): ['<'], ('A', 'v'): ['<v', 'v<'], ('A', '>'): ['v'], ('A', '<'): ['<v<', 'v<<'], ('v', '^'): ['^'], ('v', 'A'): ['>^', '^>'], ('v', '>'): ['>'], ('v', '<'): ['<'], ('>', '^'): ['<^', '^<'], ('>', 'A'): ['^'], ('>', 'v'): ['<'], ('>', '<'): ['<<'], ('<', '^'): ['>^'], ('<', 'A'): ['>>^', '>^>'], ('<', 'v'): ['>'], ('<', '>'): ['>>']})
@cache
def minimum_rewrite(level, text):
if level == NUM_BOTS + 1:
return len(text)
PM = K if level == 0 else A
k_total = 0
for start, end in zip('A'+text, text):
minks = [ minimum_rewrite(level+1, path + 'A') for path in PM[(start, end)]]
minks = [ k for k in minks if k is not None]
k_total += min(minks) if minks else 1
return k_total
with open('2024-21.in') as f:
codes = [line.strip() for line in f.readlines() if line.strip()]
total = 0
for code in codes:
ordinal = int("".join([c for c in code if c.isdigit()]))
min_len = minimum_rewrite(0, code)
total += ordinal * min_len
print(total)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment