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

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

Към профила на Мария Терзиева

Резултати

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

Код

package main
import "sync"
type Person struct {
mu sync.Mutex
coordinates [2]int
moves [][2][2]int
}
func insideMall(coordinates [2]int) [2]int {
for index, value := range coordinates {
if value == -1 {
coordinates[index] = 3
}
if value == 4 {
coordinates[index] = 0
}
}
return coordinates
}
func (p *Person) newCoordinates(mall [4][4]rune) [2]int {
newX := insideMall([2]int{p.coordinates[0] - 1, p.coordinates[0] + 1})
newY := insideMall([2]int{p.coordinates[1] - 1, p.coordinates[1] + 1})
switch {
case len(p.moves) == 100:
return [2]int{-1, -1}
case mall[newY[0]][newX[0]] == '-':
return [2]int{newX[0], newY[0]}
case mall[newY[1]][newX[1]] == '-':
return [2]int{newX[1], newY[1]}
case mall[newY[1]][newX[0]] == '-':
return [2]int{newX[0], newY[1]}
case mall[newY[0]][newX[1]] == '-':
return [2]int{newX[1], newY[0]}
default:
return [2]int{-2, -2}
}
}
func (p *Person) move(mall [4][4]rune) {
p.mu.Lock()
oldCoordinates := p.coordinates
newCoordinates := p.newCoordinates(mall)
p.moves = append(p.moves, [2][2]int{oldCoordinates, newCoordinates})
p.coordinates = newCoordinates
mall[oldCoordinates[1]][oldCoordinates[0]] = '-'
mall[newCoordinates[1]][newCoordinates[0]] = 'X'
p.mu.Unlock()
}
func (p *Person) wanderAround(mall [4][4]rune) [][2][2]int {
for len(p.moves) < 100 && p.coordinates != [2]int{-2, -2} {
p.move(mall)
}
return p.moves
}
func playMall(mall [4][4]rune) [][2][2]int {
result := make([][2][2]int, 1)
for row := 0; row < 4; row++ {
for column := 0; column < 4; column++ {
if mall[row][column] == 'X' {
p := Person{coordinates: [2]int{column, row}}
go p.wanderAround(mall)
result = append(result, p.moves...)
}
}
}
return result
}

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

panic: runtime error: index out of range

goroutine 5 [running]:
_/tmp/d20140123-11403-7l9lp9.(*Person).move(0x182306c0, 0x2d, 0x58, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:50 +0x1eb
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x182306c0, 0x58, 0x58, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:56 +0x6b
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 1 [runnable]:
testing.RunTests(0x813b310, 0x81ad0e0, 0xa, 0xa, 0x1, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:434 +0x69f
testing.Main(0x813b310, 0x81ad0e0, 0xa, 0xa, 0x81b0540, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:365 +0x69
main.main()
	_/tmp/d20140123-11403-7l9lp9/_test/_testmain.go:61 +0x81

goroutine 6 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x182306e0, 0x58, 0x58, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 7 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230700, 0x58, 0x58, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 8 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230720, 0x58, 0x58, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 9 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230740, 0x58, 0x58, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 10 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230760, 0x58, 0x58, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 11 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230780, 0x58, 0x58, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 12 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x182307a0, 0x58, 0x58, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 13 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x182307c0, 0x58, 0x58, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 14 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x182307e0, 0x58, 0x58, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 15 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230800, 0x58, 0x58, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 16 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230820, 0x58, 0x58, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 17 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230840, 0x58, 0x58, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 18 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230860, 0x58, 0x58, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 19 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230880, 0x58, 0x58, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 20 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x182308a0, 0x58, 0x58, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b
exit status 2
FAIL	_/tmp/d20140123-11403-7l9lp9	0.022s
panic: runtime error: index out of range

goroutine 5 [running]:
_/tmp/d20140123-11403-7l9lp9.(*Person).move(0x182306c0, 0x2d, 0x2d, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:50 +0x1eb
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x182306c0, 0x58, 0x2d, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:56 +0x6b
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 1 [runnable]:
testing.RunTests(0x813b310, 0x81ad0e0, 0xa, 0xa, 0x1, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:434 +0x69f
testing.Main(0x813b310, 0x81ad0e0, 0xa, 0xa, 0x81b0540, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:365 +0x69
main.main()
	_/tmp/d20140123-11403-7l9lp9/_test/_testmain.go:61 +0x81

goroutine 6 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x182306e0, 0x58, 0x2d, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 7 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230700, 0x58, 0x2d, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 8 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230720, 0x58, 0x2d, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 9 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230740, 0x58, 0x2d, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 10 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230760, 0x58, 0x2d, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 11 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230780, 0x58, 0x2d, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 12 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x182307a0, 0x58, 0x2d, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 13 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x182307c0, 0x58, 0x2d, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 14 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x182307e0, 0x58, 0x2d, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 15 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230800, 0x58, 0x2d, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 16 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230820, 0x58, 0x2d, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 17 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230840, 0x58, 0x2d, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 18 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230860, 0x58, 0x2d, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 19 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230880, 0x58, 0x2d, 0x58, 0x58, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b
exit status 2
FAIL	_/tmp/d20140123-11403-7l9lp9	0.012s
--- FAIL: Test3 (0.00 seconds)
FAIL
exit status 1
FAIL	_/tmp/d20140123-11403-7l9lp9	0.011s
panic: runtime error: index out of range

goroutine 5 [running]:
_/tmp/d20140123-11403-7l9lp9.(*Person).move(0x182306c0, 0x2d, 0x2d, 0x58, 0x2d, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:50 +0x1eb
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x182306c0, 0x58, 0x2d, 0x58, 0x2d, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:56 +0x6b
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 1 [runnable]:
testing.RunTests(0x813b310, 0x81ad0e0, 0xa, 0xa, 0x1, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:434 +0x69f
testing.Main(0x813b310, 0x81ad0e0, 0xa, 0xa, 0x81b0540, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:365 +0x69
main.main()
	_/tmp/d20140123-11403-7l9lp9/_test/_testmain.go:61 +0x81

goroutine 6 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x182306e0, 0x58, 0x2d, 0x58, 0x2d, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 7 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230700, 0x58, 0x2d, 0x58, 0x2d, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 8 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230720, 0x58, 0x2d, 0x58, 0x2d, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 9 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230740, 0x58, 0x2d, 0x58, 0x2d, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 10 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230760, 0x58, 0x2d, 0x58, 0x2d, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 11 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230780, 0x58, 0x2d, 0x58, 0x2d, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 12 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x182307a0, 0x58, 0x2d, 0x58, 0x2d, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b
exit status 2
FAIL	_/tmp/d20140123-11403-7l9lp9	0.014s
--- FAIL: Test5 (0.00 seconds)
FAIL
exit status 1
FAIL	_/tmp/d20140123-11403-7l9lp9	0.011s
--- FAIL: Test6 (0.00 seconds)
FAIL
exit status 1
FAIL	_/tmp/d20140123-11403-7l9lp9	0.011s
--- FAIL: Test7 (0.00 seconds)
FAIL
exit status 1
FAIL	_/tmp/d20140123-11403-7l9lp9	0.011s
panic: runtime error: index out of range

goroutine 5 [running]:
_/tmp/d20140123-11403-7l9lp9.(*Person).move(0x182306c0, 0x2d, 0x2d, 0x58, 0x2d, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:50 +0x1eb
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x182306c0, 0x2d, 0x58, 0x58, 0x2d, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:56 +0x6b
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 1 [runnable]:
testing.RunTests(0x813b310, 0x81ad0e0, 0xa, 0xa, 0x1, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:434 +0x69f
testing.Main(0x813b310, 0x81ad0e0, 0xa, 0xa, 0x81b0540, ...)
	/usr/local/lib/go/src/pkg/testing/testing.go:365 +0x69
main.main()
	_/tmp/d20140123-11403-7l9lp9/_test/_testmain.go:61 +0x81

goroutine 6 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x182306e0, 0x2d, 0x58, 0x58, 0x2d, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 7 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230700, 0x2d, 0x58, 0x58, 0x2d, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 8 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230720, 0x2d, 0x58, 0x58, 0x2d, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 9 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230740, 0x2d, 0x58, 0x58, 0x2d, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 10 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230760, 0x2d, 0x58, 0x58, 0x2d, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 11 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x18230780, 0x2d, 0x58, 0x58, 0x2d, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b

goroutine 12 [runnable]:
_/tmp/d20140123-11403-7l9lp9.(*Person).wanderAround(0x182307a0, 0x2d, 0x58, 0x58, 0x2d, ...)
	/tmp/d20140123-11403-7l9lp9/solution.go:54
created by _/tmp/d20140123-11403-7l9lp9.playMall
	/tmp/d20140123-11403-7l9lp9/solution.go:68 +0x12b
exit status 2
FAIL	_/tmp/d20140123-11403-7l9lp9	0.013s
--- FAIL: Test9 (0.00 seconds)
FAIL
exit status 1
FAIL	_/tmp/d20140123-11403-7l9lp9	0.011s
--- FAIL: Test10 (0.00 seconds)
FAIL
exit status 1
FAIL	_/tmp/d20140123-11403-7l9lp9	0.011s

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

Мария обнови решението на 23.01.2014 16:35 (преди над 4 години)

+package main
+
+import "sync"
+
+type Person struct {
+ mu sync.Mutex
+ coordinates [2]int
+ moves [][2][2]int
+}
+
+func insideMall(coordinates [2]int) [2]int {
+ for index, value := range coordinates {
+ if value == -1 {
+ coordinates[index] = 3
+ }
+ if value == 4 {
+ coordinates[index] = 0
+ }
+ }
+ return coordinates
+}
+
+func (p *Person) newCoordinates(mall [4][4]rune) [2]int {
+ newX := insideMall([2]int{p.coordinates[0] - 1, p.coordinates[0] + 1})
+ newY := insideMall([2]int{p.coordinates[1] - 1, p.coordinates[1] + 1})
+
+ switch {
+ case len(p.moves) == 100:
+ return [2]int{-1, -1}
+ case mall[newY[0]][newX[0]] == '-':
+ return [2]int{newX[0], newY[0]}
+ case mall[newY[1]][newX[1]] == '-':
+ return [2]int{newX[1], newY[1]}
+ case mall[newY[1]][newX[0]] == '-':
+ return [2]int{newX[0], newY[1]}
+ case mall[newY[0]][newX[1]] == '-':
+ return [2]int{newX[1], newY[0]}
+ default:
+ return [2]int{-2, -2}
+ }
+}
+
+func (p *Person) move(mall [4][4]rune) {
+ p.mu.Lock()
+ oldCoordinates := p.coordinates
+ newCoordinates := p.newCoordinates(mall)
+ p.moves = append(p.moves, [2][2]int{oldCoordinates, newCoordinates})
+ p.coordinates = newCoordinates
+ mall[oldCoordinates[1]][oldCoordinates[0]] = '-'
+ mall[newCoordinates[1]][newCoordinates[0]] = 'X'
+ p.mu.Unlock()
+}
+
+func (p *Person) wanderAround(mall [4][4]rune) [][2][2]int {
+ for len(p.moves) < 100 && p.coordinates != [2]int{-2, -2} {
+ p.move(mall)
+ }
+ return p.moves
+}
+
+func playMall(mall [4][4]rune) [][2][2]int {
+ result := make([][2][2]int, 1)
+
+ for row := 0; row < 4; row++ {
+ for column := 0; column < 4; column++ {
+ if mall[row][column] == 'X' {
+ p := Person{coordinates: [2]int{column, row}}
+ go p.wanderAround(mall)
+ result = append(result, p.moves...)
+ }
+ }
+ }
+
+ return result
+}