-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
77 lines (67 loc) · 1.12 KB
/
main.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
package main
import (
"bufio"
"fmt"
"math"
"os"
"strings"
)
type Person struct {
line int
position int
found bool
}
func main() {
in := bufio.NewReader(os.Stdin)
var m, p Person
line := 0
for {
inputBt, _, err := in.ReadLine()
input := string(inputBt)
if err != nil {
break
}
if mi := strings.Index(input, "m"); mi != -1 {
m.line = line
m.position = mi
m.found = true
}
if pi := strings.Index(input, "p"); pi != -1 {
p.line = line
p.position = pi
p.found = true
}
if m.found && p.found {
break
}
line++
}
if !m.found || !p.found {
return
}
ver := getVerticalSteps(int32(p.line-m.line), "UP", "DOWN")
hor := getVerticalSteps(int32(p.position-m.position), "LEFT", "RIGHT")
_, err := fmt.Fprint(os.Stdout, ver)
if err != nil {
return
}
_, err = fmt.Fprint(os.Stdout, hor)
if err != nil {
return
}
}
func getVerticalSteps(n int32, txtf, txts string) string {
var rst string
if n == 0 {
return rst
}
txt := txts
if n < 0 {
txt = txtf
}
n = int32(math.Abs(float64(n)))
for i := int32(0); i < n; i++ {
rst += fmt.Sprintf("%s\n", txt)
}
return rst
}