Skip to content

Instantly share code, notes, and snippets.

@torazuka
Created September 7, 2012 10:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save torazuka/3665096 to your computer and use it in GitHub Desktop.
Save torazuka/3665096 to your computer and use it in GitHub Desktop.
第3回 オフラインリアルタイムどう書く: Java解答
package yhpg3;
import static org.junit.Assert.assertEquals;
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
public class TripY {
Map<String, String> dic;
public TripY() {
dic = new HashMap<>();
dic.put("BAr", "C");
dic.put("BAl", "D");
dic.put("ABr", "E");
dic.put("ABl", "C");
dic.put("ACr", "B");
dic.put("ACl", "F");
dic.put("CAr", "D");
dic.put("CAl", "B");
dic.put("ADr", "F");
dic.put("ADl", "E");
dic.put("DAr", "B");
dic.put("DAl", "C");
dic.put("BCr", "F");
dic.put("BCl", "A");
dic.put("CBr", "A");
dic.put("CBl", "E");
dic.put("BEr", "D");
dic.put("BEl", "F");
dic.put("EBr", "C");
dic.put("EBl", "A");
dic.put("DEr", "F");
dic.put("DEl", "B");
dic.put("EDr", "A");
dic.put("EDl", "F");
dic.put("CFr", "E");
dic.put("CFl", "D");
dic.put("FCr", "A");
dic.put("FCl", "B");
dic.put("DFr", "C");
dic.put("DFl", "E");
dic.put("FDr", "E");
dic.put("FDl", "A");
dic.put("EFr", "D");
dic.put("EFl", "C");
dic.put("FEr", "B");
dic.put("FEl", "D");
}
protected String execute(String input) {
String result = "A";
String current = "BA";
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c == 'b') {
result += current.charAt(0);
current = String.valueOf(current.charAt(1))
+ String.valueOf(current.charAt(0));
} else if (c == 'r' || c == 'l') {
String tmp = current + String.valueOf(c);
String to = dic.get(tmp);
result += to;
current = String.valueOf(current.charAt(1)) + to;
}
}
return result;
}
public static void main(String[] args) {
TripY trip = new TripY();
System.out.println(trip.execute("llllllrllrlbrrr")); // ADEBADEFCBADABED
}
@Test
public void testExecute() throws Exception {
TripY trip = new TripY();
String[][] answers = {
/* 0 */{ "b", "AB" },
/* 1 */{ "l", "AD" },
/* 2 */{ "r", "AC" },
/* 3 */{ "bbb", "ABAB" },
/* 4 */{ "rrr", "ACBA" },
/* 5 */{ "blll", "ABCAB" },
/* 6 */{ "llll", "ADEBA" },
/* 7 */{ "rbrl", "ACADE" },
/* 8 */{ "brrrr", "ABEDAB" },
/* 9 */{ "llrrr", "ADEFDE" },
/* 10 */{ "lrlll", "ADFEDF" },
/* 11 */{ "lrrrr", "ADFCAD" },
/* 12 */{ "rllll", "ACFDAC" },
/* 13 */{ "blrrrr", "ABCFEBC" },
/* 14 */{ "brllll", "ABEFCBE" },
/* 15 */{ "bbrllrrr", "ABACFDEFD" },
/* 16 */{ "rrrrblll", "ACBACABCA" },
/* 17 */{ "llrlrrbrb", "ADEFCADABA" },
/* 18 */{ "rrrbrllrr", "ACBABEFCAD" },
/* 19 */{ "llrllblrll", "ADEFCBCADEB" },
/* 20 */{ "lrrlllrbrl", "ADFCBEFDFCB" },
/* 21 */{ "lllrbrrlbrl", "ADEBCBACFCAB" },
/* 22 */{ "rrrrrrlrbrl", "ACBACBADFDEB" },
/* 23 */{ "lbrbbrbrbbrr", "ADABABEBCBCFE" },
/* 24 */{ "rrrrlbrblllr", "ACBACFCACFDAB" },
/* 25 */{ "lbbrblrlrlbll", "ADADFDABCFDFED" },
/* 26 */{ "rrbbrlrlrblrl", "ACBCBADFEBEFDA" },
/* 27 */{ "blrllblbrrrrll", "ABCFDADEDABEDFE" },
/* 28 */{ "blrllrlbllrrbr", "ABCFDABCBEFDEDA" },
/* 29 */{ "lbrbbrllllrblrr", "ADABABEFCBEDEBCF" },
/* 30 */{ "rrrrbllrlrbrbrr", "ACBACABCFDEDADFC" }, };
for (int i = 0; i < 30; i++) {
assertEquals(answers[i][1], trip.execute(answers[i][0]));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment