Start of refactoring for reader+parser: read all

the lines before starting to parse
This commit is contained in:
Karma Riuk 2023-08-05 10:01:34 +02:00
parent 345d9267ad
commit e04aad4b77

View File

@ -26,18 +26,19 @@ func (r *TextReader) Read(filename string) (*maze.Maze, error) {
return nil, err return nil, err
} }
scanner := bufio.NewScanner(file) {
scanner.Split(bufio.ScanLines) scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
y := 0 for scanner.Scan() {
var line string line := scanner.Text()
for scanner.Scan() { lines = append(lines, line)
line = scanner.Text()
if len(lines) == 0 {
lines = make([]string, 0, len(line))
} }
file.Close()
}
for y, line := range lines {
fmt.Println(line)
for x := 1; x < len(line)-1; x++ { for x := 1; x < len(line)-1; x++ {
char := line[x] char := line[x]
var left_char, right_char, above_char byte var left_char, right_char, above_char byte
@ -76,15 +77,14 @@ func (r *TextReader) Read(filename string) (*maze.Maze, error) {
} }
} }
} }
lines = append(lines, line)
y++
} }
y--
fmt.Println(len(lines))
// Parse last line to get exit // Parse last line to get exit
for x, rune := range line { for x, rune := range lines[len(lines)-1] {
char := byte(rune) char := byte(rune)
if char == r.PathChar { if char == r.PathChar {
coords := maze.Coordinates{X: x, Y: y} coords := maze.Coordinates{X: x, Y: len(lines) - 1}
node := maze.NewNode(coords) node := maze.NewNode(coords)
r.lookupNeighbourAbove(&lines, node, &nodesByCoord, ret) r.lookupNeighbourAbove(&lines, node, &nodesByCoord, ret)
ret.Nodes = append(ret.Nodes, node) ret.Nodes = append(ret.Nodes, node)