Last active
December 17, 2015 04:49
-
-
Save torazuka/5553545 to your computer and use it in GitHub Desktop.
「オフラインリアルタイムどう書く第10回」の参考問題解答
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
package sample10; | |
import static org.junit.Assert.assertEquals; | |
import java.util.ArrayList; | |
import java.util.Collections; | |
import java.util.Comparator; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Set; | |
import org.junit.Test; | |
class Card { | |
Suit suit; | |
int rank; | |
Card(Suit s, String r) { | |
suit = s; | |
rank = convertRank(r); | |
} | |
private int convertRank(String s) { | |
if (s.equals("A")) { | |
return 1; | |
} | |
if (s.equals("J")) { | |
return 11; | |
} | |
if (s.equals("Q")) { | |
return 12; | |
} | |
if (s.equals("K")) { | |
return 13; | |
} | |
return Integer.parseInt(s); | |
} | |
} | |
enum Suit { | |
s, h, d, c; | |
} | |
/** | |
* 問題: http://nabetani.sakura.ne.jp/hena/ord10pokarest/ | |
*/ | |
public class Pokarest { | |
public String solve(final String input) { | |
List<Card> cards = makeCards(input); | |
sort(cards); | |
List<Card> sameSuit = getSameSuit(cards); | |
int sameSuitNum = sameSuit.size(); | |
int straightNum = getStraightNum(cards); | |
int straightInSuitNum = getStraightNum(sameSuit); | |
if (sameSuitNum == 5 && straightNum == 5 && isRoyal(sameSuit)) { | |
return "RF"; | |
} else if (sameSuitNum == 5 && straightNum == 5) { | |
return "SF"; | |
} else if (sameSuitNum == 5) { | |
return "FL"; | |
} else if (straightNum == 5) { | |
return "ST"; | |
} else if (sameSuitNum == 4 && straightInSuitNum == 4) { | |
return "4SF"; | |
} else if (sameSuitNum == 4) { | |
return "4F"; | |
} else if (straightNum == 4) { | |
return "4S"; | |
} | |
return "-"; | |
} | |
private void sort(List<Card> cards) { | |
Collections.sort(cards, new Comparator<Card>() { | |
@Override | |
public int compare(Card o1, Card o2) { | |
return o1.rank - o2.rank; | |
} | |
}); | |
} | |
private boolean isRoyal(final List<Card> cards) { | |
// ランクの昇順にソートされていることを前提にする | |
if (cards.get(1).rank - cards.get(0).rank == 9) { | |
return true; | |
} | |
return false; | |
} | |
private List<Card> getSameSuit(final List<Card> cards) { | |
List<Card> result = new ArrayList<>(); | |
Set<Suit> suitSet = new HashSet<>(); | |
for (Card each : cards) { | |
suitSet.add(each.suit); | |
} | |
List<List<Card>> cardLists = new ArrayList<>(); | |
for (Suit suit : suitSet) { | |
List<Card> tmp = new ArrayList<>(); | |
for (Card each : cards) { | |
if (suit.equals(each.suit)) { | |
tmp.add(each); | |
} | |
} | |
cardLists.add(tmp); | |
} | |
for (List<Card> list : cardLists) { | |
if (3 < list.size()) { | |
result = list; | |
} | |
} | |
return result; | |
} | |
private int getStraightNum(final List<Card> cards) { | |
List<Integer> results = new ArrayList<>(); | |
int result = 1; | |
List<Integer> ranks = new ArrayList<>(); | |
for (Card each : cards) { | |
ranks.add(each.rank); | |
} | |
if (ranks.contains(1) && ranks.contains(13)) { | |
ranks.add(14); | |
} | |
for (int i = 0; i < ranks.size() - 1; i++) { | |
if (ranks.get(i + 1) - ranks.get(i) == 1) { | |
result++; | |
} else { | |
results.add(result); | |
result = 1; | |
} | |
} | |
results.add(result); | |
return Collections.max(results); | |
} | |
private List<Card> makeCards(final String input) { | |
List<Card> result = new ArrayList<>(); | |
Suit tmpSuit = null; | |
String tmpRank = ""; | |
for (char c : input.toCharArray()) { | |
if (isSuit(c)) { | |
tmpSuit = Suit.valueOf(String.valueOf(c)); | |
result.add(new Card(tmpSuit, tmpRank)); | |
tmpSuit = null; | |
tmpRank = ""; | |
} else { | |
tmpRank += String.valueOf(c); | |
} | |
} | |
return result; | |
} | |
private boolean isSuit(final char c) { | |
String s = String.valueOf(c); | |
if (s.equals(Suit.s.name()) || s.equals(Suit.h.name()) | |
|| s.equals(Suit.d.name()) || s.equals(Suit.c.name())) { | |
return true; | |
} | |
return false; | |
} | |
private void test(String input, String expect) { | |
Pokarest p = new Pokarest(); | |
assertEquals("id:" + testId++, expect, p.solve(input)); | |
} | |
static int testId = 0; | |
@Test | |
public void testPokarest() throws Exception { | |
/* 0 */test("Qs9s3dJd10h", "4S"); | |
/* 1 */test("KdAdJd10dQd", "RF"); | |
/* 2 */test("QhJhKhAh10h", "RF"); | |
/* 3 */test("10dAdJsQdKd", "ST"); | |
/* 4 */test("Kd10dAdJd3d", "FL"); | |
/* 5 */test("4d3d2dAd5d", "SF"); | |
/* 6 */test("5d5d2d3dAd", "FL"); | |
/* 7 */test("4d2sAd5d3d", "ST"); | |
/* 8 */test("As10dJdQdKd", "ST"); | |
/* 9 */test("10d10dQdAsJd", "4F"); | |
/* 10 */test("AcJd10dQdKd", "ST"); | |
/* 11 */test("Kd2sJdAdQd", "4SF"); | |
/* 12 */test("JdAdQcKd2s", "4S"); | |
/* 13 */test("KdAdKdJd2s", "4F"); | |
/* 14 */test("As2dKdQdJd", "4F"); | |
/* 15 */test("AsKdQd2dJh", "4S"); | |
/* 16 */test("QhAd2s3dKd", "-"); | |
/* 17 */test("Ad4dKh3s2d", "4S"); | |
/* 18 */test("3d2dAh5d4s", "ST"); | |
/* 19 */test("QcKdAs2dJd", "4S"); | |
/* 20 */test("2dQcJdAs10d", "-"); | |
/* 21 */test("4d7d5s3c2d", "4S"); | |
/* 22 */test("7d5s4dAd3c", "-"); | |
/* 23 */test("3s8s10sQs6s", "FL"); | |
/* 24 */test("6hAh3h2h8h", "FL"); | |
/* 25 */test("3h4hJh9hQh", "FL"); | |
/* 26 */test("3s6s5s2sQs", "FL"); | |
/* 27 */test("9d3cKdQc2c", "-"); | |
/* 28 */test("5sKs7hQcKh", "-"); | |
/* 29 */test("Ad6d7h7c9h", "-"); | |
/* 30 */test("10h4cAh6s10c", "-"); | |
/* 31 */test("9sKsJcQs10d", "ST"); | |
/* 32 */test("5d3c2cAs4c", "ST"); | |
/* 33 */test("KcQs9c10sJs", "ST"); | |
/* 34 */test("9d8s10hJdQd", "ST"); | |
/* 35 */test("6c5s10h7d4c", "4S"); | |
/* 36 */test("QhJcKsAh8c", "4S"); | |
/* 37 */test("JsQc3h10cKs", "4S"); | |
/* 38 */test("10c9h7hAd8d", "4S"); | |
/* 39 */test("3d4dKd8d5c", "4F"); | |
/* 40 */test("10h3hQh9h2s", "4F"); | |
/* 41 */test("Qh5h7h9h6c", "4F"); | |
/* 42 */test("6s8s7s3sKc", "4F"); | |
/* 43 */test("10h8h9hJhQh", "SF"); | |
/* 44 */test("10h9hQhKhJh", "SF"); | |
/* 45 */test("6d4d7d5d3d", "SF"); | |
/* 46 */test("6h9h7h5h8h", "SF"); | |
/* 47 */test("Ac6s4s3s5s", "4SF"); | |
/* 48 */test("3c9d2c5c4c", "4SF"); | |
/* 49 */test("Kh2sQh10hJh", "4SF"); | |
/* 50 */test("4h5h2h3h4s", "4SF"); | |
/* 51 */test("Js10sAsQsKs", "RF"); | |
/* 52 */test("10dKdQdAdJd", "RF"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment