add v version.

This commit is contained in:
m-hgn 2023-05-01 23:49:29 +02:00
parent 2864ba7771
commit eb5f3b18b7
2 changed files with 56 additions and 7 deletions

View File

@ -1,14 +1,9 @@
bd = ./build/ bd = ./build/
all: c d nim odin __python rust all: c d nim odin __python rust v
build-run: all build-run: all
$(bd)c make run
$(bd)d
$(bd)nim
$(bd)odin
./snail.py
$(bd)rust
run: run:
$(bd)c $(bd)c
@ -17,6 +12,7 @@ run:
$(bd)odin $(bd)odin
./snail.py ./snail.py
$(bd)rust $(bd)rust
$(bd)v
clean: clean:
rm -f $(bd)* rm -f $(bd)*
@ -44,3 +40,7 @@ rust: snail.rs
rustc $^ -o $(bd)$@ rustc $^ -o $(bd)$@
$(bd)$@ $(bd)$@
v: snail.v
v $^ -o $(bd)$@
$(bd)$@

49
snail.v Normal file
View File

@ -0,0 +1,49 @@
fn (xss [][]int) snail() []int {
mut ys := []int{}
h := xss.len
if h == 0 { return ys }
w := xss[0].len
if w == 0 { return ys }
for b in 0 .. (max(h, w)) {
ex := w - b - 1
ey := h - b - 1
for c in b .. (ex + 1) { ys << xss[b][c] }
for r in (b + 1) .. (ey + 1) { ys << xss[r][ex] }
if ex == b || ey == b { return ys }
for c in (b + 1) .. (ex + 1) { ys << xss[ey][ex - c] }
for r in (b + 1) .. ey { ys << xss[ey - r][b] }
}
return ys
}
fn max(a int, b int) int { if a > b { return a } else { return b } }
fn main() {
mut initial := [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
mut expected := [1, 2, 3, 6, 9, 8, 7, 4, 5]
assert initial.snail() == expected
println(initial.snail() == expected)
initial = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
expected = [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]
assert initial.snail() == expected
println(initial.snail() == expected)
initial = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9],
[10, 11, 12],
[13, 14, 15]
]
expected = [1, 2, 3, 6, 9, 12, 15, 14, 13, 10, 7, 4, 5, 8, 11]
assert initial.snail() == expected
println(initial.snail() == expected)
println('all tests passed')
}