From bda2bcbb5fa55413b0d4bae1a4e88b307e678e00 Mon Sep 17 00:00:00 2001 From: rick Date: Sun, 18 Apr 2021 12:48:36 +0200 Subject: [PATCH] =?UTF-8?q?Transformation=20p1=20en=202=20fonctions=20pour?= =?UTF-8?q?=20pr=C3=A9parer=20p2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jour03/jour3.go | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) diff --git a/jour03/jour3.go b/jour03/jour3.go index 8b2cf99..f5fb258 100644 --- a/jour03/jour3.go +++ b/jour03/jour3.go @@ -30,34 +30,45 @@ func coordDansListe(l *list.List, e Coord) (bool) { 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) { reader := bufio.NewReader(file) + var nouvMaison bool var pred Coord pred.x = 0 pred.y = 0 - nbMaison := 1 + nbMaisons := 1 listeMaison := list.New() listeMaison.PushFront(pred) for char, _, err := reader.ReadRune(); err != io.EOF; { - switch string(char) { - case "^": - pred.y++ - case ">": - pred.x++ - case "<": - pred.x-- - case "v": - pred.y-- - } - - if !coordDansListe(listeMaison, pred) { - listeMaison.PushFront(pred) - nbMaison++ + pred, nouvMaison = nouvCoord(listeMaison, string(char), pred) + if nouvMaison { + nbMaisons++ } 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() {