WA1 i dont understand why (golang)
Posted by
Khinel 8 Jun 2017 23:12
package main
import (
"bufio"
"os"
"strconv"
"fmt"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
firstLine, _ := reader.ReadString('\n')
count, _ := strconv.Atoi(strings.Trim(firstLine, "\n"))
result := make([]int, count)
for i := 0; i < count; i++ {
position, _ := reader.ReadString('\n')
result[i] = countFieldsUnderAttack(position)
}
for _, val := range result {
fmt.Println(val)
}
}
func countFieldsUnderAttack(position string) int {
result := 0
var positionInts [2]int
positionInts[0] = int(position[0]) - int('a') + 1
positionInts[1] = int(position[1]) - int('1') + 1
moves := [8][2]int{
{1, 2},
{2, 1},
{-1, -2},
{-2, -1},
{-1, 2},
{2, -1},
{1, -2},
{-2, 1},
}
for _, move := range moves {
if move[0] + positionInts[0] > 0 && move[0] + positionInts[0] <= 8 {
if move[1] + positionInts[1] > 0 && move[1] + positionInts[1] <= 8 {
result++
}
}
}
return result
}
Edited by author 08.06.2017 23:12
Re: WA1 i dont understand why (golang)
Posted by
Khinel 9 Jun 2017 00:25
Nevermind. Implemented reading input with bufio scanner and it worked. It seems that i dont understand go reader.