Решение на Ескалатори в мола от Иван Божилов

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

Към профила на Иван Божилов

Резултати

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

Код

package main
import "sync"
type Mall struct {
table [4][4]rune
m sync.Mutex
log [][2][2]int
}
var wg sync.WaitGroup
var currState Mall
func playMall(mall [4][4]rune) [][2][2]int {
currState.log = [][2][2]int{}
currState.table = mall
for i := range currState.table {
for j := range currState.table[i] {
if currState.table[i][j] == 'X' {
wg.Add(1)
go goAround([2]int{i, j})
}
}
}
wg.Wait()
return currState.log
}
func mod4(n int) int {
if n > 3 {
return n % 4
} else if n < 0 {
return 4 + n
}
return n
}
func doStep(table *[4][4]rune, person *[2]int, positionX int, positionY int) {
table[positionX][positionY] = 'X'
table[person[0]][person[1]] = '-'
person[0] = positionX
person[1] = positionY
}
func goAround(person [2]int) {
var step [2][2]int
defer func() {
wg.Done()
}()
for i := 0; i < 100; i++ {
currState.m.Lock()
step[0] = person
if currState.table[mod4(person[0]-1)][mod4(person[1]-1)] == '-' {
doStep(&currState.table, &person, mod4(person[0]-1), mod4(person[1]-1))
} else if currState.table[mod4(person[0]+1)][mod4(person[1]+1)] == '-' {
doStep(&currState.table, &person, mod4(person[0]+1), mod4(person[1]+1))
} else if currState.table[mod4(person[0]+1)][mod4(person[1]-1)] == '-' {
doStep(&currState.table, &person, mod4(person[0]+1), mod4(person[1]-1))
} else if currState.table[mod4(person[0]-1)][mod4(person[1]+1)] == '-' {
doStep(&currState.table, &person, mod4(person[0]-1), mod4(person[1]+1))
} else {
step[1] = [2]int{-2, -2}
currState.log = append(currState.log, step)
currState.m.Unlock()
return
}
step[1] = person
currState.log = append(currState.log, step)
currState.m.Unlock()
}
step[0] = person
step[1] = [2]int{-1, -1}
currState.m.Lock()
currState.log = append(currState.log, step)
currState.m.Unlock()
}

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

PASS
ok  	_/tmp/d20140123-11403-e70m3e	0.011s
PASS
ok  	_/tmp/d20140123-11403-e70m3e	0.012s
PASS
ok  	_/tmp/d20140123-11403-e70m3e	0.012s
PASS
ok  	_/tmp/d20140123-11403-e70m3e	0.011s
PASS
ok  	_/tmp/d20140123-11403-e70m3e	0.013s
PASS
ok  	_/tmp/d20140123-11403-e70m3e	0.012s
PASS
ok  	_/tmp/d20140123-11403-e70m3e	0.012s
PASS
ok  	_/tmp/d20140123-11403-e70m3e	0.012s
PASS
ok  	_/tmp/d20140123-11403-e70m3e	0.011s
PASS
ok  	_/tmp/d20140123-11403-e70m3e	0.012s

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

Иван обнови решението на 20.01.2014 06:15 (преди над 4 години)

+package main
+
+import "sync"
+
+type Mall struct {
+ table [4][4]rune
+ m sync.Mutex
+ log [][2][2]int
+}
+
+var wg sync.WaitGroup
+var currState Mall
+
+func playMall(mall [4][4]rune) [][2][2]int {
+ currState.log = [][2][2]int{}
+ currState.table = mall
+ for i := range currState.table {
+ for j := range currState.table[i] {
+ if currState.table[i][j] == 'X' {
+ wg.Add(1)
+ go goAround([2]int{i, j})
+ }
+ }
+ }
+ wg.Wait()
+ return currState.log
+}
+
+func abs(n int) int {
+ if n > 0 {
+ return n
+ } else {
+ return 4 + n
+ }
+}
+
+func goAround(person [2]int) {
+ var step [2][2]int
+ defer func() {
+ wg.Done()
+ }()
+ for i := 0; i < 100; i++ {
+ currState.m.Lock()
+ step[0] = person
+ if currState.table[abs(person[0]-1)%4][abs(person[1]-1)%4] == '-' {
+ currState.table[abs(person[0]-1)%4][abs(person[1]-1)%4] = 'X'
+ currState.table[person[0]][person[1]] = '-'
+ person[0] = abs(person[0]-1) % 4
+ person[1] = abs(person[1]-1) % 4
+ step[1] = person
+ } else if currState.table[(person[0]+1)%4][(person[1]+1)%4] == '-' {
+ currState.table[(person[0]+1)%4][(person[1]+1)%4] = 'X'
+ currState.table[person[0]][person[1]] = '-'
+ person[0] = (person[0] + 1) % 4
+ person[1] = (person[1] + 1) % 4
+ step[1] = person
+ } else if currState.table[(person[0]+1)%4][abs(person[1]-1)%4] == '-' {
+ currState.table[(person[0]+1)%4][abs(person[1]-1)%4] = 'X'
+ currState.table[person[0]][person[1]] = '-'
+ person[0] = (person[0] + 1) % 4
+ person[1] = abs(person[1]-1) % 4
+ step[1] = person
+ } else if currState.table[(person[0]+1)%4][abs(person[1]-1)%4] == '-' {
+ currState.table[abs(person[0]-1)%4][(person[1]+1)%4] = 'X'
+ currState.table[person[0]][person[1]] = '-'
+ person[0] = abs(person[0]-1) % 4
+ person[1] = (person[1] + 1) % 4
+ step[1] = person
+ } else {
+ step[1] = [2]int{-2, -2}
+ currState.log = append(currState.log, step)
+ currState.m.Unlock()
+ return
+ }
+ currState.log = append(currState.log, step)
+ currState.m.Unlock()
+ }
+ step[0] = person
+ step[1] = [2]int{-1, -1}
+ currState.m.Lock()
+ currState.log = append(currState.log, step)
+ currState.m.Unlock()
+}

Иван обнови решението на 21.01.2014 16:03 (преди над 4 години)

package main
import "sync"
type Mall struct {
table [4][4]rune
m sync.Mutex
log [][2][2]int
}
var wg sync.WaitGroup
var currState Mall
func playMall(mall [4][4]rune) [][2][2]int {
currState.log = [][2][2]int{}
currState.table = mall
for i := range currState.table {
for j := range currState.table[i] {
if currState.table[i][j] == 'X' {
wg.Add(1)
go goAround([2]int{i, j})
}
}
}
wg.Wait()
return currState.log
}
-func abs(n int) int {
- if n > 0 {
- return n
- } else {
+func mod4(n int) int {
+ if n > 3 {
+ return n % 4
+ } else if n < 0 {
return 4 + n
}
+ return n
}
+func doStep(table *[4][4]rune, person *[2]int, positionX int, positionY int) {
+ table[positionX][positionY] = 'X'
+ table[person[0]][person[1]] = '-'
+ person[0] = positionX
+ person[1] = positionY
+}
+
func goAround(person [2]int) {
var step [2][2]int
defer func() {
wg.Done()
}()
for i := 0; i < 100; i++ {
currState.m.Lock()
step[0] = person
- if currState.table[abs(person[0]-1)%4][abs(person[1]-1)%4] == '-' {
- currState.table[abs(person[0]-1)%4][abs(person[1]-1)%4] = 'X'
- currState.table[person[0]][person[1]] = '-'
- person[0] = abs(person[0]-1) % 4
- person[1] = abs(person[1]-1) % 4
- step[1] = person
- } else if currState.table[(person[0]+1)%4][(person[1]+1)%4] == '-' {
- currState.table[(person[0]+1)%4][(person[1]+1)%4] = 'X'
- currState.table[person[0]][person[1]] = '-'
- person[0] = (person[0] + 1) % 4
- person[1] = (person[1] + 1) % 4
- step[1] = person
- } else if currState.table[(person[0]+1)%4][abs(person[1]-1)%4] == '-' {
- currState.table[(person[0]+1)%4][abs(person[1]-1)%4] = 'X'
- currState.table[person[0]][person[1]] = '-'
- person[0] = (person[0] + 1) % 4
- person[1] = abs(person[1]-1) % 4
- step[1] = person
- } else if currState.table[(person[0]+1)%4][abs(person[1]-1)%4] == '-' {
- currState.table[abs(person[0]-1)%4][(person[1]+1)%4] = 'X'
- currState.table[person[0]][person[1]] = '-'
- person[0] = abs(person[0]-1) % 4
- person[1] = (person[1] + 1) % 4
- step[1] = person
+ if currState.table[mod4(person[0]-1)][mod4(person[1]-1)] == '-' {
+ doStep(&currState.table, &person, mod4(person[0]-1), mod4(person[1]-1))
+ } else if currState.table[mod4(person[0]+1)][mod4(person[1]+1)] == '-' {
+ doStep(&currState.table, &person, mod4(person[0]+1), mod4(person[1]+1))
+ } else if currState.table[mod4(person[0]+1)][mod4(person[1]-1)] == '-' {
+ doStep(&currState.table, &person, mod4(person[0]+1), mod4(person[1]-1))
+ } else if currState.table[mod4(person[0]-1)][mod4(person[1]+1)] == '-' {
+ doStep(&currState.table, &person, mod4(person[0]-1), mod4(person[1]+1))
} else {
step[1] = [2]int{-2, -2}
currState.log = append(currState.log, step)
currState.m.Unlock()
return
}
+ step[1] = person
currState.log = append(currState.log, step)
currState.m.Unlock()
}
step[0] = person
step[1] = [2]int{-1, -1}
currState.m.Lock()
currState.log = append(currState.log, step)
currState.m.Unlock()
-}
+}