-
Notifications
You must be signed in to change notification settings - Fork 27
/
159.go
87 lines (79 loc) · 1.84 KB
/
159.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
// UVa 159 - Word Crosses
package main
import (
"fmt"
"io"
"os"
"strings"
)
type cross struct{ h, v int }
var noCross = cross{-1, -1}
func leadingWordCross(h, v string) cross {
for i := range h {
if idx := strings.Index(v, string(h[i])); idx >= 0 {
return cross{i, idx}
}
}
return cross{-1, -1}
}
func solve(out io.Writer, h1, v1, h2, v2 string) {
c1, c2 := leadingWordCross(h1, v1), leadingWordCross(h2, v2)
if c1 == noCross || c2 == noCross {
fmt.Fprintln(out, "Unable to make two crosses")
return
}
var idx1, idx2 int
switch {
case c1.v > c2.v:
for i := 0; i < (c1.v - c2.v); i++ {
fmt.Fprintf(out, "%s%c\n", strings.Repeat(" ", c1.h), v1[idx1])
idx1++
}
case c1.v < c2.v:
for i := 0; i < (c2.v - c1.v); i++ {
fmt.Fprintf(out, "%s%c\n", strings.Repeat(" ", len(h1)+3+c2.h), v2[idx2])
idx2++
}
}
for idx1 < c1.v {
fmt.Fprintf(out, "%s%c%s%c\n", strings.Repeat(" ", c1.h), v1[idx1], strings.Repeat(" ", len(h1)-c1.h-1+3+c2.h), v2[idx2])
idx1++
idx2++
}
fmt.Fprintf(out, "%s %s\n", h1, h2)
idx1++
idx2++
for idx1 < len(v1) || idx2 < len(v2) {
switch {
case idx1 < len(v1) && idx2 < len(v2):
fmt.Fprintf(out, "%s%c%s%c\n", strings.Repeat(" ", c1.h), v1[idx1], strings.Repeat(" ", len(h1)-c1.h-1+3+c2.h), v2[idx2])
idx1++
idx2++
case idx1 < len(v1):
fmt.Fprintf(out, "%s%c\n", strings.Repeat(" ", c1.h), v1[idx1])
idx1++
case idx2 < len(v2):
fmt.Fprintf(out, "%s%c\n", strings.Repeat(" ", len(h1)+3+c2.h), v2[idx2])
idx2++
}
}
}
func main() {
in, _ := os.Open("159.in")
defer in.Close()
out, _ := os.Create("159.out")
defer out.Close()
var h1, v1, h2, v2 string
for first := true; ; {
if fmt.Fscanf(in, "%s", &h1); h1 == "#" {
break
}
fmt.Fscanf(in, "%s%s%s", &v1, &h2, &v2)
if first {
first = false
} else {
fmt.Fprintln(out)
}
solve(out, h1, v1, h2, v2)
}
}