Skip to content

Instantly share code, notes, and snippets.

@torazuka
Created September 30, 2013 15:04
Show Gist options
  • Save torazuka/6765123 to your computer and use it in GitHub Desktop.
Save torazuka/6765123 to your computer and use it in GitHub Desktop.
第14回オフラインリアルタイムどう書くのJava解答
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
enum Arm {
a, c, e, g, i, k;
}
enum Monster {
B(Arm.a, Arm.c), D(Arm.c, Arm.e), F(Arm.e, Arm.g), H(Arm.g, Arm.i), J(
Arm.i, Arm.k), L(Arm.k, Arm.a);
Arm weakness;
Arm prize;
Monster(Arm w, Arm p) {
weakness = w;
prize = p;
}
}
public class HuntingMonster {
public String solve(String input) {
List<Arm> arms = createArmList(input);
if (arms.size() == 0) {
return String.valueOf(0);
}
List<Monster> monsters = createMonsterList(input);
List<Monster> killed = fight(monsters, arms);
return String.valueOf(killed.size());
}
List<Arm> getPrizes(List<Monster> killed) {
List<Arm> result = new ArrayList<>();
for (Monster m : killed) {
result.add(m.prize);
}
return result;
}
List<Monster> fight(List<Monster> mons, List<Arm> arms) {
List<Monster> killed = new ArrayList<>();
for (Monster m : mons) {
for (Arm a : arms) {
if (m.weakness == a) {
killed.add(m);
break;
}
}
}
if (killed.size() == mons.size() || killed.size() == 0) {
return killed;
}
mons.removeAll(killed);
killed.addAll(fight(mons, getPrizes(killed)));
return killed;
}
List<Arm> createArmList(String input) {
List<Arm> result = new ArrayList<>();
for (char c : input.toCharArray()) {
if (isArm(c)) {
result.add(Arm.valueOf(String.valueOf(c)));
}
}
return result;
}
List<Monster> createMonsterList(String input) {
List<Monster> result = new ArrayList<>();
for (char c : input.toCharArray()) {
if (isMonster(c)) {
result.add(Monster.valueOf(String.valueOf(c)));
}
}
return result;
}
boolean isArm(char c) {
for (Arm a : EnumSet.allOf(Arm.class)) {
if (a.name().equals(String.valueOf(c))) {
return true;
}
}
return false;
}
boolean isMonster(char c) {
for (Monster m : EnumSet.allOf(Monster.class)) {
if (m.name().equals(String.valueOf(c))) {
return true;
}
}
return false;
}
}
import org.junit.Test;
public class HuntingMonsterTest {
HuntingMonster target;
void test(String input, String expected) {
target = new HuntingMonster();
System.out.println(input
+ (target.solve(input).equals(expected) ? ": OK"
: ": !!!!NG!!!! " + target.solve(input)));
// assertEquals(expected, target.solve(input));
}
@Test
public void testSolve() throws Exception {
/* 0 */test("gLDLBgBgHDaD", "6");
/* 1 */test("DBcDLaLgDBH", "6");
/* 2 */test("JJca", "0");
/* 3 */test("FJDLBH", "0");
/* 4 */test("HJBLFDg", "6");
/* 5 */test("HBaDLFJ", "6");
/* 6 */test("DJaHLB", "2");
/* 7 */test("gDLHJF", "3");
/* 8 */test("cJFgLHD", "5");
/* 9 */test("FFBJaJJ", "1");
/* 10 */test("FJeJFBJ", "2");
/* 11 */test("iJFFJJB", "3");
/* 12 */test("JBJiLFJF", "5");
/* 13 */test("JDiFLFBJJ", "8");
/* 14 */test("BDFDFFDFFLLFFJFDBFDFFFFDDFaDBFFB", "28");
/* 15 */test("DDFBFcBDFFFFFFLBFDFFBFLFDFDJDFDF", "24");
/* 16 */test("FDLBFDDBFFFeFFFFFDFBLDDFDDFBFFJF", "16");
/* 17 */test("FDBFFLFDFFDBBDFFBJDLFgDFFFDFFDFF", "0");
/* 18 */test("FDiFLDFFFFBDDJDDBFBFDFFFBFFDFLFF", "31");
/* 19 */test("FDFDJBLBLBFFDDFFFDFFFFFDDFBkFDFF", "30");
/* 20 */test("HBkFFFFHBLH", "3");
/* 21 */test("FBHHFFFHLaB", "2");
/* 22 */test("LFHFBBcHFHF", "0");
/* 23 */test("LFBHFFeFHBH", "7");
/* 24 */test("LgFHHHBFBFF", "3");
/* 25 */test("FFiFHBHLBFH", "0");
/* 26 */test("BFHHFFHBeFLk", "10");
/* 27 */test("FHFaBBHFHLFg", "5");
/* 28 */test("FFgacaFg", "0");
/* 29 */test("JHDaDcBJiiHccBHDBDH", "9");
/* 30 */test("FHJJLckFckFJHDFF", "12");
/* 31 */test("DeDHJHDFHJBLHDLLDHJLBDD", "22");
/* 32 */test("gJLLLJgJgJLJL", "0");
/* 33 */test("DaaaDDD", "0");
/* 34 */test("HFeJFHiBiiBJeJBBFFB", "9");
/* 35 */test("FJFFJDBHBHaLJBHJHDLHkLLLFFFgJgHJLHkJkB", "32");
/* 36 */test("giFLBiBJLLJgHBFJigJJJBLHFLDLL", "23");
/* 37 */test("cgkLJcLJJJJgJc", "2");
/* 38 */test("LDFHJHcFBDBLJBLFLcFJcDFBL", "22");
/* 39 */test("JJHHHkHJkHLJk", "1");
/* 40 */test("kHHBBaBgHagHgaHBBB", "11");
/* 41 */test("HDBFFDHHHDFLDcHHLFDcJD", "20");
/* 42 */test("HFFFHeFFee", "7");
/* 43 */test("gLLDHgDLgFL", "1");
/* 44 */test("JJJBBaBBHBBHaLBHJ", "7");
/* 45 */test("FBFBgJBDBDgF", "0");
/* 46 */test("LLLLakakLakLL", "7");
/* 47 */test("HeJHeJe", "0");
/* 48 */test("LDFLBLLeBLDBBFFBLFBB", "4");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment