May 08, 2026

Solution for Codyssi 2025 - Risky Shortcut

Link to the puzzle text

Part 1

In this puzzle we have a list of long strings consisting of alphanumerical characters with some hyphens (-). We are asked to count the alphabetica characters (without numbers and hypens) and return the number.

We are using a custom function to transform a single character to either a True or False value. The function just calls the predefined isalpha(). This functions is called using the map function on each character in the input. We then sum up the True values to get the answer.

Part 2

In part 2, we should shorten the input strings by a certain rule. Whenever a digit is next to a alphabetical character or a hyphen, both character can be removed from the string. This should be repeated until no such pairs are found in the input. We should then sum up the remaining lengths of the input strings as the answer.

We use regex to search for all occurences of the pattern. We use two regex for simplifying the pattern. The regex r"[a-z-]\d" is looking for a single alphabetical character or hyphen ([a-z-]) followed by a single digit (\d) on the right, while r"\d[a-z-]" is looking for the digit on the left. We then use the sub function to replace all found groups with a empty string, therefore removing the group. This is repeated until the string no longer changes:

prev = line
while True:
line = re.sub(right_pattern, "", line)
line = re.sub(left_pattern, "", line)
if prev == line:
return line
prev = line 

The length of the returned string is summed up for all the input strings for the answer.

Part 3

In part 3, we should shorten the input strings by a different rule. We are still looking for an alphabetical character next to a digit, but this time not hyphens are permitted. Again we should remove the pairs until no longer possible and sum up the remaining length for all inputs.

We reuse the same function from part 2, but with a different pattern. A input flag chooses whether we include the hyphen in the regex pattern or not. For this part, the patterns are r"[a-z]\d", so the pattern from part 2 without the hyphen. The rest of the code can be the same. 

Link to my solutions

No comments:

Post a Comment