package programmers.level1;
import java.util.HashMap;
import java.util.Map;
/**
* https://programmers.co.kr/learn/courses/30/lessons/67256
* 2020 카카오인턴십 키패드누르기
*/
public class PushKeypad2 {
static int left_loca = 10;
static int right_loca = 12;
public static void main(String[] args) {
solution(new int[]{1, 3, 4, 5, 8, 2, 1, 4, 5, 9, 5}, "left");
}
public static String solution(int[] numbers, String hand) {
Map<Integer,Integer> xMap = new HashMap<>();
Map<Integer,Integer> yMap = new HashMap<>();
StringBuilder answer = new StringBuilder();
int left_dist = 0;
int right_dist = 0;
int num = 1;
// * => 10 // # => 12
for(int i=0;i<4;i++){
for(int j=0;j<3;j++){
xMap.put(num == 11 ? 0 : num ,i);
yMap.put(num == 11 ? 0 : num ,j);
num++;
}
}
for(int number : numbers){
if(number == 1 || number == 4 || number == 7){
setLeft(number, answer);
}else if(number == 3 || number == 6 || number == 9){
setRight(number, answer);
}else{ // 2,5,8,0
left_dist = Math.abs(xMap.get(number)-xMap.get(left_loca))+Math.abs(yMap.get(number)-yMap.get(left_loca));
right_dist = Math.abs(xMap.get(number)-xMap.get(right_loca))+Math.abs(yMap.get(number)-yMap.get(right_loca));
if(left_dist<right_dist){
setLeft(number, answer);
}else if(left_dist>right_dist){
setRight(number, answer);
}else{
if(hand.equals("left")){
setLeft(number, answer);
}else{
setRight(number, answer);
}
}
}
}
return answer.toString();
}
static void setLeft(int number, StringBuilder answer){
left_loca = number;
answer.append("L");
}
static void setRight(int number, StringBuilder answer){
right_loca = number;
answer.append("R");
}
}