From 16791f2ae59cd651e17a754a48a2244c90492e53 Mon Sep 17 00:00:00 2001 From: rick <rick@gnous.eu> Date: Sun, 18 Apr 2021 12:33:01 +0200 Subject: [PATCH 1/3] Modification for --- jour03/jour3.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/jour03/jour3.go b/jour03/jour3.go index a5a228b..8b2cf99 100644 --- a/jour03/jour3.go +++ b/jour03/jour3.go @@ -38,9 +38,8 @@ func p_partie(file *os.File) { nbMaison := 1 listeMaison := list.New() listeMaison.PushFront(pred) - char, _, err := reader.ReadRune() - for err != io.EOF { + for char, _, err := reader.ReadRune(); err != io.EOF; { switch string(char) { case "^": pred.y++ From bda2bcbb5fa55413b0d4bae1a4e88b307e678e00 Mon Sep 17 00:00:00 2001 From: rick <rick@gnous.eu> Date: Sun, 18 Apr 2021 12:48:36 +0200 Subject: [PATCH 2/3] =?UTF-8?q?Transformation=20p1=20en=202=20fonctions=20?= =?UTF-8?q?pour=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() { From 071cf40b215c54bc3e497c334cc13ff624182623 Mon Sep 17 00:00:00 2001 From: rick <rick@gnous.eu> Date: Sun, 18 Apr 2021 14:51:12 +0200 Subject: [PATCH 3/3] Ajout partie 2 --- jour03/jour3.go | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/jour03/jour3.go b/jour03/jour3.go index f5fb258..1b2ad03 100644 --- a/jour03/jour3.go +++ b/jour03/jour3.go @@ -13,12 +13,6 @@ type Coord struct { y int } -func check(e error) { - if e != nil { - panic(e) - } -} - func coordDansListe(l *list.List, e Coord) (bool) { var tmp Coord for elem := l.Front(); elem != nil; elem = elem.Next() { @@ -55,8 +49,7 @@ func p_partie(file *os.File) { reader := bufio.NewReader(file) var nouvMaison bool var pred Coord - pred.x = 0 - pred.y = 0 + pred.x, pred.y = 0, 0 nbMaisons := 1 listeMaison := list.New() listeMaison.PushFront(pred) @@ -71,11 +64,45 @@ func p_partie(file *os.File) { fmt.Printf("Il y a %d maisons déservies\n", nbMaisons) } +func d_partie(file *os.File) { + reader := bufio.NewReader(file) + var nouvMaison bool + var predNoel, predRobot Coord + predNoel.x , predNoel.y, predRobot.x, predRobot.y = 0, 0, 0, 0 + nbMaisons := 1 + listeMaison := list.New() + listeMaison.PushFront(predNoel) + + char, _, err := reader.ReadRune() + i := 0 + + for err != io.EOF { + if i % 2 == 0 { + predNoel, nouvMaison = nouvCoord(listeMaison, string(char), predNoel) + } else { + predRobot, nouvMaison = nouvCoord(listeMaison, string(char), predRobot) + } + + if nouvMaison { + nbMaisons++ + } + char, _, err = reader.ReadRune() + i++ + } + fmt.Printf("Il y a %d maisons déservies\n", nbMaisons) +} + func main() { inputFile, err := os.Open("input") - check(err) + if err != nil { + panic(err) + } + fmt.Printf("Traitement partie 1…\n") p_partie(inputFile) + inputFile.Seek(0, 0) + fmt.Printf("Traitement partie 2…\n") + d_partie(inputFile) inputFile.Close() }