day6, from N^3 to N^2
This commit is contained in:
		
							
								
								
									
										12
									
								
								d06/exec.awk
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								d06/exec.awk
									
									
									
									
									
								
							| @@ -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
									
								
							
							
						
						
									
										17
									
								
								d06/exec2.awk
									
									
									
									
									
										Normal 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) | ||||||
		Reference in New Issue
	
	Block a user