diff options
author | June McEnroe <june@causal.agency> | 2022-12-12 11:28:41 -0500 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2022-12-12 11:28:41 -0500 |
commit | da79251be7bc57cc901eafdf4bb7f473c14b1fd8 (patch) | |
tree | 024be6f408d38bd558bb87299095622bb42d2843 | |
parent | Solve day 11 part 2 (diff) | |
download | aoc-da79251be7bc57cc901eafdf4bb7f473c14b1fd8.tar.gz aoc-da79251be7bc57cc901eafdf4bb7f473c14b1fd8.zip |
Solve day 12 part 1
-rw-r--r-- | 2022/day12.awk | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/2022/day12.awk b/2022/day12.awk new file mode 100644 index 0000000..265fad6 --- /dev/null +++ b/2022/day12.awk @@ -0,0 +1,58 @@ +BEGIN { + FS = ""; + y = 1; +} +function elev(a) { + return index("abcdefghijklmnopqrstuvwxyz", a); +} +{ + for (x = 1; x <= NF; ++x) { + if ($x == "S") { + S = x "," y; + m[x,y] = elev("a"); + } else if ($x == "E") { + E = x "," y; + m[x,y] = elev("z"); + } else { + m[x,y] = elev($x); + } + } + y++; + w = NF; + h = NR; +} +END { + l = 1; + Q[l] = S; + N[l] = 0; + for (i = 1; Q[i]; ++i) { + if (Q[i] == E) break; + split(Q[i], a, ","); + x = a[1]; + y = a[2]; + n = N[i]; + 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]; +} |