This commit is contained in:
Daniel Bulant 2023-12-07 08:50:52 +01:00
parent 748de41102
commit 7633eac086
6 changed files with 1118 additions and 4 deletions

View file

@ -39,6 +39,10 @@ fn part2(file: &str) -> i32 {
solution
}
fn main() {
dbg!(part2("input2"));
}
#[cfg(test)]
mod tests {
use super::*;
@ -66,8 +70,4 @@ mod tests {
fn part2_input3() {
assert_eq!(part2("input3"), 281)
}
}
fn main() {
todo!();
}

5
2023/7/input1 Normal file
View file

@ -0,0 +1,5 @@
32T3K 765
T55J5 684
KK677 28
KTJJT 220
QQQJA 483

1000
2023/7/input2 Normal file

File diff suppressed because it is too large Load diff

9
2023/7/inputt Normal file
View file

@ -0,0 +1,9 @@
AAAAA 1
JJJJJ 2
AA8AA 3
23332 4
TTT98 5
TTJ98 6
23432 7
A23A4 8
23456 9

47
2023/7/part1.py Normal file
View file

@ -0,0 +1,47 @@
import functools
cards = ["A", "K", "Q", "J", "T", "9", "8", "7", "6", "5", "4", "3", "2"]
cards.reverse()
hands = [(line.split(" ")[0], int(line.split(" ")[1])) for line in open("./input2")]
def type_value(hand):
hand_count = {}
for card in hand:
if card[0] in hand_count:
hand_count[card[0]] += 1
else:
hand_count[card[0]] = 1
hand_max = max(hand_count.values())
if hand_max >= 4: return hand_max + 2
if hand_max == 3 and 2 in hand_count.values(): return 5
if hand_max == 3: return 4
if hand_max == 2 and list(hand_count.values()).count(2) == 2: return 3
return hand_max
def compare(hand1_src, hand2_src):
hand1 = hand1_src[0]
hand2 = hand2_src[0]
hand1_type = type_value(hand1)
hand2_type = type_value(hand2)
if hand1_type != hand2_type: return hand2_type - hand1_type
# if there's a tie, check each card in sequence
# print(hand1, hand2)
for i in range(len(hand1)):
if hand1[i][0] != hand2[i][0]:
return cards.index(hand2[i][0]) - cards.index(hand1[i][0])
# sort the array using the compare function
hands.sort(key=functools.cmp_to_key(compare))
hands.reverse()
# print(hands)
valued_hands = [(hand[0], hand[1], hand[1] * (i + 1), type_value(hand[0])) for i, hand in enumerate(hands)]
print(valued_hands)
value = 0
for i in range(len(hands)):
value += hands[i][1] * (i + 1)
print(value)

53
2023/7/part2.py Normal file
View file

@ -0,0 +1,53 @@
import functools
cards = ["A", "K", "Q", "T", "9", "8", "7", "6", "5", "4", "3", "2", "J"]
cards.reverse()
hands = [(line.split(" ")[0], int(line.split(" ")[1])) for line in open("./input2")]
def type_value(hand):
hand_count = {}
for card in hand:
if card[0] in hand_count:
hand_count[card[0]] += 1
else:
hand_count[card[0]] = 1
if "J" in hand_count and hand_count["J"] != 5:
j_value = hand_count["J"]
hand_count["J"] = 0
# get the highest card and add j_value to it
highest_card = list(hand_count.keys())[list(hand_count.values()).index(max(hand_count.values()))]
hand_count[highest_card] += j_value
hand_max = max(hand_count.values())
if hand_max >= 4: return hand_max + 2
if hand_max == 3 and 2 in hand_count.values(): return 5
if hand_max == 3: return 4
if hand_max == 2 and list(hand_count.values()).count(2) == 2: return 3
return hand_max
def compare(hand1_src, hand2_src):
hand1 = hand1_src[0]
hand2 = hand2_src[0]
hand1_type = type_value(hand1)
hand2_type = type_value(hand2)
if hand1_type != hand2_type: return hand2_type - hand1_type
# if there's a tie, check each card in sequence
# print(hand1, hand2)
for i in range(len(hand1)):
if hand1[i][0] != hand2[i][0]:
return cards.index(hand2[i][0]) - cards.index(hand1[i][0])
# sort the array using the compare function
hands.sort(key=functools.cmp_to_key(compare))
hands.reverse()
# print(hands)
valued_hands = [(hand[0], hand[1], hand[1] * (i + 1), type_value(hand[0])) for i, hand in enumerate(hands)]
print(valued_hands)
value = 0
for i in range(len(hands)):
value += hands[i][1] * (i + 1)
print(value)