day6, from N^3 to N^2

This commit is contained in:
setop 2024-04-13 23:52:39 +02:00
parent b360d60a05
commit 3ec99a8a10
2 changed files with 23 additions and 6 deletions

View File

@ -1,17 +1,17 @@
BEGIN { W = 14 } { # use -vW=4 or 14
{ for (i=1; ; i++) {
for (i=1; i<=length($1); i++) {
window = substr($1,i,W) window = substr($1,i,W)
# compare each char of the window to rest of the window # compare each char of the window to rest of the window
S = 0 # to count match S = 0 # to count match
for (j=1; j<=W; j++) { for (j=1; j<W; j++) {
for (k=j+1; k<=W; k++) { for (k=j+1; k<=W; k++) {
N++
S+= (substr(window,j,1) == substr(window,k,1)) ? 1 : 0 S+= (substr(window,j,1) == substr(window,k,1)) ? 1 : 0
} }
} }
if (S == 0) { if (S == 0) {
print i + W - 1 print i + W - 1, "(" N " loops)"
next next
} }
} }
} } # O(N*W^2)

17
d06/exec2.awk Normal file
View File

@ -0,0 +1,17 @@
{ j=0; # 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*W)