Compare commits
3 commits
b8e99d9d88
...
071cf40b21
Author | SHA1 | Date | |
---|---|---|---|
071cf40b21 | |||
bda2bcbb5f | |||
16791f2ae5 |
1 changed files with 64 additions and 27 deletions
|
@ -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() {
|
||||
|
@ -30,42 +24,85 @@ 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
|
||||
pred.x, pred.y = 0, 0
|
||||
nbMaisons := 1
|
||||
listeMaison := list.New()
|
||||
listeMaison.PushFront(pred)
|
||||
char, _, err := reader.ReadRune()
|
||||
|
||||
for 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++
|
||||
for char, _, err := reader.ReadRune(); err != io.EOF; {
|
||||
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 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()
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue