summary refs log tree commit diff homepage
diff options
context:
space:
mode:
-rw-r--r--2022/day12.awk36
1 files changed, 36 insertions, 0 deletions
diff --git a/2022/day12.awk b/2022/day12.awk
index 265fad6..314ce87 100644
--- a/2022/day12.awk
+++ b/2022/day12.awk
@@ -55,4 +55,40 @@ END {
 		delete N[i];
 	}
 	print N[i];
+	delete Q;
+	delete N;
+	delete V;
+	l = 1;
+	Q[l] = E;
+	N[l] = 0;
+	for (i = 1; Q[i]; ++i) {
+		split(Q[i], a, ",");
+		x = a[1];
+		y = a[2];
+		n = N[i];
+		if (m[x,y] == 1) break;
+		if (x > 1 && !V[x-1,y] && m[x-1,y] >= m[x,y]-1) {
+			V[x-1,y] = 1;
+			Q[++l] = x-1 "," y;
+			N[l] = n+1;
+		}
+		if (x < w && !V[x+1,y] && m[x+1,y] >= m[x,y]-1) {
+			V[x+1,y] = 1;
+			Q[++l] = x+1 "," y;
+			N[l] = n+1;
+		}
+		if (y > 1 && !V[x,y-1] && m[x,y-1] >= m[x,y]-1) {
+			V[x,y-1] = 1;
+			Q[++l] = x "," y-1;
+			N[l] = n+1;
+		}
+		if (y < h && !V[x,y+1] && m[x,y+1] >= m[x,y]-1) {
+			V[x,y+1] = 1;
+			Q[++l] = x "," y+1;
+			N[l] = n+1;
+		}
+		delete Q[i];
+		delete N[i];
+	}
+	print N[i];
 }