aboutsummaryrefslogtreecommitdiff
path: root/2017/03/puzzle-2.awk
diff options
context:
space:
mode:
authorThomas Voss <thomasvoss@live.com> 2021-12-05 10:37:21 +0100
committerThomas Voss <thomasvoss@live.com> 2021-12-05 10:37:21 +0100
commit2e28981207430301b54d4d74ee620d0f159f4d8b (patch)
tree5f6af101fd4ee09b16f6f9fdc9e9f002c69bf6ed /2017/03/puzzle-2.awk
parent29fce7fa2a01fe00543e317514e505b4b6524c2b (diff)
Add day 3 solutions
Diffstat (limited to '2017/03/puzzle-2.awk')
-rwxr-xr-x2017/03/puzzle-2.awk38
1 files changed, 38 insertions, 0 deletions
diff --git a/2017/03/puzzle-2.awk b/2017/03/puzzle-2.awk
new file mode 100755
index 0000000..0758658
--- /dev/null
+++ b/2017/03/puzzle-2.awk
@@ -0,0 +1,38 @@
+#!/usr/bin/env -S awk -f
+
+function move(xi, yi)
+{
+ for (k = 0; k < j; k++) {
+ if (xi)
+ x += xi
+ else
+ y += yi
+
+ spiral[y][x] = spiral[y][x - 1] + spiral[y][x + 1] + spiral[y + 1][x + 1] \
+ + spiral[y + 1][x] + spiral[y + 1][x - 1] + spiral[y - 1][x + 1] \
+ + spiral[y - 1][x] + spiral[y - 1][x - 1]
+ if (spiral[y][x] > n)
+ return spiral[y][x]
+ }
+ return 0
+}
+
+{ n = $1 }
+
+END {
+ spiral[0][0] = 1
+ x = y = 0
+ j = 1
+ for (i = 1; 1; i++) {
+ if (i % 2)
+ if (r = move(j % 2 ? 1 : -1, 0))
+ break
+ else {
+ if (r = move(0, j % 2 ? 1 : -1))
+ break
+ j++
+ }
+ }
+
+ print r
+}