Repository navigation
Expand file tree
/
Copy pathday04.sql
More file actions
54 lines (54 loc) · 2.53 KB
/
Copy pathday04.sql
File metadata and controls
54 lines (54 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
with recursive aoc4_input(i) as (select '
MMMSXXMASM
MSAMXMSMSA
AMXSXMAAMM
MSAMASMSMX
XMASAMXAMM
XXAMMXXAMA
SMSMSASXSS
SAXAMASAAA
MAMMMXMMMM
MXMXAXMASX
'),
lines(y,line) as (
select 0, substr(i,1,position(E'\n' in i)-1), substr(i,position(E'\n' in i)+1)
from aoc4_input
union all
select y+1,substr(r,1,position(E'\n' in r)-1), substr(r,position(E'\n' in r)+1)
from lines l(y,l,r) where position(E'\n' in r)>0
),
field(x,y,v) as (
select x,y,substr(line,x::integer,1) as v
from (select * from lines where line<>'') l, lateral generate_series(1,length(line)) g(x)
),
xfields(x,y) as (select x,y from field where v='X'),
mfields(x,y) as (select x,y from field where v='M'),
afields(x,y) as (select x,y from field where v='A'),
sfields(x,y) as (select x,y from field where v='S'),
msfields(x,y,v) as (select x,y,v from field where v in ('M','S')),
part1 as (
select 1 from xfields c1, mfields c2, afields c3, sfields c4 where c1.x+1=c2.x and c1.y=c2.y and c2.x+1=c3.x and c2.y=c3.y and c3.x+1=c4.x and c3.y=c4.y
union all
select 1 from sfields c1, afields c2, mfields c3, xfields c4 where c1.x+1=c2.x and c1.y=c2.y and c2.x+1=c3.x and c2.y=c3.y and c3.x+1=c4.x and c3.y=c4.y
union all
select 1 from xfields c1, mfields c2, afields c3, sfields c4 where c1.x=c2.x and c1.y+1=c2.y and c2.x=c3.x and c2.y+1=c3.y and c3.x=c4.x and c3.y+1=c4.y
union all
select 1 from sfields c1, afields c2, mfields c3, xfields c4 where c1.x=c2.x and c1.y+1=c2.y and c2.x=c3.x and c2.y+1=c3.y and c3.x=c4.x and c3.y+1=c4.y
union all
select 1 from xfields c1, mfields c2, afields c3, sfields c4 where c1.x+1=c2.x and c1.y+1=c2.y and c2.x+1=c3.x and c2.y+1=c3.y and c3.x+1=c4.x and c3.y+1=c4.y
union all
select 1 from sfields c1, afields c2, mfields c3, xfields c4 where c1.x+1=c2.x and c1.y+1=c2.y and c2.x+1=c3.x and c2.y+1=c3.y and c3.x+1=c4.x and c3.y+1=c4.y
union all
select 1 from xfields c1, mfields c2, afields c3, sfields c4 where c1.x+1=c2.x and c1.y-1=c2.y and c2.x+1=c3.x and c2.y-1=c3.y and c3.x+1=c4.x and c3.y-1=c4.y
union all
select 1 from sfields c1, afields c2, mfields c3, xfields c4 where c1.x+1=c2.x and c1.y-1=c2.y and c2.x+1=c3.x and c2.y-1=c3.y and c3.x+1=c4.x and c3.y-1=c4.y
),
part2 as (
select a.*
from afields a, msfields d1, msfields d2, msfields d3, msfields d4
where a.x-1=d1.x and a.y-1=d1.y and a.x+1=d2.x and a.y+1=d2.y
and a.x-1=d3.x and a.y+1=d3.y and a.x+1=d4.x and a.y-1=d4.y
and d1.v<>d2.v and d3.v<>d4.v
)
select (select count(*) from part1) as part1,
(select count(*) from part2) as part2