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

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

Към профила на Георги Пеев

Резултати

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

Код

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
}

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

PASS
ok  	_/tmp/d20140123-11403-11d778k	0.011s
PASS
ok  	_/tmp/d20140123-11403-11d778k	0.012s
PASS
ok  	_/tmp/d20140123-11403-11d778k	0.012s
PASS
ok  	_/tmp/d20140123-11403-11d778k	0.011s
PASS
ok  	_/tmp/d20140123-11403-11d778k	0.023s
PASS
ok  	_/tmp/d20140123-11403-11d778k	0.012s
PASS
ok  	_/tmp/d20140123-11403-11d778k	0.018s
PASS
ok  	_/tmp/d20140123-11403-11d778k	0.012s
PASS
ok  	_/tmp/d20140123-11403-11d778k	0.022s
PASS
ok  	_/tmp/d20140123-11403-11d778k	0.011s

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

Георги обнови решението на 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
+}