Last active
December 27, 2015 05:38
-
-
Save torazuka/7275330 to your computer and use it in GitHub Desktop.
第15回オフラインリアルタイムどう書くのJava解答
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 static org.junit.Assert.assertEquals; | |
import static org.junit.Assert.assertTrue; | |
import java.util.Arrays; | |
import org.junit.Test; | |
enum Rune { | |
U(3, new char[] { '1', '0', '1' }, new char[] { '1', '1', '1' }), N(3, | |
new char[] { '1', '1', '1' }, new char[] { '1', '0', '1' }), S(3, | |
new char[] { '0', '1', '1' }, new char[] { '1', '1', '0' }), T(3, | |
new char[] { '1', '1', '1' }, new char[] { '0', '1', '0' }), Z(3, | |
new char[] { '1', '1', '0' }, new char[] { '0', '1', '1' }), L(2, | |
new char[] { '1', '0' }, new char[] { '1', '1' }), R(2, new char[] { | |
'1', '1' }, new char[] { '1', '0' }), J(2, new char[] { '0', '1' }, | |
new char[] { '1', '1' }); | |
int size; | |
char[] top; | |
char[] bottom; | |
Rune(int s, char[] t, char[] b) { | |
size = s; | |
top = t; | |
bottom = b; | |
} | |
} | |
public class Elebubo { | |
boolean runeEquals(char[][] target, Rune rune) { | |
return Arrays.equals(target[0], rune.top) | |
&& Arrays.equals(target[1], rune.bottom); | |
} | |
public String solve(String input) { | |
String[] split = input.split("/"); | |
char[] top = parse(split[0]); | |
char[] bottom = parse(split[1]); | |
String result = ""; | |
for (int cursor = 0; cursor < top.length; cursor++) { | |
if (top[cursor] == '0' && bottom[cursor] == '0') { | |
continue; | |
} | |
Rune rune = check(top, bottom, cursor); | |
if (rune != null) { | |
cursor = cursor + rune.size - 1; | |
result += rune.name(); | |
} | |
} | |
return result; | |
} | |
Rune check(char[] top, char[] bottom, int cursor) { | |
Rune[] allOf = Rune.values(); | |
Rune result = null; | |
for (Rune rune : allOf) { | |
int size = rune.size; | |
char[][] tmp = substring(top, bottom, cursor, size); | |
if (runeEquals(tmp, rune)) { | |
result = rune; | |
break; | |
} | |
} | |
return result; | |
} | |
char[][] substring(char[] top, char[] bottom, int cursor, int size) { | |
char[][] result = new char[2][size]; | |
for (int i = 0; i < size && cursor + i < top.length; i++) { | |
result[0][i] = top[cursor + i]; | |
result[1][i] = bottom[cursor + i]; | |
} | |
return result; | |
} | |
char[] parse(String input) { | |
char[] charArray = input.toCharArray(); | |
StringBuilder result = new StringBuilder(); | |
for (char c : charArray) { | |
result.append(toBinary(c)); | |
} | |
String str = new String(result); | |
return str.toCharArray(); | |
} | |
String toBinary(char c) { | |
int hex = Integer.parseInt(String.valueOf(c), 16); | |
String binary = Integer.toBinaryString(hex); | |
if (binary.length() < 4) { | |
for (; binary.length() < 4;) { | |
binary = "0" + binary; | |
} | |
} | |
return binary; | |
} | |
@Test | |
public void testSubstring() throws Exception { | |
Elebubo target = new Elebubo(); | |
char[] top0 = new char[] { '0', '1', '0' }; | |
char[] bottom0 = new char[] { '0', '1', '1' }; | |
assertTrue(Arrays.equals(new char[] { '1', '0' }, | |
target.substring(top0, bottom0, 1, 2)[0])); | |
assertTrue(Arrays.equals(new char[] { '1', '1' }, | |
target.substring(top0, bottom0, 1, 2)[1])); | |
char[] top1 = new char[] { '1', '1', '0', '1' }; | |
char[] bottom1 = new char[] { '1', '0', '1', '1' }; | |
assertTrue(Arrays.equals(new char[] { '1', '0', '1' }, | |
target.substring(top1, bottom1, 1, 3)[0])); | |
assertTrue(Arrays.equals(new char[] { '0', '1', '1' }, | |
target.substring(top1, bottom1, 1, 3)[1])); | |
char[] top2 = new char[] { '1', '1', '0', '1' }; | |
char[] bottom2 = new char[] { '1', '0', '1', '1' }; | |
assertTrue(Arrays.equals(new char[] { '1', '1', '0' }, | |
target.substring(top2, bottom2, 0, 3)[0])); | |
assertTrue(Arrays.equals(new char[] { '1', '0', '1' }, | |
target.substring(top2, bottom2, 0, 3)[1])); | |
char[] top3 = new char[] { '1', '1', '0', '0', '1' }; | |
char[] bottom3 = new char[] { '1', '0', '0', '1', '1' }; | |
assertTrue(Arrays.equals(new char[] { '0', '1' }, | |
target.substring(top3, bottom3, 3, 2)[0])); | |
assertTrue(Arrays.equals(new char[] { '1', '1' }, | |
target.substring(top3, bottom3, 3, 2)[1])); | |
} | |
@Test | |
public void testRuneCheck() throws Exception { | |
Elebubo target = new Elebubo(); | |
assertEquals(true, target.runeEquals(new char[][] { { '0', '1' }, | |
{ '1', '1' } }, Rune.J)); | |
assertEquals(true, target.runeEquals(new char[][] { { '1', '0' }, | |
{ '1', '1' } }, Rune.L)); | |
assertEquals(false, target.runeEquals(new char[][] { { '1', '1' }, | |
{ '0', '0' } }, Rune.J)); | |
assertEquals(false, target.runeEquals(new char[][] { { '1', '1' }, | |
{ '1', '0' } }, Rune.N)); | |
assertEquals( | |
true, | |
target.runeEquals(new char[][] { { '1', '1', '1' }, | |
{ '1', '0', '1' } }, Rune.N)); | |
assertEquals( | |
false, | |
target.runeEquals(new char[][] { { '1', '1', '1' }, | |
{ '1', '0', '1' } }, Rune.S)); | |
} | |
@Test | |
public void testToBinary() throws Exception { | |
Elebubo target = new Elebubo(); | |
assertEquals("1111", target.toBinary('f')); | |
assertEquals("0001", target.toBinary('1')); | |
} | |
void test(String input, String expected) { | |
Elebubo target = new Elebubo(); | |
assertEquals(expected, target.solve(input)); | |
} | |
@Test | |
public void testSolve() throws Exception { | |
/* 0 */test("2ed8aeed/34b0ea5b", "LTRSUNTSJ"); | |
/* 1 */test("00000200/00000300", "L"); | |
/* 2 */test("00018000/00010000", "R"); | |
/* 3 */test("00002000/00006000", "J"); | |
/* 4 */test("00000700/00000200", "T"); | |
/* 5 */test("01400000/01c00000", "U"); | |
/* 6 */test("00003800/00002800", "N"); | |
/* 7 */test("000c0000/00180000", "S"); | |
/* 8 */test("00003000/00001800", "Z"); | |
/* 9 */test("132eae6c/1a64eac6", "LRJTUNSZ"); | |
/* 10 */test("637572d0/36572698", "ZSNUTJRL"); | |
/* 11 */test("baddb607/d66b6c05", "LTJZTSSSN"); | |
/* 12 */test("db74cd75/6dac6b57", "ZZZTJZRJNU"); | |
/* 13 */test("3606c2e8/1b0d8358", "ZZSSLTJ"); | |
/* 14 */test("ad98c306/e6cc6183", "UZZZZZZ"); | |
/* 15 */test("4a4aaee3/db6eeaa6", "JJLLUUNNS"); | |
/* 16 */test("ecd9bbb6/598cd124", "TSSZZTTRR"); | |
/* 17 */test("e0000002/40000003", "TL"); | |
/* 18 */test("a0000007/e0000005", "UN"); | |
/* 19 */test("c0000003/80000006", "RS"); | |
/* 20 */test("40000006/c0000003", "JZ"); | |
/* 21 */test("01da94db/00b3b6b2", "TSUJLRSR"); | |
/* 22 */test("76eeaaea/24aaeeae", "TRNNUUNU"); | |
/* 23 */test("1dacaeee/1566e444", "NRJZUTTT"); | |
/* 24 */test("26c9ac60/6c6d66c0", "JSZLRJZS"); | |
/* 25 */test("6c977620/36da5360", "ZZLLTNZJ"); | |
/* 26 */test("069aeae6/0db34eac", "SJSLTUNS"); | |
/* 27 */test("06d53724/049da56c", "RRULRNJJ"); | |
/* 28 */test("069b58b0/04d66da0", "RLRSLZJR"); | |
/* 29 */test("1b6eced4/11b46a9c", "RZZTZNRU"); | |
/* 30 */test("522e8b80/db6ad900", "JLLJNLJT"); | |
/* 31 */test("6546cdd0/376c6898", "ZULSZRTL"); | |
/* 32 */test("4e6d5b70/6ad9d620", "LNSSURST"); | |
/* 33 */test("37367772/65635256", "SNSZNTNJ"); | |
/* 34 */test("25535d58/377669cc", "LUUSLTUZ"); | |
/* 35 */test("0ae6a55d/0eacedcb", "UNSUJUTJ"); | |
/* 36 */test("76762edc/23536a88", "TZNZJNRT"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment