add: part2
This commit is contained in:
		
							
								
								
									
										96
									
								
								2023/day07/src/part2.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										96
									
								
								2023/day07/src/part2.py
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,96 @@
 | 
			
		||||
text: str = open("input.txt", "r").read()
 | 
			
		||||
 | 
			
		||||
_value: int = 0
 | 
			
		||||
 | 
			
		||||
lines = text.splitlines()
 | 
			
		||||
 | 
			
		||||
CARDS = "AKQT98765432J"
 | 
			
		||||
 | 
			
		||||
def get_best_replace(string: str):
 | 
			
		||||
	if ("J" not in string):
 | 
			
		||||
		return string
 | 
			
		||||
	if (string == "JJJJJ"):
 | 
			
		||||
		return "AAAAA"
 | 
			
		||||
	tmp = string
 | 
			
		||||
	tmp = tmp.replace("J", "")
 | 
			
		||||
	bozo: list = list(set(tmp))
 | 
			
		||||
	bozo2: list = [(tmp.count(x), x) for x in tmp]
 | 
			
		||||
	max_occurence = [bozo2[0]]
 | 
			
		||||
	for occurence, char in bozo2[1:]:
 | 
			
		||||
		if (max_occurence[0] == occurence):
 | 
			
		||||
			max_occurence.append((occurence, char))
 | 
			
		||||
		elif (max_occurence[0][0] < occurence):
 | 
			
		||||
			max_occurence = [(occurence, char)]
 | 
			
		||||
	_max = max_occurence[0][1]
 | 
			
		||||
	for occurence, char in max_occurence[1:]:
 | 
			
		||||
		if (CARDS.index(_max) > CARDS.index(char)):
 | 
			
		||||
			_max = char
 | 
			
		||||
 | 
			
		||||
	string = string.replace("J", _max)
 | 
			
		||||
	return string
 | 
			
		||||
 | 
			
		||||
def parse_line(line: str):
 | 
			
		||||
	bozo = line.split()
 | 
			
		||||
	raw_card = bozo[0]
 | 
			
		||||
	bid = int(bozo[1])
 | 
			
		||||
	card = get_best_replace(raw_card)
 | 
			
		||||
	same = {"card": card, "raw_card": raw_card, "bid": bid}
 | 
			
		||||
	bozo = set(card)
 | 
			
		||||
	if (len(bozo) == 5):
 | 
			
		||||
		_type = 0
 | 
			
		||||
	elif (len(bozo) == 4):
 | 
			
		||||
		_type = 1
 | 
			
		||||
	elif (len(bozo) == 3 and (card.count(list(bozo)[0]) == 2 or card.count(list(bozo)[1]) == 2)):
 | 
			
		||||
		_type = 2
 | 
			
		||||
	elif (len(bozo) == 3):
 | 
			
		||||
		_type = 3
 | 
			
		||||
	elif (len(bozo) == 2 and (card.count(list(bozo)[0]) == 2 or card.count(list(bozo)[1]) == 2)):
 | 
			
		||||
		_type = 4
 | 
			
		||||
	elif (len(bozo) == 2):
 | 
			
		||||
		_type = 5
 | 
			
		||||
	elif (len(bozo) == 1):
 | 
			
		||||
		_type = 6
 | 
			
		||||
 | 
			
		||||
	same.update({"type": int(_type)})
 | 
			
		||||
	return same
 | 
			
		||||
 | 
			
		||||
def parse(lines):
 | 
			
		||||
	hands = []
 | 
			
		||||
	for line in lines:
 | 
			
		||||
		hands.append(parse_line(line))
 | 
			
		||||
	return hands
 | 
			
		||||
 | 
			
		||||
def hand_cmp(hand1: dict, hand2: dict):
 | 
			
		||||
	#print(hand1, hand2)
 | 
			
		||||
	for card1, card2 in zip(hand1.get("raw_card"), hand2.get("raw_card")):
 | 
			
		||||
		if card1 == card2:
 | 
			
		||||
			continue
 | 
			
		||||
		return (CARDS.index(card1) - CARDS.index(card2))
 | 
			
		||||
	return 0
 | 
			
		||||
 | 
			
		||||
def get_insert_index(lst: [dict], hand: dict):
 | 
			
		||||
	index: int = None
 | 
			
		||||
	for i, hand2 in enumerate(lst):
 | 
			
		||||
		if (hand.get("type") == hand2.get("type")):
 | 
			
		||||
			if (hand_cmp(hand, hand2) < 0):
 | 
			
		||||
				return i
 | 
			
		||||
		elif (hand.get("type") > hand2.get("type")):
 | 
			
		||||
			return i
 | 
			
		||||
	return len(lst)
 | 
			
		||||
 | 
			
		||||
def sort_hands(hands: [dict]):
 | 
			
		||||
	sort = [hands[0]]
 | 
			
		||||
	for hand in hands[1:]:
 | 
			
		||||
		index = get_insert_index(sort, hand)
 | 
			
		||||
		sort.insert(index, hand)
 | 
			
		||||
	return sort
 | 
			
		||||
 | 
			
		||||
hands = parse(lines)
 | 
			
		||||
hands = sort_hands(hands)
 | 
			
		||||
 | 
			
		||||
for i, hand in enumerate(hands):
 | 
			
		||||
	_value += (len(hands) - i) * hand.get("bid")
 | 
			
		||||
	#print(f'{hand.get("card")} {hand.get("raw_card")} {hand.get("bid")}')
 | 
			
		||||
	#print(f'{len(hands) - i} {hand.get("card")} {hand.get("bid")} type={hand.get("type")}')
 | 
			
		||||
 | 
			
		||||
print(_value)
 | 
			
		||||
		Reference in New Issue
	
	Block a user