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,34 +30,45 @@ func coordDansListe(l *list.List, e Coord) (bool) {
return false return false
} }
func nouvCoord(l *list.List, c string, pred Coord) (Coord, bool) {
ret := false
switch c {
case "^":
pred.y++
case ">":
pred.x++
case "<":
pred.x--
case "v":
pred.y--
}
if !coordDansListe(l, pred) {
l.PushFront(pred)
ret = true
}
return pred, ret
}
func p_partie(file *os.File) { func p_partie(file *os.File) {
reader := bufio.NewReader(file) reader := bufio.NewReader(file)
var nouvMaison bool
var pred Coord var pred Coord
pred.x = 0 pred.x = 0
pred.y = 0 pred.y = 0
nbMaison := 1 nbMaisons := 1
listeMaison := list.New() listeMaison := list.New()
listeMaison.PushFront(pred) listeMaison.PushFront(pred)
for char, _, err := reader.ReadRune(); err != io.EOF; { for char, _, err := reader.ReadRune(); err != io.EOF; {
switch string(char) { pred, nouvMaison = nouvCoord(listeMaison, string(char), pred)
case "^": if nouvMaison {
pred.y++ nbMaisons++
case ">":
pred.x++
case "<":
pred.x--
case "v":
pred.y--
}
if !coordDansListe(listeMaison, pred) {
listeMaison.PushFront(pred)
nbMaison++
} }
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() {