72 lines
1.4 KiB
MySQL
72 lines
1.4 KiB
MySQL
|
|
with recursive
|
||
|
|
|
||
|
|
grid (line, row) as (
|
||
|
|
select
|
||
|
|
unnest(line),
|
||
|
|
generate_subscripts(line, 1) as row
|
||
|
|
from (
|
||
|
|
select string_split(trim(content, e'\n'), e'\n') as line
|
||
|
|
from read_text('data/07.in')
|
||
|
|
)
|
||
|
|
),
|
||
|
|
|
||
|
|
dims as (
|
||
|
|
select
|
||
|
|
max(row) as height,
|
||
|
|
max(length(line)) as width
|
||
|
|
from grid
|
||
|
|
),
|
||
|
|
|
||
|
|
initial as (
|
||
|
|
select col from range(1, (select width from dims)) r (col), grid
|
||
|
|
where row = 1 and line[col] <> '.'
|
||
|
|
),
|
||
|
|
|
||
|
|
trace (depth, col, num) as (
|
||
|
|
select
|
||
|
|
1,
|
||
|
|
col,
|
||
|
|
cast(1 as int128)
|
||
|
|
from initial
|
||
|
|
|
||
|
|
union
|
||
|
|
|
||
|
|
select
|
||
|
|
depth,
|
||
|
|
col,
|
||
|
|
sum(num)
|
||
|
|
from
|
||
|
|
(
|
||
|
|
select
|
||
|
|
depth + 1 as depth,
|
||
|
|
unnest(
|
||
|
|
case
|
||
|
|
when line[col] = '^'
|
||
|
|
then [col - 1, col + 1]
|
||
|
|
else [col]
|
||
|
|
end
|
||
|
|
) as col,
|
||
|
|
num
|
||
|
|
from
|
||
|
|
trace, grid
|
||
|
|
where
|
||
|
|
depth + 1 = row
|
||
|
|
)
|
||
|
|
group by depth, col
|
||
|
|
),
|
||
|
|
|
||
|
|
part1 (solution) as (
|
||
|
|
select count(*) from trace, grid
|
||
|
|
where line[col] = '^' and depth + 1 = row
|
||
|
|
),
|
||
|
|
|
||
|
|
part2 (solution) as (
|
||
|
|
select sum(num) from trace
|
||
|
|
where depth = (select height from dims)
|
||
|
|
)
|
||
|
|
|
||
|
|
select
|
||
|
|
part1.solution as p1,
|
||
|
|
part2.solution as p2
|
||
|
|
from part1, part2;
|