Upload files to "/"
This commit is contained in:
parent
0eda976ed3
commit
53553d40af
185
day7-1.py
Normal file
185
day7-1.py
Normal file
@ -0,0 +1,185 @@
|
||||
from collections import Counter
|
||||
|
||||
with open("input_day7.txt") as file:
|
||||
lines = [line.rstrip() for line in file]
|
||||
|
||||
hands = [line.split()[0] for line in lines]
|
||||
bids = [line.split()[1] for line in lines]
|
||||
|
||||
length = len(lines)
|
||||
def convert_to_int(input:str) -> int:
|
||||
match input:
|
||||
case '0':
|
||||
return 0
|
||||
case '1':
|
||||
return 1
|
||||
case '2':
|
||||
return 2
|
||||
case '3':
|
||||
return 3
|
||||
case '4':
|
||||
return 4
|
||||
case '5':
|
||||
return 5
|
||||
case '6':
|
||||
return 6
|
||||
case '7':
|
||||
return 7
|
||||
case '8':
|
||||
return 8
|
||||
case '9':
|
||||
return 9
|
||||
case 'A':
|
||||
return 14
|
||||
case 'K':
|
||||
return 13
|
||||
case 'Q':
|
||||
return 12
|
||||
case 'J':
|
||||
return 11
|
||||
case 'T':
|
||||
return 10
|
||||
|
||||
class element:
|
||||
def __init__(self,hand,bid,value,highest_count):
|
||||
self.hand = [convert_to_int(i) for i in list(hand)]
|
||||
self.bid = bid
|
||||
self.rank = 0
|
||||
self.value = [convert_to_int(i) for i in list(value)]
|
||||
self.highest_count = highest_count
|
||||
|
||||
|
||||
def count_runs(hand:str):
|
||||
lst = Counter(hand).most_common()
|
||||
highest_count = max([i[1] for i in lst])
|
||||
values = [i[0] for i in lst if i[1] == highest_count]
|
||||
#print(f"{values} highest_count: {highest_count}")
|
||||
return values, highest_count
|
||||
|
||||
def determine_initial_rank(e:element):
|
||||
if e.highest_count == 5:
|
||||
#five of kind
|
||||
return 6
|
||||
if e.highest_count == 4:
|
||||
#four of kind
|
||||
return 5
|
||||
if e.highest_count == 3:
|
||||
#full house (3 of kind + 2 of kind)
|
||||
#print(e.value[0])
|
||||
other = [a for a in e.hand if a != e.value[0]]
|
||||
#print(f"a{other[0]} {other[1]}")
|
||||
if other[0] == other[1]:
|
||||
return 4
|
||||
else:
|
||||
#three of kind
|
||||
return 3
|
||||
if e.highest_count == 2:
|
||||
#two pair
|
||||
if len(e.value) == 2:
|
||||
return 2
|
||||
else:
|
||||
# one pair
|
||||
return 1
|
||||
return 0
|
||||
|
||||
|
||||
|
||||
rank = []
|
||||
for i in range(length):
|
||||
value, highest_count = count_runs(hands[i])
|
||||
e = element(hands[i],bids[i],value,highest_count)
|
||||
e.rank = determine_initial_rank(e)
|
||||
rank.append(e)
|
||||
#print(e.rank)
|
||||
|
||||
|
||||
|
||||
def bubbleSort(arr:element):
|
||||
n = len(arr)
|
||||
|
||||
# Traverse through all array elements
|
||||
for i in range(n):
|
||||
swapped = False
|
||||
|
||||
# Last i elements are already in place
|
||||
for j in range(0, n-i-1):
|
||||
|
||||
# Traverse the array from 0 to n-i-1
|
||||
# Swap if the element found is greater
|
||||
# than the next element
|
||||
if arr[j].rank > arr[j+1].rank:
|
||||
arr[j], arr[j+1] = arr[j+1], arr[j]
|
||||
swapped = True
|
||||
elif arr[j].rank == arr[j+1].rank:
|
||||
#print("comp")
|
||||
#print(f"MATCH {arr[j].rank}")
|
||||
#ranks are same then do ordering
|
||||
for b in range(5):
|
||||
#print(f"{arr[j].hand[b]} {arr[j+1].hand[b]}")
|
||||
if arr[j].hand[b] > arr[j+1].hand[b]:
|
||||
arr[j], arr[j+1] = arr[j+1], arr[j]
|
||||
swapped = True
|
||||
#print("swap")
|
||||
break
|
||||
|
||||
if (swapped == False):
|
||||
break
|
||||
|
||||
def otherBubbleSort(arr:element):
|
||||
n = len(arr)
|
||||
|
||||
# Traverse through all array elements
|
||||
for i in range(n):
|
||||
|
||||
swapped = False
|
||||
for j in range(0, n-i-1):
|
||||
if arr[j].rank == arr[j+1].rank:
|
||||
#print("comp")
|
||||
#print(f"MATCH {arr[j].rank}")
|
||||
#ranks are same then do ordering
|
||||
for b in range(5):
|
||||
|
||||
if arr[j].hand[b] > arr[j+1].hand[b]:
|
||||
print(f"{arr[j].hand[b]} {arr[j+1].hand[b]}")
|
||||
arr[j], arr[j+1] = arr[j+1], arr[j]
|
||||
swapped = True
|
||||
#print("swap")
|
||||
break
|
||||
elif arr[j].hand[b] < arr[j+1].hand[b]:
|
||||
break
|
||||
# 2 2100
|
||||
# 3 1200
|
||||
|
||||
if (swapped == False):
|
||||
break
|
||||
|
||||
|
||||
bubbleSort(rank)
|
||||
|
||||
|
||||
def get_result():
|
||||
result = 0
|
||||
for e in range(len(lines)):
|
||||
result += int(rank[e].bid)*(e+1)
|
||||
print(result)
|
||||
return result
|
||||
|
||||
#bubbleSort(rank)
|
||||
otherBubbleSort(rank)
|
||||
|
||||
otherBubbleSort(rank)
|
||||
otherBubbleSort(rank)
|
||||
#otherBubbleSort(rank)
|
||||
last_result = 0
|
||||
#result = 1
|
||||
#while(last_result != result):
|
||||
# otherBubbleSort(rank)
|
||||
# get_result()
|
||||
|
||||
|
||||
for e in rank:
|
||||
print(f"{e.rank} {''.join(str(e.hand))} {e.bid}")
|
||||
|
||||
|
||||
|
||||
get_result()
|
1000
input_day7.txt
Normal file
1000
input_day7.txt
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user