Skip to content

Instantly share code, notes, and snippets.

@torazuka
Created June 9, 2013 13:18
Show Gist options
  • Save torazuka/5743494 to your computer and use it in GitHub Desktop.
Save torazuka/5743494 to your computer and use it in GitHub Desktop.
第11回オフラインリアルタイムどう書く(Java解答)
package yhpg11;
public class BitamidaEx {
enum Direction {
right {
@Override
int move(int row, int x) {
for (int i = x; i < 9; i++) {
if (BitamidaEx.is(row, i) == false) {
return i;
}
}
return 8;
}
},
left {
@Override
int move(int row, int x) {
for (int i = x; -1 < i; i--) {
if (BitamidaEx.is(row, i - 1) == false) {
return i;
}
}
return 0;
}
},
down {
@Override
int move(int row, int x) {
return x; // do nothing
}
};
abstract int move(int row, int x);
}
public String solve(String input) {
int[] rows = parseRows(input);
int[] result = new int[9];
for (int i = 0; i < 9; i++) {
int beginX = i;
int x = beginX;
for (int y = 0; y < 4; y++) {
int r = rows[y];
Direction d = decide(r, x);
x = d.move(r, x);
}
result[x] = beginX;
}
return format(result);
}
String format(int[] result) {
StringBuilder sb = new StringBuilder();
for (int i : result) {
sb.append(String.valueOf(i));
}
return new String(sb);
}
Direction decide(int row, int x) {
boolean left = is(row, x - 1);
boolean current = is(row, x);
if (left == current) {
return Direction.down;
}
if (left) {
return Direction.left;
}
return Direction.right;
}
static boolean is(int row, int index) {
return ((row >> (7 - index)) & 1) != 0;
}
int[] parseRows(String input) {
String[] split = input.split("-");
int[] result = new int[4];
for (int i = 0; i < result.length; i++) {
result[i] = Integer.parseInt(split[i], 16);
}
return result;
}
}
package yhpg11;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import org.junit.Before;
import org.junit.Test;
import yhpg11.BitamidaEx.Direction;
public class BitamidaTest {
BitamidaEx target;
@Before
public void setUp() {
target = new BitamidaEx();
}
@Test
public void testFormat() throws Exception {
int[] actual = { 0, 1, 2, 3 };
assertEquals("0123", target.format(actual));
}
@Test
public void testParseRows() throws Exception {
String actual = "d6-7b-e1-9e";
int[] expected = { 0xd6, 0x7b, 0xe1, 0x9e };
assertArrayEquals(expected, target.parseRows(actual));
}
public void testDecide(Class<?> clazz, int row, int x) throws Exception {
assertEquals(clazz, target.decide(row, x).getClass());
}
@Test
public void testDecideRight() throws Exception {
int actualRow = 0xdb;
int actualX = 3;
testDecide(Direction.right.getClass(), actualRow, actualX);
}
@Test
public void testDecideLeft() throws Exception {
int actualRow = 0xdb;
int actualX = 2;
testDecide(Direction.left.getClass(), actualRow, actualX);
}
@Test
public void testDecideDown() throws Exception {
int actualRow = 0xdb;
int actualX = 4;
testDecide(Direction.down.getClass(), actualRow, actualX);
}
@Test
public void testDecideThrough() throws Exception {
int actualRow = 0xdf;
int actualX = 4;
testDecide(Direction.down.getClass(), actualRow, actualX);
}
@Test
public void testMoveRight() throws Exception {
Direction target = Direction.right;
assertEquals(2, target.move(0xdb, 0));
}
@Test
public void testMoveLeft() throws Exception {
Direction target = Direction.left;
assertEquals(3, target.move(0xdb, 5));
}
public void test(String input, String expected) {
assertEquals(input, target.solve(input), expected);
}
@Test
public void testSolve() throws Exception {
/* 0 */test("d6-7b-e1-9e", "740631825");
/* 1 */test("83-4c-20-10", "123805476");
/* 2 */test("fb-f7-7e-df", "274056813");
/* 3 */test("55-33-0f-ff", "123456780");
/* 4 */test("00-00-00-00", "012345678");
/* 5 */test("00-00-00-55", "021436587");
/* 6 */test("40-10-04-01", "021436587");
/* 7 */test("00-00-aa-00", "103254768");
/* 8 */test("80-20-08-02", "103254768");
/* 9 */test("ff-7e-3c-18", "876543210");
/* 10 */test("aa-55-aa-55", "351708264");
/* 11 */test("55-aa-aa-55", "012345678");
/* 12 */test("db-24-db-e7", "812543670");
/* 13 */test("00-01-00-40", "021345687");
/* 14 */test("00-00-80-00", "102345678");
/* 15 */test("01-40-00-00", "021345687");
/* 16 */test("00-00-00-02", "012345768");
/* 17 */test("00-00-02-00", "012345768");
/* 18 */test("00-14-00-00", "012436578");
/* 19 */test("00-00-01-40", "021345687");
/* 20 */test("00-80-01-00", "102345687");
/* 21 */test("c8-00-00-81", "120354687");
/* 22 */test("05-48-08-14", "021435687");
/* 23 */test("24-05-00-f0", "413205687");
/* 24 */test("40-08-14-01", "021536487");
/* 25 */test("18-c8-80-80", "210534678");
/* 26 */test("1c-88-52-00", "120564738");
/* 27 */test("ec-dc-67-62", "213468705");
/* 28 */test("0a-b6-60-e9", "035162784");
/* 29 */test("52-d6-c6-c2", "120345678");
/* 30 */test("47-e7-b0-36", "231047658");
/* 31 */test("0f-85-91-aa", "108263754");
/* 32 */test("76-b6-ed-f3", "601435782");
/* 33 */test("f5-5e-f7-3d", "025847163");
/* 34 */test("dd-e7-fb-f9", "610247538");
/* 35 */test("8f-f4-af-fd", "583246017");
/* 36 */test("bf-fb-cb-f7", "105382674");
/* 37 */test("e5-fd-ff-ff", "512046378");
/* 38 */test("ef-df-ef-fe", "713205648");
/* 39 */test("bf-7f-fd-d7", "826437105");
/* 40 */test("36-ff-df-de", "814527603");
/* 41 */test("6f-dd-ff-ff", "230685147");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment