Георги обнови решението на 22.01.2014 21:51 (преди над 4 години)
+package main
+
+import (
+ "time"
+ )
+
+type inhabitant struct {
+ mall *[4][4]rune
+
+ isMoving bool
+
+ position [2]int
+ moves int
+}
+
+func inhabit(mall *[4][4]rune) []inhabitant {
+ var inhabitants []inhabitant
+ for i := 0; i < 4; i++ {
+ for j := 0; j < 4; j++ {
+ if mall[i][j] == 'X' {
+ inhabitants = append(inhabitants, inhabitant{mall: mall, isMoving: true, position: [2]int{i, j}, moves: 0})
+ }
+ }
+ }
+
+ return inhabitants
+}
+
+func (i *inhabitant) checkForMove() [2]int {
+ //reached 100 moves
+ if i.moves == 100 {
+ i.isMoving = false
+ return [2]int{-1, -1}
+ }
+
+ //upper left
+ if i.mall[(i.position[0]+3)%4][(i.position[1]+3)%4] != 'X' {
+ return [2]int{(i.position[0] + 3) % 4, (i.position[1] + 3) % 4}
+ }
+
+ //lower right
+ if i.mall[(i.position[0]+5)%4][(i.position[1]+5)%4] != 'X' {
+ return [2]int{(i.position[0] + 5) % 4, (i.position[1] + 5) % 4}
+ }
+
+ //lowwer left
+ if i.mall[(i.position[0]+3)%4][(i.position[1]+5)%4] != 'X' {
+ return [2]int{(i.position[0] + 3) % 4, (i.position[1] + 5) % 4}
+ }
+
+ //upper right
+ if i.mall[(i.position[0]+5)%4][(i.position[1]+3)%4] != 'X' {
+ return [2]int{(i.position[0] + 5) % 4, (i.position[1] + 3) % 4}
+ }
+
+ //no move available
+ i.isMoving = false
+ return [2]int{-2, -2}
+}
+
+func (i *inhabitant) move() [2][2]int {
+
+ newPosition := i.checkForMove()
+
+ if !i.isMoving {
+ return [2][2]int{i.position, newPosition}
+ }
+ i.mall[i.position[0]][i.position[1]] = '-'
+ i.mall[newPosition[0]][newPosition[1]] = 'X'
+ result := [2][2]int{i.position, newPosition}
+ i.position = newPosition
+ i.moves++
+ return result
+}
+
+func playMall(mall [4][4]rune) [][2][2]int {
+ inhabitants := inhabit(&mall)
+ var log [][2][2]int
+
+ for i := 0; i < len(inhabitants); i++ {
+ go func (n int) {
+ for inhabitants[n].isMoving {
+ log = append(log, inhabitants[n].move())
+ }
+ }(i)
+ }
+
+ time.Sleep(100)
+
+ return log
+}