Skip to content

Instantly share code, notes, and snippets.

@torazuka
Created January 1, 2014 17:20
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/8209724 to your computer and use it in GitHub Desktop.
Save torazuka/8209724 to your computer and use it in GitHub Desktop.
第16回オフラインリアルタイムどう書くのJava解答(nidouchiさんの解答の再実装)
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.Test;
/***
* Question: http://nabetani.sakura.ne.jp/hena/ord16boseg/ -- this answer is a
* RE implementation of the answer,
* http://qiita.com/nidouchi/items/433563d664c0a1983919
*/
public class Boseg {
public String solve(String input) {
int[][] table = parse(input);
int[] hLineCount = countLine(table);
int[] vLineCount = countLine(transpose(table));
return format(hLineCount, vLineCount);
}
String format(int[] hlist, int[] vlist) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < hlist.length; i++) {
sb.append(String.valueOf(hlist[i] + vlist[i]));
sb.append(",");
}
sb.deleteCharAt(sb.length() - 1);
return new String(sb);
}
int[][] transpose(int[][] old) {
int[][] result = new int[old[0].length][old.length];
for (int i = 0; i < old.length; i++) {
for (int j = 0; j < old[0].length; j++) {
result[j][old.length - 1 - i] = old[i][j];
}
}
return result;
}
int[] countLine(int[][] table) {
List<boolean[]> lines = new ArrayList<>();
for (int i = 0; i < table.length - 1; i++) {
boolean[] w = new boolean[table.length];
for (int j = 0; j < table.length; j++) {
if (table[i][j] != table[i + 1][j]) {
w[j] = true;
}
}
lines.add(w);
}
int[] result = { 0, 0, 0, 0, 0, 0 };
for (boolean[] row : lines) {
int count = 0;
for (boolean cell : row) {
if (cell) {
count++;
} else {
if (0 < count) {
result[count - 1] = result[count - 1] + 1;
count = 0;
}
}
}
if (0 < count) {
result[count - 1] = result[count - 1] + 1;
}
}
return result;
}
int[][] parse(String input) {
char[] charArray = input.toCharArray();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < charArray.length; i++) {
sb.append(toBinary(String.valueOf(input.charAt(i))));
}
String str = new String(sb);
int cnt = 0;
int[][] result = new int[6][6];
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
result[i][j] = Integer.parseInt(String.valueOf(str
.charAt(cnt++)));
}
}
return result;
}
String toBinary(String s) {
int o = Integer.parseInt(String.valueOf(s), 8);
String binary = Integer.toBinaryString(o);
if (binary.length() < 3) {
for (; binary.length() < 3;) {
binary = "0" + binary;
}
}
return binary;
}
@Test
public void testTranspose() throws Exception {
Boseg target = new Boseg();
int[][] input = { { 0, 1 }, { 0, 0 }, { 1, 0 } };
int[][] expected = { { 1, 0, 0 }, { 0, 0, 1 } };
int[][] actual = target.transpose(input);
for (int i = 0; i < 2; i++) {
assertEquals(true, Arrays.equals(expected[i], actual[i]));
}
}
@Test
public void testParse() throws Exception {
Boseg target = new Boseg();
int[][] expected = { { 0, 0, 0, 1, 1, 0 }, { 0, 0, 0, 0, 1, 0 },
{ 1, 1, 1, 1, 1, 0 }, { 1, 1, 1, 0, 1, 0 },
{ 1, 0, 0, 0, 1, 0 }, { 1, 1, 1, 1, 1, 0 } };
int[][] actual = target.parse("060276724276");
for (int i = 0; i < 6; i++) {
assertEquals(true, Arrays.equals(expected[i], actual[i]));
}
}
@Test
public void testToBinary() throws Exception {
Boseg target = new Boseg();
assertEquals("000", target.toBinary("0"));
assertEquals("001", target.toBinary("1"));
assertEquals("110", target.toBinary("6"));
assertEquals("111", target.toBinary("7"));
}
private int counter = 0;
void test(String input, String expected) {
Boseg boseg = new Boseg();
System.out.println(counter++
+ (expected.equals(boseg.solve(input)) ? " ok"
: " **** NG ****"));
// assertEquals(expected, boseg.solve(input));
}
@Test
public void testBoseg() throws Exception {
/* 0 */test("060276724276", "6,2,1,1,0,1");
/* 1 */test("770175454177", "2,3,0,3,1,0");
/* 2 */test("743733377170", "9,3,1,0,0,0");
/* 3 */test("724212121273", "5,2,1,1,1,1");
/* 4 */test("100000000000", "3,0,0,0,0,0");
/* 5 */test("000002000000", "4,0,0,0,0,0");
/* 6 */test("003622223600", "0,4,0,4,0,0");
/* 7 */test("520073737070", "8,3,1,1,0,0");
/* 8 */test("770077007700", "0,0,0,0,0,5");
/* 9 */test("555555555514", "2,0,0,0,2,2");
/* 10 */test("764252427600", "4,0,4,0,2,0");
/* 11 */test("774555554177", "3,3,1,3,0,0");
/* 12 */test("674574754557", "11,5,0,1,0,0");
/* 13 */test("000000000000", "0,0,0,0,0,0");
/* 14 */test("777777777777", "0,0,0,0,0,0");
/* 15 */test("774377777577", "6,0,2,0,0,0");
/* 16 */test("070777777777", "0,1,1,0,0,0");
/* 17 */test("373737373737", "0,0,0,0,0,1");
/* 18 */test("603260327725", "30,0,0,0,0,0");
/* 19 */test("466331144663", "30,0,0,0,0,0");
/* 20 */test("000000000242", "3,2,0,0,0,0");
/* 21 */test("567656043772", "18,2,1,0,0,0");
/* 22 */test("200763012420", "15,4,1,0,0,0");
/* 23 */test("400101140052", "14,3,0,0,0,0");
/* 24 */test("764767476476", "13,2,0,1,0,0");
/* 25 */test("001110140110", "12,2,1,0,0,0");
/* 26 */test("765405076527", "16,3,0,1,0,0");
/* 27 */test("377323370373", "8,4,2,0,0,0");
/* 28 */test("250541131216", "11,5,2,0,0,0");
/* 29 */test("744165741476", "12,3,2,0,0,0");
/* 30 */test("042101000300", "10,3,0,0,0,0");
/* 31 */test("002004554101", "11,3,1,0,0,0");
/* 32 */test("371707762706", "15,1,1,0,0,0");
/* 33 */test("130371310175", "7,3,1,2,0,0");
/* 34 */test("212537003613", "13,2,1,1,1,0");
/* 35 */test("157700063411", "15,3,0,0,0,1");
/* 36 */test("011500036007", "6,7,1,0,0,0");
/* 37 */test("743113313517", "17,2,1,0,0,0");
/* 38 */test("174105270405", "13,3,1,1,0,0");
/* 39 */test("427272200311", "13,3,2,0,0,0");
/* 40 */test("725370332237", "12,5,1,1,0,0");
/* 41 */test("005640420046", "12,1,3,0,0,0");
/* 42 */test("700350001101", "14,3,1,0,0,0");
/* 43 */test("577627744076", "16,1,1,1,0,0");
/* 44 */test("620332232007", "10,4,2,1,0,0");
/* 45 */test("260406401000", "15,1,1,0,0,0");
/* 46 */test("737272723276", "5,0,0,0,3,0");
/* 47 */test("000400040444", "7,0,2,0,0,0");
/* 48 */test("370222002177", "13,2,2,0,0,0");
/* 49 */test("372236024656", "9,3,2,0,1,0");
/* 50 */test("276131137003", "11,6,2,0,0,0");
/* 51 */test("742134007240", "13,4,2,0,0,0");
/* 52 */test("777721775571", "13,1,2,0,0,0");
/* 53 */test("700301232233", "11,2,3,0,0,0");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment