From 4f0bb121647e9450c3ccfd51c8de0883e4aa75ef Mon Sep 17 00:00:00 2001 From: FrederikBaerentsen Date: Mon, 4 Dec 2023 12:52:13 +0100 Subject: [PATCH] Finished 2023-12-04 part 2. had to get help --- 2023/day4/exp | 3 ++ 2023/day4/part2.py | 86 +++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 84 insertions(+), 5 deletions(-) create mode 100644 2023/day4/exp diff --git a/2023/day4/exp b/2023/day4/exp new file mode 100644 index 0000000..8ba0693 --- /dev/null +++ b/2023/day4/exp @@ -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 diff --git a/2023/day4/part2.py b/2023/day4/part2.py index 3eb7fbe..f61a303 100644 --- a/2023/day4/part2.py +++ b/2023/day4/part2.py @@ -20,6 +20,58 @@ def calc_double(x): tmp = tmp*2 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: for line in file: tmp = line.replace(':','|').rstrip() @@ -29,10 +81,34 @@ with open(input_f) as file: cards.append([]) for jdx,j in enumerate(i): cards[idx].append(j.split()) + + for xdx,x in enumerate(cards): + val[xdx] += 1 + print("====Original====") + #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 + """ - for x in cards: - match = list(set(x[1]) & set(x[2])) - if len(match) != 0: - result = result + int(calc_double(match)) + + + + print(sum(val.values())) + #print(find_match(x)) -print(result)