Compare commits
3 Commits
d0e01148ff
...
3427e53698
Author | SHA1 | Date |
---|---|---|
Sugui | 3427e53698 | |
Sugui | 9f6b734836 | |
Sugui | 62bc45f2b8 |
File diff suppressed because it is too large
Load Diff
|
@ -1,11 +1,9 @@
|
|||
package sugui;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Array;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
|
@ -0,0 +1,4 @@
|
|||
package sugui.day07;
|
||||
|
||||
public record Card(String label) {
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package sugui.day07;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public class CardComparator implements Comparator<Card> {
|
||||
|
||||
private static String ORDERING = "23456789TJQKA";
|
||||
|
||||
@Override
|
||||
public int compare(Card c1, Card c2) {
|
||||
return Integer.compare(ORDERING.indexOf(c1.label()), ORDERING.indexOf(c2.label()));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package sugui.day07;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
public class CardWithJokerComparator implements Comparator<Card> {
|
||||
private static String ORDERING = "J23456789TQKA";
|
||||
|
||||
@Override
|
||||
public int compare(Card c1, Card c2) {
|
||||
return Integer.compare(ORDERING.indexOf(c1.label()), ORDERING.indexOf(c2.label()));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,60 @@
|
|||
package sugui.day07;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.StringReader;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class Day07 {
|
||||
|
||||
public static Path inputPath = Paths.get("resources/input07.txt");
|
||||
|
||||
// Other static variables needed to solve the problem...
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
var input = Files.readString(inputPath);
|
||||
var firstResult = getFirstPuzzleResult(input);
|
||||
var secondResult = getSecondPuzzleResult(input);
|
||||
System.out.println("First result: " + firstResult);
|
||||
System.out.println("Second result: " + secondResult);
|
||||
}
|
||||
|
||||
public static List<Hand> parsing(String input) {
|
||||
var bufferedReader = new BufferedReader(new StringReader(input));
|
||||
return bufferedReader.lines().map(line -> {
|
||||
String[] splittedLine = line.split(" ");
|
||||
String[] labels = splittedLine[0].split("");
|
||||
int bid = Integer.parseInt(splittedLine[1]);
|
||||
return new Hand(new Card(labels[0]), new Card(labels[1]), new Card(labels[2]), new Card(labels[3]),
|
||||
new Card(labels[4]), bid);
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public static String getFirstPuzzleResult(String input) {
|
||||
List<Hand> hands = parsing(input);
|
||||
hands.sort(new HandComparator());
|
||||
int currentRank = 1;
|
||||
int result = 0;
|
||||
for (Hand hand : hands) {
|
||||
result += hand.bid() * currentRank;
|
||||
currentRank++;
|
||||
}
|
||||
return Integer.toString(result);
|
||||
}
|
||||
|
||||
public static String getSecondPuzzleResult(String input) {
|
||||
List<Hand> hands = parsing(input);
|
||||
hands.sort(new HandWithJokerComparator());
|
||||
int currentRank = 1;
|
||||
int result = 0;
|
||||
for (Hand hand : hands) {
|
||||
result += hand.bid() * currentRank;
|
||||
currentRank++;
|
||||
}
|
||||
return Integer.toString(result);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,14 @@
|
|||
package sugui.day07;
|
||||
|
||||
public record Hand(Card c1, Card c2, Card c3, Card c4, Card c5, int bid) {
|
||||
|
||||
public enum HandType {
|
||||
HIGH_CARD,
|
||||
ONE_PAIR,
|
||||
TWO_PAIR,
|
||||
THREE_OF_A_KIND,
|
||||
FULL_HOUSE,
|
||||
FOUR_OF_A_KIND,
|
||||
FIVE_OF_A_KIND,
|
||||
}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
package sugui.day07;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import sugui.day07.Hand.HandType;
|
||||
|
||||
public class HandComparator implements Comparator<Hand> {
|
||||
|
||||
private HandType getHandType(Hand hand) {
|
||||
Map<String, Integer> cardCount = new HashMap<>();
|
||||
for (Card c : List.of(hand.c1(), hand.c2(), hand.c3(), hand.c4(), hand.c5())) {
|
||||
if (cardCount.containsKey(c.label()))
|
||||
cardCount.compute(c.label(), (k, v) -> v + 1);
|
||||
else
|
||||
cardCount.put(c.label(), 1);
|
||||
}
|
||||
List<Integer> counts = new ArrayList<>(cardCount.values());
|
||||
counts.sort(Comparator.reverseOrder());
|
||||
int fstCount = counts.get(0);
|
||||
int sndCount = counts.size() == 1 ? 0 : counts.get(1);
|
||||
if (fstCount == 5)
|
||||
return HandType.FIVE_OF_A_KIND;
|
||||
else if (fstCount == 4)
|
||||
return HandType.FOUR_OF_A_KIND;
|
||||
else if (fstCount == 3 && sndCount == 2)
|
||||
return HandType.FULL_HOUSE;
|
||||
else if (fstCount == 3 && sndCount == 1)
|
||||
return HandType.THREE_OF_A_KIND;
|
||||
else if (fstCount == 2 && sndCount == 2)
|
||||
return HandType.TWO_PAIR;
|
||||
else if (fstCount == 2 && sndCount == 1)
|
||||
return HandType.ONE_PAIR;
|
||||
else
|
||||
return HandType.HIGH_CARD;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compare(Hand h1, Hand h2) {
|
||||
HandType h1HandType = getHandType(h1);
|
||||
HandType h2HandType = getHandType(h2);
|
||||
|
||||
if (h1HandType.equals(h2HandType)) {
|
||||
var cardComparator = new CardComparator();
|
||||
int cmp1 = cardComparator.compare(h1.c1(), h2.c1());
|
||||
int cmp2 = cardComparator.compare(h1.c2(), h2.c2());
|
||||
int cmp3 = cardComparator.compare(h1.c3(), h2.c3());
|
||||
int cmp4 = cardComparator.compare(h1.c4(), h2.c4());
|
||||
int cmp5 = cardComparator.compare(h1.c5(), h2.c5());
|
||||
return Stream.of(cmp1, cmp2, cmp3, cmp4, cmp5)
|
||||
.dropWhile(cmp -> cmp == 0)
|
||||
.findFirst()
|
||||
.orElse(0);
|
||||
} else {
|
||||
return h1HandType.compareTo(h2HandType);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,72 @@
|
|||
package sugui.day07;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import sugui.day07.Hand.HandType;
|
||||
|
||||
public class HandWithJokerComparator implements Comparator<Hand> {
|
||||
|
||||
private HandType getHandType(Hand hand) {
|
||||
Map<String, Integer> cardCount = new HashMap<>();
|
||||
int jokerCount = 0;
|
||||
for (Card c : List.of(hand.c1(), hand.c2(), hand.c3(), hand.c4(), hand.c5())) {
|
||||
if ("J".equals(c.label())) {
|
||||
jokerCount++;
|
||||
} else if (cardCount.containsKey(c.label())) {
|
||||
cardCount.compute(c.label(), (k, v) -> v + 1);
|
||||
} else {
|
||||
cardCount.put(c.label(), 1);
|
||||
}
|
||||
}
|
||||
List<Integer> counts = new ArrayList<>(cardCount.values());
|
||||
counts.sort(Comparator.reverseOrder());
|
||||
if (counts.size() == 0) {
|
||||
// Hand of full Jokers
|
||||
return HandType.FIVE_OF_A_KIND;
|
||||
} else {
|
||||
int fstCount = counts.get(0) + jokerCount;
|
||||
int sndCount = counts.size() == 1 ? 0 : counts.get(1);
|
||||
if (fstCount == 5)
|
||||
return HandType.FIVE_OF_A_KIND;
|
||||
else if (fstCount == 4)
|
||||
return HandType.FOUR_OF_A_KIND;
|
||||
else if (fstCount == 3 && sndCount == 2)
|
||||
return HandType.FULL_HOUSE;
|
||||
else if (fstCount == 3 && sndCount == 1)
|
||||
return HandType.THREE_OF_A_KIND;
|
||||
else if (fstCount == 2 && sndCount == 2)
|
||||
return HandType.TWO_PAIR;
|
||||
else if (fstCount == 2 && sndCount == 1)
|
||||
return HandType.ONE_PAIR;
|
||||
else
|
||||
return HandType.HIGH_CARD;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compare(Hand h1, Hand h2) {
|
||||
HandType h1HandType = getHandType(h1);
|
||||
HandType h2HandType = getHandType(h2);
|
||||
|
||||
if (h1HandType.equals(h2HandType)) {
|
||||
var cardComparator = new CardWithJokerComparator();
|
||||
int cmp1 = cardComparator.compare(h1.c1(), h2.c1());
|
||||
int cmp2 = cardComparator.compare(h1.c2(), h2.c2());
|
||||
int cmp3 = cardComparator.compare(h1.c3(), h2.c3());
|
||||
int cmp4 = cardComparator.compare(h1.c4(), h2.c4());
|
||||
int cmp5 = cardComparator.compare(h1.c5(), h2.c5());
|
||||
return Stream.of(cmp1, cmp2, cmp3, cmp4, cmp5)
|
||||
.dropWhile(cmp -> cmp == 0)
|
||||
.findFirst()
|
||||
.orElse(0);
|
||||
} else {
|
||||
return h1HandType.compareTo(h2HandType);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -3,9 +3,7 @@ package sugui.util;
|
|||
import java.util.Iterator;
|
||||
|
||||
public class IntervalIterator implements Iterator<Long> {
|
||||
private Interval interval;
|
||||
public IntervalIterator(Interval interval) {
|
||||
this.interval = interval;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
package sugui;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class Day07Test {
|
||||
|
||||
public static String firstBasicInput = """
|
||||
32T3K 765
|
||||
T55J5 684
|
||||
KK677 28
|
||||
KTJJT 220
|
||||
QQQJA 483
|
||||
""";
|
||||
public static String secondBasicInput = firstBasicInput;
|
||||
public static String firstBasicResult = "6440";
|
||||
public static String secondBasicResult = "5905";
|
||||
|
||||
@Test
|
||||
public void firstBasicCase() throws IOException {
|
||||
String actualResult = Day07.getFirstPuzzleResult(firstBasicInput);
|
||||
assertEquals(firstBasicResult, actualResult);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void secondBasicCase() throws IOException {
|
||||
String actualResult = Day07.getSecondPuzzleResult(secondBasicInput);
|
||||
assertEquals(secondBasicResult, actualResult);
|
||||
}
|
||||
}
|
|
@ -1,11 +1,8 @@
|
|||
package sugui;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import sugui.util.Interval;
|
||||
|
|
Loading…
Reference in New Issue