From 3ec99a8a109a9672e2867cb20d903d221549b677 Mon Sep 17 00:00:00 2001 From: setop Date: Sat, 13 Apr 2024 23:52:39 +0200 Subject: [PATCH] day6, from N^3 to N^2 --- d06/exec.awk | 12 ++++++------ d06/exec2.awk | 17 +++++++++++++++++ 2 files changed, 23 insertions(+), 6 deletions(-) create mode 100644 d06/exec2.awk diff --git a/d06/exec.awk b/d06/exec.awk index 2cd1cb7..4eeae56 100644 --- a/d06/exec.awk +++ b/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 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) \ No newline at end of file