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

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

Към профила на Александър Димитров

Резултати

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

Код

package main
import "sync"
var directions [4][2]int = [4][2]int{{-1, -1}, {1, 1}, {-1, 1}, {1, -1}}
type person struct {
movesCount int
position [2]int
Finished bool
}
func newPerson(x int, y int) *person {
var p *person = new(person)
p.position[0] = x
p.position[1] = y
return p
}
func (p *person) move(mall *[4][4]rune) [2][2]int {
var result [2][2]int
result[0][0] = p.position[0]
result[0][1] = p.position[1]
if p.movesCount != 100 {
var x, y int
moved := false
for i := 0; i < 4; i++ {
x = (p.position[0] + directions[i][0] + 4) % 4
y = (p.position[1] + directions[i][1] + 4) % 4
if mall[x][y] == '-' {
mall[x][y] = 'X'
mall[p.position[0]][p.position[1]] = '-'
result[1][0] = x
result[1][1] = y
p.position[0] = x
p.position[1] = y
p.movesCount++
moved = true
break
}
}
if !moved {
result[1][0] = -2
result[1][1] = -2
p.Finished = true
}
} else {
result[1][0] = -1
result[1][1] = -1
p.Finished = true
}
return result
}
func playMall(mall [4][4]rune) [][2][2]int {
log := [][2][2]int{}
people := []*person{}
peopleCount := 0
for i := 0; i < 4; i++ {
for j := 0; j < 4; j++ {
if mall[i][j] == 'X' {
people = append(people, newPerson(i, j))
peopleCount++
}
}
}
var wg sync.WaitGroup
for i := 0; i < peopleCount; i++ {
wg.Add(1)
go func(p *person, log *[][2][2]int, mall *[4][4]rune) {
defer wg.Done()
for !p.Finished {
*log = append(*log, p.move(mall))
}
}(people[i], &log, &mall)
}
wg.Wait()
return log
}

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

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

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

Александър обнови решението на 19.01.2014 17:54 (преди над 4 години)

+package main
+
+var directions [4][2]int = [4][2]int{{-1, -1}, {1, 1}, {-1, 1}, {1, -1}}
+
+type person struct {
+ movesCount int
+ position [2]int
+ Finished bool
+}
+
+func newPerson(x int, y int) *person {
+ var p *person = new(person)
+ p.position[0] = x
+ p.position[1] = y
+ return p
+}
+
+func (p *person) move(mall *[4][4]rune) [2][2]int {
+ var result [2][2]int
+ result[0][0] = p.position[0]
+ result[0][1] = p.position[1]
+
+ if p.movesCount != 100 {
+ var x, y int
+ moved := false
+
+ for i := 0; i < 4; i++ {
+ x = (p.position[0] + directions[i][0] + 4) % 4
+ y = (p.position[1] + directions[i][1] + 4) % 4
+
+ if mall[x][y] == '-' {
+ mall[x][y] = 'X'
+ mall[p.position[0]][p.position[1]] = '-'
+
+ result[1][0] = x
+ result[1][1] = y
+ p.position[0] = x
+ p.position[1] = y
+
+ p.movesCount++
+ moved = true
+ break
+ }
+ }
+
+ if !moved {
+ result[1][0] = -2
+ result[1][1] = -2
+ p.Finished = true
+ }
+ } else {
+ result[1][0] = -1
+ result[1][1] = -1
+ p.Finished = true
+ }
+
+ return result
+}
+
+func playMall(mall [4][4]rune) [][2][2]int {
+ log := [][2][2]int{}
+ people := []*person{}
+ peopleCount := 0
+
+ for i := 0; i < 4; i++ {
+ for j := 0; j < 4; j++ {
+ if mall[i][j] == 'X' {
+ people = append(people, newPerson(i, j))
+ peopleCount++
+ }
+ }
+ }
+
+ activePeople := true
+
+ for activePeople {
+ activePeople = false
+
+ for i := 0; i < peopleCount; i++ {
+ if !people[i].Finished {
+ log = append(log, people[i].move(&mall))
+ activePeople = true
+ }
+ }
+ }
+
+ return log
+}

В решението ти няма никаква конкурентност. Всичко върви последователно.

В условието изрично пише:

Обитателите трябва да се мърдат паралелно един с друг и независимо един от друг. Не се опитвайте да изпълнявате всичко последователно.

Александър обнови решението на 21.01.2014 23:30 (преди над 4 години)

package main
+import "sync"
+
var directions [4][2]int = [4][2]int{{-1, -1}, {1, 1}, {-1, 1}, {1, -1}}
type person struct {
movesCount int
position [2]int
Finished bool
}
func newPerson(x int, y int) *person {
var p *person = new(person)
p.position[0] = x
p.position[1] = y
return p
}
func (p *person) move(mall *[4][4]rune) [2][2]int {
var result [2][2]int
result[0][0] = p.position[0]
result[0][1] = p.position[1]
if p.movesCount != 100 {
var x, y int
moved := false
for i := 0; i < 4; i++ {
x = (p.position[0] + directions[i][0] + 4) % 4
y = (p.position[1] + directions[i][1] + 4) % 4
if mall[x][y] == '-' {
mall[x][y] = 'X'
mall[p.position[0]][p.position[1]] = '-'
result[1][0] = x
result[1][1] = y
p.position[0] = x
p.position[1] = y
p.movesCount++
moved = true
break
}
}
if !moved {
result[1][0] = -2
result[1][1] = -2
p.Finished = true
}
} else {
result[1][0] = -1
result[1][1] = -1
p.Finished = true
}
return result
}
func playMall(mall [4][4]rune) [][2][2]int {
log := [][2][2]int{}
people := []*person{}
peopleCount := 0
for i := 0; i < 4; i++ {
for j := 0; j < 4; j++ {
if mall[i][j] == 'X' {
people = append(people, newPerson(i, j))
peopleCount++
}
}
}
- activePeople := true
+ var wg sync.WaitGroup
- for activePeople {
- activePeople = false
+ for i := 0; i < peopleCount; i++ {
+ wg.Add(1)
- for i := 0; i < peopleCount; i++ {
- if !people[i].Finished {
- log = append(log, people[i].move(&mall))
- activePeople = true
+ go func(p *person, log *[][2][2]int, mall *[4][4]rune) {
+ defer wg.Done()
+
+ for !p.Finished {
+ *log = append(*log, p.move(mall))
}
- }
+ }(people[i], &log, &mall)
}
+
+ wg.Wait()
return log
}