/* All static "map" arrays follow the same rules allowing for optimal solution. List of rules from the most to the least important: 1. Avoid changing direction. Because on next iteration robot will have to go back and forward between directional key and A key each time the direction key change. 2. Move to the left first when possible. Left directional key is farthest from A key making it the most expensive. Moving to the left first makes other followed moves smaller. It's not possible to start moving to the left in all combinations because of the empty key. In that situation just stick to the first rule. 3. Use ^>A instead of >^A when possible. First combination produce shorter sequence after 2 more iterations. ^>A (3)-> A^A (7)-> v<>^AvA^AA (19)-> ... >^A (3)-> vA<^A>A (7)-> v^Av<^A>Av^A (21)-> ... */ #include #include #include static const int dir_indexof[] = { // Directional keypad key indexes /* -- */ ['^']=0, [ 0 ]=4, ['<']=3, ['v']=2, ['>']=1, }; static char *dir_map[16][16] = { // [from][to] key combinations /*^*/ {"","v>","v","v<",">"}, /*>*/ {"<^","","<","<<","^"}, /*v*/ {"^",">","","<","^>"}, /*<*/ {">^",">>",">","",">>^"}, /*A*/ {"<","v","","^^<","^^","^^>","^^^<","^^^","^^^>",">"}, /*1*/ {"v>","",">",">>","^","^>","^>>","^^","^^>","^^>>",">>v"}, /*2*/ {"v","<","",">","<^","^","^>","<^^","^^","^^>",">v"}, /*3*/ {"vv","v","v>","v>>","",">",">>","^","^>","^>>",">>vv"}, /*5*/ {"vv","","<","",">","<^","^","^>",">vv"}, /*6*/ {"vvv","vv","vv>","vv>>","v","v>","v>>","",">",">>",">>vvv"}, /*8*/ {"vvv","","","<","",">","vvv>"}, /*9*/ {" 0) { result += atoi(buf) * click(num_indexof, num_map, buf, 3); } printf("%lu\n", result); return !(result == 126384 || result == 205160); }