-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patha5.sql
40 lines (31 loc) · 1 KB
/
a5.sql
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
create or replace database a5;
use a5;
create or replace table polymers (
polymer text not null
);
load data local infile '/home/setorgan/aoc2018/a5.txt'
into table polymers
;
with recursive reduction as (
select polymer from polymers
union
select regexp_replace(polymer, '(?i)([a-z])(?-i)(?!\\1)(?i)\\1', '')
from reduction
) select length(polymer) from reduction order by length(polymer) limit 1;
create table units (
unit char(1)
);
insert into units values
('a'), ('b'),('c'),('d'),('e'),('f'),('g'),('h'),('i'),('j'),('k'),
('l'),('m'),('n'),('o'),('p'),('q'),('r'),('s'),('t'),('u'),('v'),
('w'),('x'),('y'),('z');
create table modifieds as
select u.unit, regexp_replace(p.polymer, concat('(?i)', u.unit), '') polymer
from units u join polymers p;
with recursive reduction as (
select unit, polymer from modifieds
union
select unit, regexp_replace(polymer, '(?i)([a-z])(?-i)(?!\\1)(?i)\\1', '')
from reduction
) select unit, length(polymer) from reduction order by length(polymer) limit 1;
drop database a5;