Compare commits

...

3 Commits

Author SHA1 Message Date
Sugui 3427e53698 moved each day to their specific folder 2023-12-07 12:09:13 +01:00
Sugui 9f6b734836 day 7 (sorry for so many aux classes) 2023-12-07 12:06:55 +01:00
Sugui 62bc45f2b8 removed warnings 2023-12-07 09:17:16 +01:00
17 changed files with 1276 additions and 7 deletions

1000
resources/input07.txt Normal file

File diff suppressed because it is too large Load Diff

View File

@ -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;

View File

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

View File

@ -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()));
}
}

View File

@ -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()));
}
}

View File

@ -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);
}
}

View File

@ -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,
}
}

View File

@ -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);
}
}
}

View File

@ -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);
}
}
}

View File

@ -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

View File

@ -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);
}
}

View File

@ -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;