-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpush_dominoes.go
101 lines (88 loc) · 1.78 KB
/
push_dominoes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
// func fall(l, c, r rune) rune {
// if c == '.' {
// if l == 'R' && r == 'L' {
// return '.'
// }
// if l == 'R' {
// return 'R'
// }
// if r == 'L' {
// return 'L'
// }
// }
// return c
// }
// func pushIteration(dominoes string) string {
// result := make([]rune, len(dominoes))
// if len(dominoes) == 1 {
// return dominoes
// }
// dominoesR := []rune(dominoes)
// for i := 0; i < len(dominoesR); i++ {
// switch i {
// case 0:
// result[i] = fall('.', dominoesR[i], dominoesR[i+1])
// case len(dominoes) - 1:
// result[i] = fall(dominoesR[i-1], dominoesR[i], '.')
// default:
// result[i] = fall(dominoesR[i-1], dominoesR[i], dominoesR[i+1])
// }
// }
// return string(result)
// }
// func pushDominoes(dominoes string) string {
// previousIteration := pushIteration(dominoes)
// for previousIteration != dominoes {
// previousIteration = dominoes
// dominoes = pushIteration(dominoes)
// }
// return dominoes
// }
func pushDominoes(dominoes string) string {
var (
buf = make([]int, len(dominoes))
res = make([]byte, 0, len(buf))
val = 0
dot byte = 46
r byte = 82
l byte = 76
)
for i := 0; i < len(dominoes); i++ {
if dominoes[i] == r {
val = len(dominoes)
} else if dominoes[i] == l {
val = 0
} else {
val = max(val-1, 0)
}
buf[i] += val
}
val = 0
for i := len(dominoes) - 1; i >= 0; i-- {
if dominoes[i] == l {
val = len(dominoes)
} else if dominoes[i] == r {
val = 0
} else {
val = max(val-1, 0)
}
buf[i] -= val
}
for i := 0; i < len(buf); i++ {
if buf[i] < 0 {
res = append(res, l)
} else if buf[i] > 0 {
res = append(res, r)
} else {
res = append(res, dot)
}
}
return string(res)
}
func max(a, b int) int {
if a > b {
return a
}
return b
}