/Challenge213.java Secret
Last active
August 29, 2015 14:20
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.HashMap; | |
import java.util.Scanner; | |
/** | |
* Created by dustin on 5/9/15. | |
*/ | |
public class Main { | |
public static void main(String[] args){ | |
String keyboard = | |
"qwertyuiop\nasdfghjkl\n^zxcvbnm ^\n #####"; | |
int row = 0; | |
int col = 0; | |
HashMap<Character,ArrayList<Point>> keyboardMap = new HashMap<Character, ArrayList<Point>>(); | |
for (char e: keyboard.toCharArray()){ | |
if (e == '\n'){ | |
row++; | |
col = -1; | |
}else { | |
if (!keyboardMap.containsKey(e)){ | |
keyboardMap.put(e,new ArrayList<Point>()); | |
keyboardMap.get(e).add(new Point(row,col)); | |
}else{ | |
keyboardMap.get(e).add(new Point(row,col)); | |
} | |
} | |
col++; | |
} | |
/** | |
for (char e: keyboard.toCharArray()){ | |
if (e != '\n'){ | |
System.out.println(e +": " + keyboardMap.get(e)); | |
} | |
} | |
*/ | |
Scanner in = new Scanner(System.in); | |
char[] input = in.nextLine().toCharArray(); | |
StringBuilder treatedInput = new StringBuilder(); | |
for (char e : input){ | |
if (Character.isUpperCase(e)){ | |
treatedInput.append('^'); | |
treatedInput.append(Character.toLowerCase(e)); | |
}else if (e == ' '){ | |
treatedInput.append('#'); | |
}else{ | |
treatedInput.append(e); | |
} | |
} | |
// System.out.println(treatedInput); | |
Point leftHand = null; | |
Point rightHand = null; | |
char lefthandChar = '!'; | |
char rightHandchar = '!'; | |
int totalEffort = 0; | |
boolean cantUseLeft = false; | |
boolean cantUseRight = false; | |
int leftCount = 0; | |
int rightCount = 0; | |
for (char e: treatedInput.toString().toCharArray()){ | |
if (leftHand == null && !cantUseLeft){ | |
System.out.println(String.format( | |
"%c: Use left hand",e | |
)); | |
leftHand = keyboardMap.get(e).get(0); | |
lefthandChar = e; | |
if (e == '^'){ | |
cantUseLeft = true; | |
} | |
}else if(rightHand == null && !cantUseRight){ | |
System.out.println(String.format( | |
"%c: Use right hand",e | |
)); | |
rightHand = keyboardMap.get(e).get(0); | |
rightHandchar = e; | |
if (e == '^'){ | |
cantUseRight = true; | |
} | |
}else{ | |
Point leftMove = getShortestPoint(leftHand,keyboardMap.get(e)); | |
Point rightMove = getShortestPoint(rightHand,keyboardMap.get(e)); | |
int leftDistance = leftHand.getManhattanDistance(leftMove); | |
int rightDistance = rightHand.getManhattanDistance(rightMove); | |
if (cantUseRight ||(leftDistance < rightDistance && !cantUseLeft)){ | |
System.out.println(String.format( | |
"%c: Move left hand from %c (effort: %d)",e,lefthandChar,leftDistance | |
)); | |
leftHand = leftMove; | |
lefthandChar = e; | |
totalEffort += leftDistance; | |
if (e == '^'){ | |
cantUseLeft = true; | |
} | |
}else{ | |
System.out.println(String.format( | |
"%c: Move right hand from %c (effort: %d)",e,rightHandchar,rightDistance | |
)); | |
rightHand = rightMove; | |
rightHandchar = e; | |
totalEffort += rightDistance; | |
if (e == '^'){ | |
cantUseRight = true; | |
} | |
} | |
} | |
if (cantUseLeft){ | |
if (leftCount == 0){ | |
leftCount++; | |
}else{ | |
leftCount = 0; | |
cantUseLeft = false; | |
} | |
} | |
if (cantUseRight){ | |
if (rightCount == 0){ | |
rightCount++; | |
}else{ | |
rightCount = 0; | |
cantUseRight = false; | |
} | |
} | |
} | |
System.out.println(String.format( | |
"Total effort: %d", totalEffort | |
)); | |
} | |
private static Point getShortestPoint(Point leftHand, ArrayList<Point> points) { | |
Point min = points.get(0); | |
for (Point e: points){ | |
if (leftHand.getManhattanDistance(e) < leftHand.getManhattanDistance(min)){ | |
min = e; | |
} | |
} | |
return min; | |
} | |
} | |
class Point { | |
private int x; | |
private int y; | |
public Point(int x, int y){ | |
this.x = x; | |
this.y = y; | |
} | |
public int getX() { | |
return x; | |
} | |
public int getY() { | |
return y; | |
} | |
public int getManhattanDistance(Point other){ | |
return Math.abs(other.x-this.x) + Math.abs(other.y-this.y); | |
} | |
@Override | |
public String toString(){ | |
return String.format("(%d,%d)",x,y); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment