package two import ( "bufio" "io" "strings" log "github.com/sirupsen/logrus" ) func parseLines(r io.Reader) (*graph, string, error) { graph := newGraph() var directions []string var sb strings.Builder var inDirections bool var linecount int scanner := bufio.NewScanner(r) for scanner.Scan() { line := scanner.Text() if len(line) == 0 { inDirections = true continue } if inDirections { directions = append(directions, line) } else { for _, r := range line { switch r { case '#': sb.WriteRune('#') sb.WriteRune('#') case 'O': sb.WriteRune('[') sb.WriteRune(']') case '@': sb.WriteRune('@') sb.WriteRune('.') default: sb.WriteRune('.') sb.WriteRune('.') } } indx := strings.Index(sb.String(), "@") if indx != -1 { log.Debugf("adding start point at (%d, %d)", indx, linecount) graph.robot = newPoint(indx, linecount) } graph.data = append(graph.data, sb.String()) sb.Reset() } linecount++ } if err := scanner.Err(); err != nil { return nil, "", err } return graph, strings.Join(directions, ""), nil } func replaceAtIndex(s string, r rune, i int) string { out := []rune(s) out[i] = r return string(out) }