From e04aad4b7769e1b7fb92efa99eb4dc9c8fbc71e3 Mon Sep 17 00:00:00 2001 From: Karma Riuk Date: Sat, 5 Aug 2023 10:01:34 +0200 Subject: [PATCH] Start of refactoring for reader+parser: read all the lines before starting to parse --- io/reader/text.go | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/io/reader/text.go b/io/reader/text.go index fe245da..5355b8b 100644 --- a/io/reader/text.go +++ b/io/reader/text.go @@ -26,18 +26,19 @@ func (r *TextReader) Read(filename string) (*maze.Maze, error) { return nil, err } - scanner := bufio.NewScanner(file) - scanner.Split(bufio.ScanLines) + { + scanner := bufio.NewScanner(file) + scanner.Split(bufio.ScanLines) - y := 0 - var line string - for scanner.Scan() { - line = scanner.Text() - - if len(lines) == 0 { - lines = make([]string, 0, len(line)) + for scanner.Scan() { + line := scanner.Text() + lines = append(lines, line) } + file.Close() + } + for y, line := range lines { + fmt.Println(line) for x := 1; x < len(line)-1; x++ { char := line[x] 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 - for x, rune := range line { + for x, rune := range lines[len(lines)-1] { char := byte(rune) if char == r.PathChar { - coords := maze.Coordinates{X: x, Y: y} + coords := maze.Coordinates{X: x, Y: len(lines) - 1} node := maze.NewNode(coords) r.lookupNeighbourAbove(&lines, node, &nodesByCoord, ret) ret.Nodes = append(ret.Nodes, node)