オフラインリアルタイムどう書くF01 の問題

現地で終わらず、持ち帰って解いた。

現地ではL字マスを考慮にいれていなくて解けなかった。
とりあえず、現地で解いていた時の内容をできるだけ変えないようにしながらL字にも対応できるようにしたつもり。

```java:Yhpg.java


import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Yhpg {

	public int[] solve(String input){

		String[] data = input.split("/");
		for(int i = 0; i < data.length; i++){
			//８文字になるようにゼロうめ
			String zero = "0000000";
			data[i] = zero + Integer.toBinaryString(Integer.parseInt(data[i],16));
			data[i] = data[i].substring(data[i].length()-8, data[i].length());
		}

		//縦検索用の配列作成
		String[] data2 = makeDataForTate(data);

		int blackCount= 0;
		int whiteCount = 0;

		for( int y = 0; y < data.length; y++){
			// 横列の数をカウント
			blackCount += searchBlack(y, data[y], data);
			whiteCount += searchWhite(y, data[y], data);

			// 縦列の数をカウント
			blackCount += searchBlack(y, data2[y], data2);
			whiteCount += searchWhite(y, data2[y], data2);
		}

		System.out.println(whiteCount + "," + blackCount);
		int result[] = {whiteCount,blackCount};
		return result;
	}

	private int searchBlack(int y, String data, String[] allData){
		// 1の値が横に2個だけ並んでいるのをカウント
		int count = 0;;

		// 先頭がマッチするか
		String regex = "^110";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(data);

		if( m.find()){
			if( (isSeparate(m.start(), y,  allData, '1') == true)  && (isSeparate(m.start()+1, y,  allData, '1') == true) ){
				count++;
			}
		}

		// 末尾
		regex = "011$";
		p = Pattern.compile(regex);
		m = p.matcher(data);
		if( m.find()){
			if( (isSeparate(m.start()+1, y,  allData, '1') == true)  && (isSeparate(m.start()+2, y,  allData, '1') == true) ){
				count++;
			}
		}

		//　中間
		regex = "0110";
		p = Pattern.compile(regex);
		m = p.matcher(data);
		for( int i = 0; i< data.length();i++){
			if(m.find(i)){
				if( (isSeparate(m.start()+1, y,  allData, '1') == true)  && (isSeparate(m.start()+2, y,  allData, '1') == true) ){
					count++;
					i+=m.start();
				}
			}
		}

		return count;

	}
	private int searchWhite(int y, String data,String[] allData){
		// 0の値が横に2個だけ並んでいるのをカウント
		int count = 0;;

		// 先頭がマッチするか
		String regex = "^001";
		Pattern p = Pattern.compile(regex);
		Matcher m = p.matcher(data);
		if( m.find()){
			if( (isSeparate(m.start(), y,  allData, '0') == true)  && (isSeparate(m.start()+1, y,  allData, '0') == true) ){
				count++;
			}
		}

		// 末尾
		regex = "100$";
		p = Pattern.compile(regex);
		m = p.matcher(data);
		if( m.find()){
			if( (isSeparate(m.start()+1, y,  allData, '0') == true)  && (isSeparate(m.start()+2, y,  allData, '0') == true) ){
				count++;
			}
		}

		//　中間
		regex = "1001";
		p = Pattern.compile(regex);
		m = p.matcher(data);

		for( int i = 0; i< data.length();i++){
			if(m.find(i)){
				if( (isSeparate(m.start()+1, y,  allData, '0') == true)  && (isSeparate(m.start()+2, y,  allData, '0') == true) ){
					count++;
					i+=m.start();
				}
			}
		}
		return count;
	}

	// 指定したマスが孤立してるかどうか見る
	// ２マスだけ並んでた際の左側の上下が別の色ならtrue
	// 同じ色が含まれていたらfalseを返す
	private boolean isSeparate(int x,int y, String[] data, char target){
		// 上側チェック
		if( !(y == 0) ){	//最上部だったら上側チェックはスルー
			if( (data[y-1].charAt(x) == target)){
				return false;
			}
		}
		// 左側右側チェックは正規表現で同じ色になってることはないはずなので、やらないことにした
		// 下側チェック
		if ( (y < data.length -1) ){	// 最下部だったら下側チェックはスル
			if( (data[y+1].charAt(x) == target)){
				return false;
			}
		}
		return true;
	}

	private String[] makeDataForTate(String[] data){
		String[] result = {"","","","","","","",""};
		for( int y = 0; y < data.length; y++){
			for(int x = 0; x < data[y].length(); x++){
				result[y] += data[x].charAt(y);
			}
		}
		return result;
	}
}

```

```java:YhpgTest.java
import static org.junit.Assert.*;

import org.junit.Test;

public class YhpgTest{

	@Test
	public void yhpgTest(){
		Yhpg yhpg = new Yhpg();

		assertArrayEquals(new int[]{2,3} , yhpg.solve("dc/bc/a7/59/03/d5/d4/ea"));
		assertArrayEquals(new int[]{0,0}, yhpg.solve("ff/ff/ff/ff/ff/ff/ff/ff"));
		assertArrayEquals(new int[]{0,0}, yhpg.solve("00/00/00/00/00/00/00/00"));
		assertArrayEquals(new int[]{16,16}, yhpg.solve("cc/33/cc/33/cc/33/cc/33"));
		assertArrayEquals(new int[]{16,16}, yhpg.solve("aa/aa/55/55/aa/aa/55/55"));
		assertArrayEquals(new int[]{8,8}, yhpg.solve("ac/a3/5c/53/ca/3a/c5/35"));
		assertArrayEquals(new int[]{0,13}, yhpg.solve("db/00/db/00/db/00/aa/aa"));
		assertArrayEquals(new int[]{0,12}, yhpg.solve("24/24/db/24/24/db/24/24"));
		assertArrayEquals(new int[]{3,2}, yhpg.solve("d7/d7/e9/f1/f7/de/60/56"));
		assertArrayEquals(new int[]{2,2}, yhpg.solve("17/7d/64/9b/a5/39/53/a6"));
		assertArrayEquals(new int[]{1,2}, yhpg.solve("bb/8f/18/fb/89/c2/c7/35"));
		assertArrayEquals(new int[]{2,2}, yhpg.solve("6d/63/20/08/54/cd/32/4f"));
		assertArrayEquals(new int[]{2,2}, yhpg.solve("a9/ca/cd/46/99/e6/f0/30"));
		assertArrayEquals(new int[]{1,2}, yhpg.solve("5b/70/fd/45/e2/a1/ab/9a"));
		assertArrayEquals(new int[]{2,1}, yhpg.solve("24/e4/a8/12/e1/a6/3f/f3"));
		assertArrayEquals(new int[]{2,2}, yhpg.solve("79/32/2e/07/d5/10/e7/9d"));
		assertArrayEquals(new int[]{4,2}, yhpg.solve("60/bc/ab/ec/1f/eb/63/2c"));
		assertArrayEquals(new int[]{6,1}, yhpg.solve("a5/dd/92/4e/67/c6/dc/34"));
		assertArrayEquals(new int[]{3,2}, yhpg.solve("aa/96/6d/67/d2/a8/ac/90"));
		assertArrayEquals(new int[]{4,0}, yhpg.solve("95/72/7d/5c/47/dc/ef/99"));
		assertArrayEquals(new int[]{2,1}, yhpg.solve("17/d6/6a/27/1f/25/26/b8"));
		assertArrayEquals(new int[]{1,2}, yhpg.solve("f0/f3/76/c5/31/ca/6b/ae"));
		assertArrayEquals(new int[]{1,4}, yhpg.solve("01/59/26/fa/8c/70/12/cd"));
		assertArrayEquals(new int[]{0,5}, yhpg.solve("1a/c3/1f/0b/83/b6/81/0d"));
		assertArrayEquals(new int[]{1,2}, yhpg.solve("4c/49/05/cf/54/bb/1f/da"));
		assertArrayEquals(new int[]{0,7}, yhpg.solve("eb/7c/d5/09/2a/c2/14/6b"));
		assertArrayEquals(new int[]{1,3}, yhpg.solve("b4/d3/4c/c4/ed/19/e8/63"));
		assertArrayEquals(new int[]{2,4}, yhpg.solve("bd/bc/6d/60/9b/00/9a/32"));
		assertArrayEquals(new int[]{2,2}, yhpg.solve("94/97/3f/e3/c7/06/15/c0"));
		assertArrayEquals(new int[]{2,2}, yhpg.solve("5f/1d/67/16/b8/f7/0a/2a"));
		assertArrayEquals(new int[]{3,0}, yhpg.solve("df/e6/f9/4f/59/e9/1f/ee"));
		assertArrayEquals(new int[]{3,2}, yhpg.solve("5a/53/9a/9a/73/b4/37/07"));
		assertArrayEquals(new int[]{2,3}, yhpg.solve("bd/87/7c/e7/c0/37/82/da"));
		assertArrayEquals(new int[]{2,2}, yhpg.solve("3d/c0/13/ac/57/3d/15/78"));
		assertArrayEquals(new int[]{0,3}, yhpg.solve("63/64/54/3a/40/28/4e/4e"));
		assertArrayEquals(new int[]{1,4}, yhpg.solve("f6/81/c9/15/00/4c/a0/a8"));
		assertArrayEquals(new int[]{4,2}, yhpg.solve("19/41/df/f8/e3/74/6b/9b"));
		assertArrayEquals(new int[]{1,5}, yhpg.solve("d5/0b/dd/35/3b/d2/0b/6b"));
		assertArrayEquals(new int[]{0,0}, yhpg.solve("08/b7/91/f3/6e/3c/74/a0"));
		assertArrayEquals(new int[]{0,0}, yhpg.solve("b8/a8/b4/a6/93/2c/94/3f"));
		assertArrayEquals(new int[]{0,0}, yhpg.solve("88/22/21/ee/dc/19/43/01"));
		assertArrayEquals(new int[]{0,0}, yhpg.solve("e1/ee/35/bc/fc/00/8e/fe"));
		assertArrayEquals(new int[]{0,0}, yhpg.solve("3c/42/63/5f/27/47/07/90"));

	}
}

```