Add Files of first version
This commit is contained in:
86
parse.go
Normal file
86
parse.go
Normal file
@@ -0,0 +1,86 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func parseMap(path string) Map {
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
file.Close()
|
||||
}()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
|
||||
scanner.Scan()
|
||||
line := scanner.Text()
|
||||
amountPaths, err := strconv.ParseInt(line, 10, 8)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
paths := parsePaths(scanner, int(amountPaths))
|
||||
scanner.Scan()
|
||||
line = scanner.Text()
|
||||
amountPonds, err := strconv.ParseInt(line, 10, 8)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
ponds := parsePonds(scanner, int(amountPonds))
|
||||
|
||||
return Map{paths, ponds}
|
||||
}
|
||||
|
||||
func parsePaths(scanner *bufio.Scanner, amount int) []Path {
|
||||
paths := make([]Path, amount)
|
||||
for i := 0; i < amount && scanner.Scan(); i++ {
|
||||
line := scanner.Text()
|
||||
numbers := strings.Split(line, " ")
|
||||
startx, _ := strconv.ParseInt(numbers[0], 10, 8)
|
||||
starty, _ := strconv.ParseInt(numbers[1], 10, 8)
|
||||
endx, _ := strconv.ParseInt(numbers[2], 10, 8)
|
||||
endy, _ := strconv.ParseInt(numbers[3], 10, 8)
|
||||
|
||||
startc := Coordinate{int(startx), int(starty)}
|
||||
endc := Coordinate{int(endx), int(endy)}
|
||||
paths[i] = Path{startc, endc}
|
||||
}
|
||||
if scanner.Err() != nil {
|
||||
panic(scanner.Err())
|
||||
}
|
||||
|
||||
return paths
|
||||
}
|
||||
|
||||
func parsePonds(scanner *bufio.Scanner, amount int) []Pond {
|
||||
ponds := make([]Pond, amount)
|
||||
for i := 0; i < amount && scanner.Scan(); i++ {
|
||||
line := scanner.Text()
|
||||
amountVertices, err := strconv.ParseInt(line, 10, 8)
|
||||
vertices := make([]Coordinate, amountVertices)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
for j := 0; j < int(amountVertices) && scanner.Scan(); j++ {
|
||||
line := scanner.Text()
|
||||
numbers := strings.Split(line, " ")
|
||||
vx, _ := strconv.ParseInt(numbers[0], 10, 8)
|
||||
vy, _ := strconv.ParseInt(numbers[1], 10, 8)
|
||||
vertices[j] = Coordinate{int(vx), int(vy)}
|
||||
}
|
||||
if scanner.Err() != nil {
|
||||
panic(scanner.Err())
|
||||
}
|
||||
ponds[i] = Pond{vertices}
|
||||
}
|
||||
if scanner.Err() != nil {
|
||||
panic(scanner.Err())
|
||||
}
|
||||
return ponds
|
||||
}
|
||||
Reference in New Issue
Block a user