with recursive banks as (
    select unnest(split(content, e'\n')) as bank from read_text('data/03.in')
),

banks_1 as (
    select
        (select arg_max(i, bank[i]) from range(1, len(bank)) r (i)) as i1,
        bank
    from banks
),

banks_2 as (
    select
        i1,
        (select arg_max(i, bank[i]) from range(i1 + 1, len(bank) + 1) r (i))
            as i2,
        bank
    from banks_1
),

banks_12 as (
    select
        12 as i,
        bank,
        0 as r
    from banks
    union all
    select
        i - 1,
        bank,
        (
            select arg_max(j, bank[j])
            from
                range(
                    r + 1,
                    len(bank) - i + 2
                ) m (j)
        )
    from banks_12
    where i > 0
),

part1 as (
    select
        sum(
            cast(bank[i1] as int64) * 10
            + cast(bank[i2] as int64)
        ) as solution
    from banks_2
),

part2 as (
    select sum(cast(10 ^ i as int64) * cast(bank[r] as int64)) as solution
    from banks_12
    where i < 12
)

select
    (select solution from part1) as p1,
    (select solution from part2) as p2;
