Beware, this challenge is quite tricky. If you have a (quite common) bug in your solution, then you will get something that looks like a valid flag, but is actually a little bit off from the correct flag.
Double and triple-check your solution for logic bugs, and you’ll discover the right flag.