#advent of code 2024
#day 21
#finally fucking worked
#I had a good idea for part1 to not generate all steps but only count
#the number of steps since the strings for part 2 would eat up all the RAM
#however I got hit by another problem which I didn't forsee,
#that is permutations. Basically, for part 2, you need to take into account
#that maybe going in different order is going to yield better results
#I hardcoded the possible steps instead of DFSing them like a normal
#human being and doubled down on it later. When I'll have more time and will to
#improve the code, I'll include the part where I generate the movements
#solution for part 1 was developed on my own, but for part 2 I needed
#external help (at least I understand what's going on in here so it's not
#that I copy-pasted someone else's code).
from functools import cache
import itertools
#use this for later
'''
keyboard_num = {};
keyboard_num["7"] = (0,0);
keyboard_num["8"] = (1,0);
keyboard_num["9"] = (2,0);
keyboard_num["4"] = (1,0);
keyboard_num["5"] = (1,1);
keyboard_num["6"] = (1,2);
keyboard_num["3"] = (2,0);
keyboard_num["2"] = (2,1);
keyboard_num["1"] = (2,2);
keyboard_num["0"] = (3,1);
keyboard_num["A"] = (3,2);
keyboard_dir = {};
keyboard_dir"^"] = (1,0);
keyboard_dir"A"] = (2,0);
keyboard_dir"<"] = (0,1);
keyboard_dir"v"] = (1,1);
keyboard_dir">"] = (2,1); #'''
dn = {};
dn["^"] = {};
dn["A"] = {};
dn["<"] = {};
dn["v"] = {};
dn[">"] = {};
dn["^"]["^"] =["A"];
dn["^"]["A"] =[">A"];
dn["^"]["<"] =["v"] =["v>A",">vA"];
dn["A"]["^"] =[""] =["vA"];
dn["<"]["^"] =[">^A"];
dn["<"]["A"] =[">^>A",">>^A"];
dn["<"]["<"] =["A"];
dn["<"]["v"] =[">A"];
dn["<"][">"] =[">>A"];
dn["v"]["^"] =["^A"];
dn["v"]["A"] =["^>A",">^A"];
dn["v"]["<"] =[""] =[">A"];
dn[">"]["^"] =["^"]["A"] =["^A"];
dn[">"]["<"] =["<"]["v"] =[""][">"] =["A"];
kn={};
kn["7"] = {};
kn["8"] = {};
kn["9"] = {};
kn["4"] = {};
kn["5"] = {};
kn["6"] = {};
kn["1"] = {};
kn["2"] = {};
kn["3"] = {};
kn["0"] = {};
kn["A"] = {};
kn["7"]["7"] =["A"];
kn["7"]["8"] =[">A"];
kn["7"]["9"] =[">>A"];
kn["7"]["4"] =["vA"];
kn["7"]["5"] =["v>A",">vA"];
kn["7"]["6"] =["v>>A",">v>A",">>vA"];
kn["7"]["1"] =["vvA"];
kn["7"]["2"] =["vv>A","v>vA",">vvA"];
kn["7"]["3"] =["vv>>A","v>v>A","v>>vA",">vv>A",">v>vA",">>vvA"];
kn["7"]["0"] =["vv>vA","v>vvA",">vvvA"];
kn["7"]["A"] =["vv>v>A","vv>>vA","v>vv>A","v>v>vA","v>>vvA",">vvv>A",">vv>vA",">v>vvA",">>vvvA"];
kn["8"]["7"] =["A"];
kn["8"]["4"] =["vA",">vA"];
kn["8"]["1"] =["vvA","v>vA",">vvA"];
kn["8"]["0"] =["vvvA"];
kn["8"]["A"] =["vvv>A","vv>vA","v>vvA",">vvvA"];
kn["9"]["7"] =["<A",">^A"];
kn["4"]["9"] =["^>>A",">^>A",">>^A"];
kn["4"]["4"] =["A"];
kn["4"]["5"] =[">A"];
kn["4"]["6"] =[">>A"];
kn["4"]["1"] =["vA"];
kn["4"]["2"] =["v>A",">vA"];
kn["4"]["3"] =["v>>A",">v>A",">>vA"];
kn["4"]["0"] =["v>vA",">vvA"];
kn["4"]["A"] =["v>v>A","v>>vA",">vv>A",">v>vA",">>vvA"];
kn["5"]["7"] =["^A",">^A"];
kn["5"]["4"] =["A"];
kn["5"]["1"] =["vA",">vA"];
kn["5"]["0"] =["vvA"];
kn["5"]["A"] =["vv>A","v>vA",">vvA"];
kn["6"]["7"] =["^<A","^>^A",">^^A"];
kn["1"]["9"] =["^^>>A","^>^>A","^>>^A",">^^>A",">^>^A",">>^^A"];
kn["1"]["4"] =["^A"];
kn["1"]["5"] =["^>A",">^A"];
kn["1"]["6"] =["^>>A",">^>A",">>^A"];
kn["1"]["1"] =["A"];
kn["1"]["2"] =[">A"];
kn["1"]["3"] =[">>A"];
kn["1"]["0"] =[">vA"];
kn["1"]["A"] =[">v>A",">>vA"];
kn["2"]["7"] =["^^A","^>^A",">^^A"];
kn["2"]["4"] =["^A",">^A"];
kn["2"]["1"] =["A"];
kn["2"]["0"] =["vA"];
kn["2"]["A"] =["v>A",">vA"];
kn["3"]["7"] =["^^<A","^^>^A","^>^^A",">^^^A"];
kn["0"]["4"] =["^^A","^>^A",">^^A"];
kn["0"]["1"] =["^A",">^A"];
kn["0"]["0"] =["A"];
kn["0"]["A"] =[">A"];
kn["A"]["7"] =["^^^<