with
bounds as (
    select
        unnest(string_split(trim(content, '\n'), ',')) as f,
        string_split(f, '-') as bounds,
        cast(bounds[1] as int64) as low,
        cast(bounds[2] as int64) as high
    from read_text('data/02.in')
),

candidates as (
    select
        unnest(range(low, high + 1)) as num,
        cast(num as string) as num_str,
        len(num_str) as len
    from bounds
),

part1 as (
    select sum(num) as solution
    from candidates
    where num_str[: len // 2] = num_str[len // 2 + 1:]
),

part2 as (
    select sum(num) as solution
    from candidates
    where exists (
        select stride from range(1, len) as range (stride)
        where
            len % stride = 0
            and repeat(num_str[: stride], len // stride) = num_str
    )
)

select
    part1.solution as p1,
    part2.solution as p2
from part1, part2;
