Явор обнови решението на 23.01.2014 07:32 (преди над 4 години)
+package main
+
+var globalMall [4][4]rune
+
+func fixPos(i int, j int) (i1 int, j1 int) {
+ i1 = i
+ j1 = j
+ if i1 < 0 {
+ i1 += 4
+ }
+ if j1 < 0 {
+ j1 += 4
+ }
+ if i1 >= 4 {
+ i1 -= 4
+ }
+ if j1 >= 4 {
+ j1 -= 4
+ }
+ return
+}
+
+func getMove(moves chan<- [2][2]int, pulse <-chan bool, x int, y int) {
+ var movesMade int
+ var currentMove [2][2]int
+ var nextMoveX, nextMoveY int
+ for movesMade = 0; <-pulse && movesMade < 100; movesMade++ {
+ if nextMoveX, nextMoveY = fixPos(x-1, y-1); globalMall[nextMoveX][nextMoveY] == '-' {
+ } else if nextMoveX, nextMoveY = fixPos(x+1, y+1); globalMall[nextMoveX][nextMoveY] == '-' {
+ } else if nextMoveX, nextMoveY = fixPos(x-1, y+1); globalMall[nextMoveX][nextMoveY] == '-' {
+ } else if nextMoveX, nextMoveY = fixPos(x+1, y-1); globalMall[nextMoveX][nextMoveY] == '-' {
+ } else {
+ break
+ }
+ currentMove[0][0] = x
+ currentMove[0][1] = y
+ currentMove[1][0] = nextMoveX
+ currentMove[1][1] = nextMoveY
+ moves <- currentMove
+ x = nextMoveX
+ y = nextMoveY
+ }
+ var finalMove int
+ if movesMade == 100 {
+ finalMove = -1
+ } else {
+ finalMove = -2
+ }
+ currentMove[0][0] = x
+ currentMove[0][1] = y
+ currentMove[1][0] = finalMove
+ currentMove[1][1] = finalMove
+ moves <- currentMove
+ return
+}
+
+func playMall(mall [4][4]rune) (result [][2][2]int) {
+ result = make([][2][2]int, 0)
+ var pulse chan bool = make(chan bool)
+ var moveChannel chan [2][2]int = make(chan [2][2]int)
+ var activeActors int
+ var newMove [2][2]int
+ globalMall = mall
+ for i := 0; i < 4; i++ {
+ for j := 0; j < 4; j++ {
+ if mall[i][j] == 'X' {
+ go getMove(moveChannel, pulse, i, j)
+ activeActors++
+ }
+ }
+ }
+ for activeActors > 0 {
+ pulse <- true
+ newMove = <-moveChannel
+ result = append(result, newMove)
+ if newMove[1][0] < 0 {
+ activeActors--
+ } else {
+ globalMall[newMove[0][0]][newMove[0][1]] = '-'
+ globalMall[newMove[1][0]][newMove[1][1]] = 'X'
+ }
+ }
+ return
+}