AdventOfCode/2023/day4/part2.py

115 lines
2.8 KiB
Python

#!/bin/python3
import sys
from pprint import pprint
input_f = sys.argv[1]
arr = []
cards = []
result = 0
def calc_double(x):
tmp = 1
if len(x) <= 2:
return len(x)
for i in range(1,len(x)+1):
if i == 1:
tmp = 1
else:
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()
arr.append(tmp.split('|'))
for idx,i in enumerate(arr):
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
"""
print(sum(val.values()))
#print(find_match(x))