Transformation p1 en 2 fonctions pour préparer p2

This commit is contained in:
rick 2021-04-18 12:48:36 +02:00
parent 16791f2ae5
commit bda2bcbb5f
Signed by: Rick
GPG key ID: 2B593F087240EE99

View file

@ -30,17 +30,9 @@ func coordDansListe(l *list.List, e Coord) (bool) {
return false return false
} }
func p_partie(file *os.File) { func nouvCoord(l *list.List, c string, pred Coord) (Coord, bool) {
reader := bufio.NewReader(file) ret := false
var pred Coord switch c {
pred.x = 0
pred.y = 0
nbMaison := 1
listeMaison := list.New()
listeMaison.PushFront(pred)
for char, _, err := reader.ReadRune(); err != io.EOF; {
switch string(char) {
case "^": case "^":
pred.y++ pred.y++
case ">": case ">":
@ -51,13 +43,32 @@ func p_partie(file *os.File) {
pred.y-- pred.y--
} }
if !coordDansListe(listeMaison, pred) { if !coordDansListe(l, pred) {
l.PushFront(pred)
ret = true
}
return pred, ret
}
func p_partie(file *os.File) {
reader := bufio.NewReader(file)
var nouvMaison bool
var pred Coord
pred.x = 0
pred.y = 0
nbMaisons := 1
listeMaison := list.New()
listeMaison.PushFront(pred) listeMaison.PushFront(pred)
nbMaison++
for char, _, err := reader.ReadRune(); err != io.EOF; {
pred, nouvMaison = nouvCoord(listeMaison, string(char), pred)
if nouvMaison {
nbMaisons++
} }
char, _, err = reader.ReadRune() char, _, err = reader.ReadRune()
} }
fmt.Printf("Il y a %d maisons déservies\n", nbMaison) fmt.Printf("Il y a %d maisons déservies\n", nbMaisons)
} }
func main() { func main() {