From ee4d2992b7e4e7b8a56afc1a5d24ba46bca58be9 Mon Sep 17 00:00:00 2001 From: Alexey Rykhalskiy Date: Sat, 25 May 2024 10:36:09 +0300 Subject: [PATCH] -- layers --- sandbox/src/main/scala/snake/Snake.scala | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/sandbox/src/main/scala/snake/Snake.scala b/sandbox/src/main/scala/snake/Snake.scala index 3b3a92bd..c62103af 100644 --- a/sandbox/src/main/scala/snake/Snake.scala +++ b/sandbox/src/main/scala/snake/Snake.scala @@ -22,6 +22,16 @@ object Snake { if (y == 0) next + x else snake(h - 1, w, y - 1, w - x - 1, next + w) + def layers2(w: Int, h: Int, x: Int, y: Int) = { + val layer = x min y min (w - (x + 1)) min (h - (y + 1)) + + if (layer == y) 2 * layer * (w + h - 2 * layer - 1) + (x - layer + 1) // top triangle with diagonals + else if (layer == w - (x + 1)) 2 * layer * (w + h - 2 * layer - 1) + (w - 2 * layer - 1 + y - layer + 1) // right + else if (layer == h - (y + 1)) 2 * layer * (w + h - 2 * layer - 1) + (w + h - 4 * layer - 3 + w - 1 - layer - x + 1) // bottom + else if (layer == x) 2 * layer * (w + h - 2 * layer - 1) + (2 * (w + h - 4 * layer - 2) + h - 1 - layer - y + 1) // left + else ??? + } + def layers(w: Int, h: Int, x: Int, y: Int) = { val layer = x min y min (w - 1 - x) min (h - 1 - y) if (y == layer)