From 2e28981207430301b54d4d74ee620d0f159f4d8b Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sun, 5 Dec 2021 10:37:21 +0100 Subject: Add day 3 solutions --- 2017/03/puzzle-2.awk | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100755 2017/03/puzzle-2.awk (limited to '2017/03/puzzle-2.awk') 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 +} -- cgit v1.2.3