mirror of
https://github.com/danbulant/adventOfCode
synced 2026-05-24 12:30:16 +00:00
day 7
This commit is contained in:
parent
748de41102
commit
7633eac086
6 changed files with 1118 additions and 4 deletions
|
|
@ -39,6 +39,10 @@ fn part2(file: &str) -> i32 {
|
||||||
solution
|
solution
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
dbg!(part2("input2"));
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
@ -67,7 +71,3 @@ mod tests {
|
||||||
assert_eq!(part2("input3"), 281)
|
assert_eq!(part2("input3"), 281)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
|
||||||
todo!();
|
|
||||||
}
|
|
||||||
5
2023/7/input1
Normal file
5
2023/7/input1
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
32T3K 765
|
||||||
|
T55J5 684
|
||||||
|
KK677 28
|
||||||
|
KTJJT 220
|
||||||
|
QQQJA 483
|
||||||
1000
2023/7/input2
Normal file
1000
2023/7/input2
Normal file
File diff suppressed because it is too large
Load diff
9
2023/7/inputt
Normal file
9
2023/7/inputt
Normal 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
47
2023/7/part1.py
Normal 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
53
2023/7/part2.py
Normal 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)
|
||||||
Loading…
Reference in a new issue