-
Notifications
You must be signed in to change notification settings - Fork 0
/
05.go
60 lines (55 loc) · 1.3 KB
/
05.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
package main
import (
"bufio"
"fmt"
"os"
)
type stack []string
func (s *stack) move(to *stack, n int) {
for i := 0; i < n; i++ {
*to = append(*to, (*s)[len(*s)-1])
*s = (*s)[:len(*s)-1]
}
}
func tops(ss []stack) string {
result := ""
for _, s := range ss {
result = result + s[len(s)-1]
}
return result
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
lines := []string{}
for scanner.Scan() {
if scanner.Text() == "" {
break
}
lines = append(lines, scanner.Text())
}
stacks := []stack{stack{}, stack{}, stack{}, stack{}, stack{}, stack{}, stack{}, stack{}, stack{}}
stacks2 := []stack{stack{}, stack{}, stack{}, stack{}, stack{}, stack{}, stack{}, stack{}, stack{}}
for i := len(lines) - 2; i >= 0; i-- {
for j, n := 1, 0; j < len(lines[i]); j, n = j+4, n+1 {
crate := string(lines[i][j])
if crate != " " {
stacks[n] = append(stacks[n], crate)
stacks2[n] = append(stacks2[n], crate)
}
}
}
for scanner.Scan() {
var count, from, to int
_, err := fmt.Sscanf(scanner.Text(), "move %d from %d to %d", &count, &from, &to)
if err != nil {
continue
}
from = from - 1
to = to - 1
(&stacks[from]).move(&stacks[to], count)
tmp := stack{}
(&stacks2[from]).move(&tmp, count)
(&tmp).move(&stacks2[to], count)
}
fmt.Println(tops(stacks), tops(stacks2))
}