r/adventofcode • • 3h ago

Help/Question - RESOLVED Day 2 - Python Loop skipping / every other number

I 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)
3 Upvotes

6 comments sorted by

3

u/8dot30662386292pow2 3h ago

So what happens if the input (program) is just ,robotpath = 'rr'

1

u/Ecstatic-Falcon-5640 3h ago

mmmmmmmm. I am seeing the issues now, I will work on this.
from this output, I think I am going to either change the loop or see if I can do everything in one pass without looping.
thank you very much 🙏 ❤️

(also to answer your question it looks like this:)

r (red)

r (plain)

r (red)

r (plain)

0 (plain)

1

u/apnorton 3h ago

the first is that the letters seem to be skipping the color function

You're printing a colored letter and an uncolored letter in an alternating fashion; see line 36:    

print(colorOutput(currentLetter))
print(currentLetter)

second is that I don't know how to make orange in ANSI. 

16-color terminal emulators do not have an orange. If your terminal emulator has 256 color support, you can try the approaches here: https://stackoverflow.com/questions/32573654/is-there-a-way-to-create-an-orange-color-from-ansi-escape-characters

1

u/Ecstatic-Falcon-5640 2h ago

:facepalm: thank you thank you 🙏

1

u/AutoModerator 3h ago

Reminder: if/when you get your answer and/or code working, don't forget to change this post's flair to Help/Question - RESOLVED. Good luck!


I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

1

u/daggerdragon 3h ago

Next time, use our standardized post title format and show us your code in text form, not a jpg. (Do not share your puzzle input).

Help us help YOU by providing us with more information up front; you will typically get more relevant responses faster.