Решение на Ескалатори в мола от Симеон Тотев

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

Към профила на Симеон Тотев

Резултати

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

Код

package main
import (
"sync"
)
type Consumer struct {
Stores int
Stuck bool
}
func playMall(mall [4][4]rune) [][2][2]int {
result := make([][2][2]int, 0)
wg := new(sync.WaitGroup)
m := new(sync.Mutex)
for oldX, i := range mall {
for oldY, j := range i {
if j == '-' {
continue
}
wg.Add(1)
go func(oldX int, oldY int) {
c := new(Consumer)
movable := false
for c.Stores < 100 && !c.Stuck {
var newX int
var newY int
var k int
for k = 0; k < 4; k = k + 1 {
plusi := k%2 == 1
plusj := (k/2+(k+1)%2)%2 == 0
if plusi {
newX = oldX + 1
} else {
newX = oldX - 1
}
if plusj {
newY = oldY + 1
} else {
newY = oldY - 1
}
if newX < 0 {
newX = 3
}
if newX >= 4 {
newX = 0
}
if newY < 0 {
newY = 3
}
if newY >= 4 {
newY = 0
}
m.Lock()
movable = mall[newX][newY] == '-'
if movable {
mall[newX][newY] = 'X'
mall[oldX][oldY] = '-'
c.Stores = c.Stores + 1
result = append(result, [2][2]int{{oldX, oldY}, {newX, newY}})
oldX = newX
oldY = newY
m.Unlock()
break
} else {
m.Unlock()
}
}
if !movable && k == 4 {
c.Stuck = true
result = append(result, [2][2]int{{oldX, oldY}, {-2, -2}})
} else if c.Stores == 100 {
result = append(result, [2][2]int{{oldX, oldY}, {-1, -1}})
}
}
wg.Done()
}(oldX, oldY)
}
}
wg.Wait()
return result
}

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

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

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

Симеон обнови решението на 23.01.2014 01:38 (преди над 4 години)

+package main
+
+import (
+ "sync"
+)
+
+type Consumer struct {
+ Stores int
+ Stuck bool
+}
+
+func playMall(mall [4][4]rune) [][2][2]int {
+
+ result := make([][2][2]int, 0)
+
+ wg := new(sync.WaitGroup)
+ m := new(sync.Mutex)
+
+ for oldX, i := range mall {
+ for oldY, j := range i {
+ if j == '-' {
+ continue
+ }
+
+ wg.Add(1)
+
+ go func(oldX int, oldY int) {
+ c := new(Consumer)
+ movable := false
+
+ for c.Stores < 100 && !c.Stuck {
+
+ var newX int
+ var newY int
+
+ var k int
+
+ for k = 0; k < 4; k = k + 1 {
+ plusi := k%2 == 1
+ plusj := (k/2+(k+1)%2)%2 == 0
+
+ if plusi {
+ newX = oldX + 1
+ } else {
+ newX = oldX - 1
+ }
+
+ if plusj {
+ newY = oldY + 1
+ } else {
+ newY = oldY - 1
+ }
+
+ if newX < 0 {
+ newX = 3
+ }
+
+ if newX >= 4 {
+ newX = 0
+ }
+
+ if newY < 0 {
+ newY = 3
+ }
+
+ if newY >= 4 {
+ newY = 0
+ }
+
+ m.Lock()
+
+ movable = mall[newX][newY] == '-'
+
+ if movable {
+ mall[newX][newY] = 'X'
+ mall[oldX][oldY] = '-'
+
+ c.Stores = c.Stores + 1
+
+ result = append(result, [2][2]int{{oldX, oldY}, {newX, newY}})
+
+ oldX = newX
+ oldY = newY
+
+ m.Unlock()
+
+ break
+
+ } else {
+ m.Unlock()
+ }
+ }
+
+ if !movable && k == 4 {
+ c.Stuck = true
+ result = append(result, [2][2]int{{oldX, oldY}, {-2, -2}})
+ } else if c.Stores == 100 {
+ result = append(result, [2][2]int{{oldX, oldY}, {-1, -1}})
+ }
+ }
+
+ wg.Done()
+ }(oldX, oldY)
+ }
+ }
+
+ wg.Wait()
+
+ return result
+}