All write-ups

CTF Write-up · Coding

Count Words

Counting the words in a file whose embedded digits sum to exactly ten.

Source: EOman 2022 Category: Coding 40 points Easy

Challenge

The flag is the total number of words in the supplied file whose embedded digits add up to ten.

t5?@h3ro2usands has 5 + 3 + 2 = 10 (counted) o4?@f6r has 4 + 6 = 10 (counted) w1?@e2rr1e has 1 + 2 + 1 = 4 (not counted)

Analysis

Each token may interleave letters, symbols, and digits. Only the digit characters are relevant: they are extracted, summed, and compared against ten. A single pass over the file suffices.

Solution

def sum_digits_string(s):
    return sum(int(x) for x in s if x.isdigit())

with open("dump.txt") as file:
    count = 0
    for line in file:
        for word in line.split():
            if sum_digits_string(word) == 10:
                count += 1
    print(count)

The printed count is the flag.

← Back to all write-ups