Ajout première partie

This commit is contained in:
rick 2021-04-18 03:13:04 +02:00
parent 4bedac2831
commit 44dc331f6e
Signed by: Rick
GPG key ID: 2B593F087240EE99
2 changed files with 73 additions and 0 deletions

1
jour03/input Normal file

File diff suppressed because one or more lines are too long

72
jour03/jour3.go Normal file
View file

@ -0,0 +1,72 @@
package main
import (
"bufio"
"fmt"
"io"
"os"
"container/list"
)
type Coord struct {
x int
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() {
tmp = Coord(elem.Value.(Coord))
if tmp.x == e.x && tmp.y == e.y {
return true
}
}
return false
}
func p_partie(file *os.File) {
reader := bufio.NewReader(file)
var pred Coord
pred.x = 0
pred.y = 0
nbMaison := 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++
}
char, _, err = reader.ReadRune()
}
fmt.Printf("Il y a %d maisons déservies\n", nbMaison)
}
func main() {
inputFile, err := os.Open("input")
check(err)
p_partie(inputFile)
inputFile.Close()
}