Skip to content

Instantly share code, notes, and snippets.

@torazuka
Created April 7, 2013 05:59
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/5329238 to your computer and use it in GitHub Desktop.
Save torazuka/5329238 to your computer and use it in GitHub Desktop.
第3回 オフラインリアルタイムどう書くの参考問題(Enum編)
import static org.junit.Assert.assertEquals;
import org.junit.Test;
enum Rule {
s {
@Override
int[] play(int[] count) {
count[1] += 1;
if (count[1] == 3) {
count[0] += 1;
count[1] = 0;
}
return count;
}
},
b {
@Override
int[] play(int[] count) {
if ((count[2] += 1) == 4) {
count[1] = 0;
count[2] = 0;
}
return count;
}
},
f {
@Override
int[] play(int[] count) {
if (count[1] < 2) {
count[1] += 1;
}
return count;
}
},
h {
@Override
int[] play(int[] count) {
count[1] = 0;
count[2] = 0;
return count;
}
},
p {
@Override
int[] play(int[] count) {
count[0] += 1;
count[1] = 0;
count[2] = 0;
return count;
}
};
abstract int[] play(int[] count);
}
/**
* 問題: http://qiita.com/items/ebd8a56b41711ba459f9
*/
public class BallCountEx {
public String solve(String input) {
int[] count = new int[3]; // out, strike, ball
char[] charArray = input.toCharArray();
String result = "";
for (char each : charArray) {
Rule rule = Rule.valueOf(String.valueOf(each));
count = rule.play(count);
count = outCheck(count);
for (int i : count) {
result += i;
}
result += ",";
}
return result.substring(0, result.length() - 1);
}
private int[] outCheck(int[] count) {
if (count[0] == 3) {
count[0] = 0;
count[1] = 0;
count[2] = 0;
}
return count;
}
@Test
public void testName() throws Exception {
BallCountEx bc = new BallCountEx();
assertEquals("010", bc.solve("s"));
assertEquals("010,020,100", bc.solve("sss"));
assertEquals("001,002,003,000", bc.solve("bbbb"));
assertEquals("010,020,021,022,023,000", bc.solve("ssbbbb"));
assertEquals("000,010,011,000,010,000,001,000", bc.solve("hsbhfhbh"));
assertEquals("100,110,111,200,210,000,001,100", bc.solve("psbpfpbp"));
assertEquals("100,200,000", bc.solve("ppp"));
assertEquals("010,020,020,020,100", bc.solve("ffffs"));
assertEquals("010,020,100,200,210,220,220,000", bc.solve("ssspfffs"));
assertEquals("001,002,003,013,023,000,100,200,000",
bc.solve("bbbsfbppp"));
assertEquals(
"010,020,100,101,102,103,100,110,111,100,110,111,200,000,100",
bc.solve("sssbbbbsbhsbppp"));
assertEquals("010,020,020,020,100,110,120,200,210,000",
bc.solve("ssffpffssp"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment