Решение на Ескалатори в мола от Мартин Ангелов

Обратно към всички решения

Към профила на Мартин Ангелов

Резултати

  • 10 точки от тестове
  • 0 бонус точки
  • 10 точки общо
  • 10 успешни тест(а)
  • 0 неуспешни тест(а)

Код

package main
import (
"sync"
)
type Game struct {
Mu sync.Mutex
Mall [4][4]rune
Moves [][2][2]int
}
func playMall(mall [4][4]rune) [][2][2]int {
var wg sync.WaitGroup
game := &Game{Mu: sync.Mutex{}, Mall: mall}
for x, slice := range mall {
for y, info := range slice {
if info == 'X' {
wg.Add(1)
go func(x int, y int) {
oldPos := [2]int{x, y}
var i int
for i = 0; i < 100; i++ {
game.Mu.Lock()
pos := getNextPos(game.Mall, oldPos)
game.Moves = append(game.Moves, [2][2]int{oldPos, pos})
game.Mu.Unlock()
if pos[0] == -2 && pos[1] == -2 {
break
}
game.Mu.Lock()
game.Mall[oldPos[0]][oldPos[1]] = '-'
game.Mall[pos[0]][pos[1]] = 'X'
game.Mu.Unlock()
oldPos = pos
}
if i==100 {
game.Mu.Lock()
game.Moves = append(game.Moves, [2][2]int{oldPos, {-1, -1}})
game.Mu.Unlock()
}
wg.Done()
}(x, y)
}
}
}
wg.Wait()
return game.Moves
}
func getNextPos(mall [4][4]rune, unit [2]int) [2]int {
toMove := [][2]int{{-1,-1}, {1,1}, {-1,1}, {1, -1}}
for _, move := range toMove {
nextPos := [2]int{(unit[0]+move[0]+len(mall))%len(mall), (unit[1]+move[1]+len(mall))%len(mall)}
if mall[nextPos[0]][nextPos[1]] == '-' {
return nextPos
}
}
return [2]int{-2, -2}
}
func main() {
}

Лог от изпълнението

PASS
ok  	_/tmp/d20140123-11403-1t0sbv6	0.011s
PASS
ok  	_/tmp/d20140123-11403-1t0sbv6	0.012s
PASS
ok  	_/tmp/d20140123-11403-1t0sbv6	0.012s
PASS
ok  	_/tmp/d20140123-11403-1t0sbv6	0.011s
PASS
ok  	_/tmp/d20140123-11403-1t0sbv6	0.012s
PASS
ok  	_/tmp/d20140123-11403-1t0sbv6	0.012s
PASS
ok  	_/tmp/d20140123-11403-1t0sbv6	0.012s
PASS
ok  	_/tmp/d20140123-11403-1t0sbv6	0.012s
PASS
ok  	_/tmp/d20140123-11403-1t0sbv6	0.011s
PASS
ok  	_/tmp/d20140123-11403-1t0sbv6	0.011s

История (1 версия и 0 коментара)

Мартин обнови решението на 19.01.2014 16:15 (преди над 4 години)

+package main
+
+import (
+ "sync"
+)
+
+type Game struct {
+ Mu sync.Mutex
+ Mall [4][4]rune
+ Moves [][2][2]int
+}
+
+func playMall(mall [4][4]rune) [][2][2]int {
+ var wg sync.WaitGroup
+ game := &Game{Mu: sync.Mutex{}, Mall: mall}
+ for x, slice := range mall {
+ for y, info := range slice {
+ if info == 'X' {
+ wg.Add(1)
+ go func(x int, y int) {
+ oldPos := [2]int{x, y}
+ var i int
+ for i = 0; i < 100; i++ {
+ game.Mu.Lock()
+ pos := getNextPos(game.Mall, oldPos)
+ game.Moves = append(game.Moves, [2][2]int{oldPos, pos})
+ game.Mu.Unlock()
+ if pos[0] == -2 && pos[1] == -2 {
+ break
+ }
+ game.Mu.Lock()
+ game.Mall[oldPos[0]][oldPos[1]] = '-'
+ game.Mall[pos[0]][pos[1]] = 'X'
+ game.Mu.Unlock()
+ oldPos = pos
+ }
+ if i==100 {
+ game.Mu.Lock()
+ game.Moves = append(game.Moves, [2][2]int{oldPos, {-1, -1}})
+ game.Mu.Unlock()
+ }
+ wg.Done()
+ }(x, y)
+ }
+ }
+ }
+ wg.Wait()
+ return game.Moves
+}
+
+func getNextPos(mall [4][4]rune, unit [2]int) [2]int {
+ toMove := [][2]int{{-1,-1}, {1,1}, {-1,1}, {1, -1}}
+ for _, move := range toMove {
+ nextPos := [2]int{(unit[0]+move[0]+len(mall))%len(mall), (unit[1]+move[1]+len(mall))%len(mall)}
+ if mall[nextPos[0]][nextPos[1]] == '-' {
+ return nextPos
+ }
+ }
+ return [2]int{-2, -2}
+}
+
+func main() {
+
+}