diff --git a/config.json b/config.json index 8a63c27..46f6d1d 100644 --- a/config.json +++ b/config.json @@ -495,6 +495,14 @@ "prerequisites": [], "difficulty": 4 }, + { + "slug": "spiral-matrix", + "name": "Spiral Matrix", + "uuid": "2fd887a4-bf2d-4cbe-8293-d195f7947f9e", + "practices": [], + "prerequisites": [], + "difficulty": 4 + }, { "slug": "armstrong-numbers", "name": "Armstrong Numbers", diff --git a/exercises/practice/spiral-matrix/.docs/instructions.md b/exercises/practice/spiral-matrix/.docs/instructions.md new file mode 100644 index 0000000..ba99e12 --- /dev/null +++ b/exercises/practice/spiral-matrix/.docs/instructions.md @@ -0,0 +1,24 @@ +# Instructions + +Given the size, return a square matrix of numbers in spiral order. + +The matrix should be filled with natural numbers, starting from 1 in the top-left corner, increasing in an inward, clockwise spiral order, like these examples: + +## Examples + +### Spiral matrix of size 3 + +```text +1 2 3 +8 9 4 +7 6 5 +``` + +### Spiral matrix of size 4 + +```text + 1 2 3 4 +12 13 14 5 +11 16 15 6 +10 9 8 7 +``` diff --git a/exercises/practice/spiral-matrix/.meta/config.json b/exercises/practice/spiral-matrix/.meta/config.json new file mode 100644 index 0000000..c1bfe2c --- /dev/null +++ b/exercises/practice/spiral-matrix/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "keiravillekode" + ], + "files": { + "solution": [ + "spiral-matrix.v" + ], + "test": [ + "run_test.v" + ], + "example": [ + ".meta/example.v" + ] + }, + "blurb": "Given the size, return a square matrix of numbers in spiral order.", + "source": "Reddit r/dailyprogrammer challenge #320 [Easy] Spiral Ascension.", + "source_url": "https://web.archive.org/web/20230607064729/https://old.reddit.com/r/dailyprogrammer/comments/6i60lr/20170619_challenge_320_easy_spiral_ascension/" +} diff --git a/exercises/practice/spiral-matrix/.meta/example.v b/exercises/practice/spiral-matrix/.meta/example.v new file mode 100644 index 0000000..221a9ab --- /dev/null +++ b/exercises/practice/spiral-matrix/.meta/example.v @@ -0,0 +1,36 @@ +module main + +fn spiral_matrix(size int) [][]int { + mut result := [][]int{len: size} + for i in 0 .. size { + result[i] = []int{len: size, init: size * size} + } + + mut value := 1 + mut row := 0 + mut column := 0 + mut side_length := size - 1 + for side_length >= 1 { + for _ in 0 .. side_length { + result[row][column] = value++ + column++ + } + for _ in 0 .. side_length { + result[row][column] = value++ + row++ + } + for _ in 0 .. side_length { + result[row][column] = value++ + column-- + } + for _ in 0 .. side_length { + result[row][column] = value++ + row-- + } + row++ + column++ + side_length -= 2 + } + + return result +} diff --git a/exercises/practice/spiral-matrix/.meta/tests.toml b/exercises/practice/spiral-matrix/.meta/tests.toml new file mode 100644 index 0000000..b1ea4be --- /dev/null +++ b/exercises/practice/spiral-matrix/.meta/tests.toml @@ -0,0 +1,21 @@ +# This is an auto-generated file. Regular comments will be removed when this +# file is regenerated. Regenerating will not touch any manually added keys, +# so comments can be added in a "comment" key. + +[8f584201-b446-4bc9-b132-811c8edd9040] +description = "empty spiral" + +[e40ae5f3-e2c9-4639-8116-8a119d632ab2] +description = "trivial spiral" + +[cf05e42d-eb78-4098-a36e-cdaf0991bc48] +description = "spiral of size 2" + +[1c475667-c896-4c23-82e2-e033929de939] +description = "spiral of size 3" + +[05ccbc48-d891-44f5-9137-f4ce462a759d] +description = "spiral of size 4" + +[f4d2165b-1738-4e0c-bed0-c459045ae50d] +description = "spiral of size 5" diff --git a/exercises/practice/spiral-matrix/run_test.v b/exercises/practice/spiral-matrix/run_test.v new file mode 100644 index 0000000..8486357 --- /dev/null +++ b/exercises/practice/spiral-matrix/run_test.v @@ -0,0 +1,51 @@ +module main + +fn test_empty_spiral() { + expect := [][]int{} + assert spiral_matrix(0) == expect +} + +fn test_trivial_spiral() { + expect := [ + [1], + ] + assert spiral_matrix(1) == expect +} + +fn test_spiral_of_size_2() { + expect := [ + [1, 2], + [4, 3], + ] + assert spiral_matrix(2) == expect +} + +fn test_spiral_of_size_3() { + expect := [ + [1, 2, 3], + [8, 9, 4], + [7, 6, 5], + ] + assert spiral_matrix(3) == expect +} + +fn test_spiral_of_size_4() { + expect := [ + [1, 2, 3, 4], + [12, 13, 14, 5], + [11, 16, 15, 6], + [10, 9, 8, 7], + ] + assert spiral_matrix(4) == expect +} + +fn test_spiral_of_size_5() { + expect := [ + [1, 2, 3, 4, 5], + [16, 17, 18, 19, 6], + [15, 24, 25, 20, 7], + [14, 23, 22, 21, 8], + [13, 12, 11, 10, 9], + ] + assert spiral_matrix(5) == expect +} diff --git a/exercises/practice/spiral-matrix/spiral-matrix.v b/exercises/practice/spiral-matrix/spiral-matrix.v new file mode 100644 index 0000000..f91cfe0 --- /dev/null +++ b/exercises/practice/spiral-matrix/spiral-matrix.v @@ -0,0 +1,4 @@ +module main + +fn spiral_matrix(size int) [][]int { +}