-
Notifications
You must be signed in to change notification settings - Fork 26
/
11221.go
69 lines (60 loc) · 1.27 KB
/
11221.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
// UVa 11221 - Magic square palindromes.
package main
import (
"bufio"
"fmt"
"math"
"os"
"strconv"
"strings"
)
var keepLetter = func(r rune) rune {
if r >= 'a' && r <= 'z' {
return r
}
return -1
}
func isPalindromes(l int, line string) bool {
lastIndex := l - 1
for i := 0; i < lastIndex/2; i++ {
if line[i] != line[lastIndex-i] {
return false
}
}
return true
}
func reShape(side int, line string) string {
newLine := make([]byte, side*side)
for i := 0; i < side; i++ {
for j := 0; j < side; j++ {
newLine[i*side+j] = line[i+j*side]
}
}
return string(newLine)
}
func solve(line string) int {
newLine := strings.Map(keepLetter, line)
length := len(newLine)
if side := int(math.Sqrt(float64(length)) + .5); side*side == length && isPalindromes(length, newLine) && isPalindromes(length, reShape(side, newLine)) {
return side
}
return -1
}
func main() {
in, _ := os.Open("11221.in")
defer in.Close()
out, _ := os.Create("11221.out")
defer out.Close()
s := bufio.NewScanner(in)
s.Split(bufio.ScanLines)
s.Scan()
kase, _ := strconv.Atoi(s.Text())
for i := 1; i <= kase && s.Scan(); i++ {
fmt.Fprintf(out, "Case #%d:\n", i)
if side := solve(s.Text()); side == -1 {
fmt.Fprintln(out, "No magic :(")
} else {
fmt.Fprintln(out, side)
}
}
}