day6, from N^3 to N^2
This commit is contained in:
parent
b360d60a05
commit
071e5aa8b6
12
d06/exec.awk
12
d06/exec.awk
|
@ -1,17 +1,17 @@
|
|||
BEGIN { W = 14 }
|
||||
{
|
||||
for (i=1; i<=length($1); i++) {
|
||||
{ # use -vW=4 or 14
|
||||
for (i=1; ; i++) {
|
||||
window = substr($1,i,W)
|
||||
# compare each char of the window to rest of the window
|
||||
S = 0 # to count match
|
||||
for (j=1; j<=W; j++) {
|
||||
for (j=1; j<W; j++) {
|
||||
for (k=j+1; k<=W; k++) {
|
||||
N++
|
||||
S+= (substr(window,j,1) == substr(window,k,1)) ? 1 : 0
|
||||
}
|
||||
}
|
||||
if (S == 0) {
|
||||
print i + W - 1
|
||||
print i + W - 1, "(" N " loops)"
|
||||
next
|
||||
}
|
||||
}
|
||||
}
|
||||
} # O(N^3)
|
|
@ -0,0 +1,17 @@
|
|||
{ # use -vW=4 or 14
|
||||
for (i=1; ; i++) {
|
||||
c = substr($0,i,1);
|
||||
# look backward for a "new j" > j
|
||||
for (k=i-1; k>j; k--) {
|
||||
N++
|
||||
if (substr($0,k,1) == c) {
|
||||
j = k
|
||||
break
|
||||
}
|
||||
}
|
||||
if ((i-j) == W) {
|
||||
print i, "(" N " loops)"
|
||||
next
|
||||
}
|
||||
}
|
||||
} # O(N^2)
|
Loading…
Reference in New Issue