Finished 2023-12-04 part 2. had to get help

This commit is contained in:
FrederikBaerentsen 2023-12-04 12:52:13 +01:00
parent e0a188f7c8
commit 4f0bb12164
2 changed files with 84 additions and 5 deletions

3
2023/day4/exp Normal file
View File

@ -0,0 +1,3 @@
Card 1: 41 48 83 86 17 | 83 86 6 31 17 9 48 53
Card 2: 13 32 20 16 61 | 61 30 68 82 17 32 24 19
Card 3: 1 21 53 59 44 | 69 82 63 72 16 21 14 1

View File

@ -20,6 +20,58 @@ def calc_double(x):
tmp = tmp*2 tmp = tmp*2
return tmp return tmp
def find_match(x):
return len(list(set(x[1]) & set(x[2])))
def total_cards_won(x):
input()
new = []
print(x)
global result
matches = find_match(x)
result += matches
#print(x[0][1])
for i in range(int(x[0][1]),matches+int(x[0][1])):
if i < len(cards):
new.append(cards[i])
else:
print('error')
print("Won the following cards (" + str(matches) + " matches) ")
#pprint(new)
#print("getting cards in range: " + str(x[0][1]) + ' to ' + str(int(x[0][1]) + matches))
#print("result: "+ str(result))
pprint(new)
for i in new:
return total_cards_won(i)
"""
for i in new:
print('printing card nr ' + str(i+1) + " at spot " + str(i))
if (i+1) >= len(cards):
return
print(cards[i])
#print(cards[i+int(x[0][1])][0][1])
tmp = find_match(cards[i])
print(i+tmp,len(cards))
print("^ card has " + str(tmp) + " matches")
if tmp != 0:
#card_nr = int(cards[i][0][1])
print(len(cards))
#if 1 + tmp + i >= len(cards):
# print('error')
# return
#else
print("^^ has matches in range " + str(i+1) + " to " + str(i+tmp+1))
for j in range(i+1, i+ tmp + 1):
print(cards[j])
return total_cards_won(cards[j])
else:
return
"""
from collections import defaultdict
val = defaultdict(int)
with open(input_f) as file: with open(input_f) as file:
for line in file: for line in file:
tmp = line.replace(':','|').rstrip() tmp = line.replace(':','|').rstrip()
@ -30,9 +82,33 @@ with open(input_f) as file:
for jdx,j in enumerate(i): for jdx,j in enumerate(i):
cards[idx].append(j.split()) cards[idx].append(j.split())
for x in cards: for xdx,x in enumerate(cards):
match = list(set(x[1]) & set(x[2])) val[xdx] += 1
if len(match) != 0: print("====Original====")
result = result + int(calc_double(match)) #tmp = find_match(x)
#print("Found " + str(tmp) + " matches in ",end="")
#print(cards[xdx][0])
#print(">>")
#result += 1
#if tmp != 0:
#total_cards_won(x)
#print("index " + str(xdx))
for i in range(find_match(x)):
#print("match nr " + str(i) + " in " + str(xdx))
#print(val[xdx])
#print(val)
val[xdx+i+1] += val[xdx]
"""
for y in range(0,tmp):
if (xdx + int(cards[xdx][0][1])+y) < len(cards):
print(cards[xdx+int(cards[xdx][0][1])+y])
result += 1
"""
print(sum(val.values()))
#print(find_match(x))
print(result)