Решение на Ескалатори в мола от Недялко Андреев

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

Към профила на Недялко Андреев

Резултати

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

Код

package main
// A pretty stupid solution, can't think of anything better...
import (
"sync"
)
func moves(pos [2]int) [][2]int {
return [][2]int{
{(3 + pos[0]) % 4, (3 + pos[1]) % 4},
{(5 + pos[0]) % 4, (5 + pos[1]) % 4},
{(5 + pos[0]) % 4, (3 + pos[1]) % 4},
{(3 + pos[0]) % 4, (5 + pos[1]) % 4},
}
}
func playMall(mall [4][4]rune) [][2][2]int {
var wg sync.WaitGroup
var mallMutex, resultMutex sync.Mutex // This is shitty, there has to be a better way than locking the whole mall...
successPos := [2]int{-1, -1}
stuckPos := [2]int{-2, -2}
result := make([][2][2]int, 0, 100)
shopper := func(myrow, mycol int) {
defer wg.Done()
myPos := [2]int{myrow, mycol}
var newPos [2]int
for i := 0; i < 100; i++ {
mallMutex.Lock()
myMoves := moves(myPos)
newPos = stuckPos
for _, move := range myMoves {
if mall[move[0]][move[1]] != 'X' {
newPos = move
break
}
}
if newPos != stuckPos {
mall[newPos[0]][newPos[1]] = 'X'
mall[myPos[0]][myPos[1]] = '-'
}
resultMutex.Lock()
mallMutex.Unlock()
result = append(result, [2][2]int{myPos, newPos})
myPos = newPos
resultMutex.Unlock()
if myPos == stuckPos {
break
}
//time.Sleep(10 * time.Millisecond)
}
if myPos != stuckPos {
resultMutex.Lock()
result = append(result, [2][2]int{myPos, successPos})
resultMutex.Unlock()
}
}
for row := range mall {
for col, val := range mall[row] {
if val == 'X' {
wg.Add(1)
go shopper(row, col)
}
}
}
wg.Wait()
return result
}

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

PASS
ok  	_/tmp/d20140123-11403-b2h66	0.011s
PASS
ok  	_/tmp/d20140123-11403-b2h66	0.012s
PASS
ok  	_/tmp/d20140123-11403-b2h66	0.012s
PASS
ok  	_/tmp/d20140123-11403-b2h66	0.011s
PASS
ok  	_/tmp/d20140123-11403-b2h66	0.012s
PASS
ok  	_/tmp/d20140123-11403-b2h66	0.012s
PASS
ok  	_/tmp/d20140123-11403-b2h66	0.012s
PASS
ok  	_/tmp/d20140123-11403-b2h66	0.012s
PASS
ok  	_/tmp/d20140123-11403-b2h66	0.011s
PASS
ok  	_/tmp/d20140123-11403-b2h66	0.011s

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

Недялко обнови решението на 23.01.2014 14:32 (преди над 4 години)

+package main
+
+// A pretty stupid solution, can't think of anything better...
+
+import (
+ "sync"
+)
+
+func moves(pos [2]int) [][2]int {
+ return [][2]int{
+ {(3 + pos[0]) % 4, (3 + pos[1]) % 4},
+ {(5 + pos[0]) % 4, (5 + pos[1]) % 4},
+ {(5 + pos[0]) % 4, (3 + pos[1]) % 4},
+ {(3 + pos[0]) % 4, (5 + pos[1]) % 4},
+ }
+}
+
+func playMall(mall [4][4]rune) [][2][2]int {
+ var wg sync.WaitGroup
+ var mallMutex, resultMutex sync.Mutex // This is shitty, there has to be a better way than locking the whole mall...
+
+ successPos := [2]int{-1, -1}
+ stuckPos := [2]int{-2, -2}
+
+ result := make([][2][2]int, 0, 100)
+
+ shopper := func(myrow, mycol int) {
+ defer wg.Done()
+ myPos := [2]int{myrow, mycol}
+ var newPos [2]int
+
+ for i := 0; i < 100; i++ {
+ mallMutex.Lock()
+ myMoves := moves(myPos)
+ newPos = stuckPos
+
+ for _, move := range myMoves {
+ if mall[move[0]][move[1]] != 'X' {
+ newPos = move
+ break
+ }
+ }
+
+ if newPos != stuckPos {
+ mall[newPos[0]][newPos[1]] = 'X'
+ mall[myPos[0]][myPos[1]] = '-'
+ }
+
+ resultMutex.Lock()
+ mallMutex.Unlock()
+
+ result = append(result, [2][2]int{myPos, newPos})
+ myPos = newPos
+ resultMutex.Unlock()
+
+ if myPos == stuckPos {
+ break
+ }
+
+ //time.Sleep(10 * time.Millisecond)
+ }
+
+ if myPos != stuckPos {
+ resultMutex.Lock()
+ result = append(result, [2][2]int{myPos, successPos})
+ resultMutex.Unlock()
+ }
+
+ }
+
+ for row := range mall {
+ for col, val := range mall[row] {
+ if val == 'X' {
+ wg.Add(1)
+ go shopper(row, col)
+ }
+ }
+ }
+
+ wg.Wait()
+
+ return result
+}