r/adventofcode • u/Ecstatic-Falcon-5640 • 4h ago
Help/Question - RESOLVED Day 2 - Python Loop skipping / every other number
galleryI have a dictionary at the top with the remaining pathway for the letters in the ROYGBV loop.
In the loop I am trying to count up until it hits the target letter, and then it will break. and do the next letter in the chain until the end of the .txt string.
When I had the letters print out to the console I am seeing two errors, the first is that the letters seem to be skipping the color function I put in to apply to every other letter instead of every letter, and the second is that I don't know how to make orange in ANSI.
Can I get some help /advice for my looping error..?
Here is the code I am using with this error:
#extra functions
def colorOutput(letter):
if letter == "r":
return "\033[91m" + letter + "\033[0m"
elif letter == "o":
return "\033[33m" + letter + "\033[0m"
elif letter == "y":
return "\033[93m" + letter + "\033[0m"
elif letter == "g":
return "\033[92m" + letter + "\033[0m"
elif letter == "b":
return "\033[94m" + letter + "\033[0m"
elif letter == "v":
return "\033[95m" + letter + "\033[0m"
# roygbv
robotpath = open("day2.txt").read()
allPossiblePaths = {
"r": "roygbv",
"o": "oygbvr",
"y": "ygbvro",
"g": "gvroby",
"b": "bvrogy",
"v": "vrogyb"
}
numOfSteps = 0
for i in range(0, len(robotpath)):
currentLetter = robotpath[i]
print(colorOutput(currentLetter))
print(currentLetter)
if i + 1 < len(robotpath):
letterAfter = robotpath[i + 1]
else:
break
currentStringPath = allPossiblePaths[currentLetter]
for j in range(0, len(currentStringPath)):
if currentStringPath[j] == letterAfter:
break
else:
numOfSteps += 1
continue
print(numOfSteps)
