Compare commits

..

No commits in common. "3427e536981406fd12e48a401b93fe262f6d9051" and "d0e01148ffb4558a7ed4d2dc2cd284e675f1ca76" have entirely different histories.

17 changed files with 7 additions and 1276 deletions

File diff suppressed because it is too large Load Diff

View File

@ -1,9 +1,11 @@
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;

View File

@ -1,4 +0,0 @@
package sugui.day07;
public record Card(String label) {
}

View File

@ -1,14 +0,0 @@
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()));
}
}

View File

@ -1,13 +0,0 @@
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()));
}
}

View File

@ -1,60 +0,0 @@
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);
}
}

View File

@ -1,14 +0,0 @@
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,
}
}

View File

@ -1,63 +0,0 @@
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);
}
}
}

View File

@ -1,72 +0,0 @@
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);
}
}
}

View File

@ -3,7 +3,9 @@ package sugui.util;
import java.util.Iterator;
public class IntervalIterator implements Iterator<Long> {
private Interval interval;
public IntervalIterator(Interval interval) {
this.interval = interval;
}
@Override

View File

@ -1,36 +0,0 @@
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);
}
}

View File

@ -1,8 +1,11 @@
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;