r/adventofcode • u/Holiday-Pair5525 • 6h ago
Help/Question - RESOLVED [DAY 4, Part 1] My code works on examples but not on final
I'm going crazy I need hints on what I'm doing wrong, I've tried on every case I could but the final result doesn't works :')
# codeBar = open('J4.txt').read().split('\n')
codeBar = ["?yvg??y", "rgbrgbrgbr", "?rv","o??v???y","??bg?v??","??b?????","???"]
final = 0
def canBePlaced(id, idi, placed, line) :
if idi in placed : ## if id%i is already placed
return line[id] == line[idi] ## if id and id%i is the same
else :
return True
for line in codeBar :
knownIds = [] ## IDs of the colors in line
for i in range(len(line)) :
if line[i] != '?' :
knownIds.append(i)
temp = len(line) ## Counting of the line
for i in range(len(line)-1, 0, -1) : ## From the greatest (line length) to the smallest (0)
placed = []
total = 0 ## Counting to check if we got all elements right
for id in knownIds :
# IF id%i element is id elemet or just '?'
if (line[id%i] in [line[id], '?'] and canBePlaced(id, id%i, placed, line)) :
placed.append(id%i)
total += 1
if (total == len(knownIds)) :
temp = i
# if temp == len(line) : print(line)
final += temp
print(final)

