Skip to content

Instantly share code, notes, and snippets.

@itspacchu
Created September 23, 2024 15:08
Show Gist options
  • Save itspacchu/75913109d404b35c6d776fc81a3a1edc to your computer and use it in GitHub Desktop.
Save itspacchu/75913109d404b35c6d776fc81a3a1edc to your computer and use it in GitHub Desktop.
package main
import "fmt"
var MAT_SIZE int = 6
func PrintMatrix(mat [][]int, size int, iter int) {
fmt.Printf("------ [ %d ] ------\n", iter)
for x := 0; x < size; x++ {
for y := 0; y < size; y++ {
fmt.Printf("%d\t", mat[x][y])
}
fmt.Println()
}
}
func SafeElemSet(MAT [][]int, x int, y int, elem *int) {
fmt.Printf("(%d,%d) -> %d\n", x, y, *elem)
if y < MAT_SIZE && x < MAT_SIZE && x >= 0 && y >= 0 {
MAT[y][x] = *elem
*elem++
}
}
func main() {
fmt.Println("Enter size of matrix (int) : ")
fmt.Scanf("%d", &MAT_SIZE)
fmt.Printf("Weird matrix for %d size\n", MAT_SIZE)
fmt.Printf("Moves needed %d\n", 2*MAT_SIZE-1)
MAT := make([][]int, MAT_SIZE)
for i := range MAT {
MAT[i] = make([]int, MAT_SIZE)
}
x, y, elemID, flip := 0, 0, 1, false
genMoveArray := make([]int, 0)
genFlipsArray := make([]int, 0)
// First box needs 3 Sides always
genMoveArray = append(genMoveArray, MAT_SIZE-1)
for i := MAT_SIZE - 1; i > 0; i-- {
for j := 0; j < 2; j++ {
genMoveArray = append(genMoveArray, i)
}
}
// Last element is always on X axis motion +1 for odd ; -1 for even
genMoveArray = append(genMoveArray, 1)
curSign := -1
for i := 0; i < len(genMoveArray); i++ {
if i%2 == 0 {
curSign *= -1
}
genFlipsArray = append(genFlipsArray, curSign)
}
fmt.Println(genMoveArray)
fmt.Println(genFlipsArray)
for index, l := range genMoveArray {
flip = !flip
fmt.Printf("[%d]->%d switch_axis\n", index, l)
if flip {
for i := 0; i < l; i++ {
SafeElemSet(MAT, x, y, &elemID)
x += genFlipsArray[index]
}
} else {
for j := 0; j < l; j++ {
SafeElemSet(MAT, x, y, &elemID)
y += genFlipsArray[index]
}
}
}
PrintMatrix(MAT, MAT_SIZE, 2*MAT_SIZE-1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment