2021-04-18 03:13:04 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"container/list"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Coord struct {
|
|
|
|
x int
|
|
|
|
y int
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-04-18 12:48:36 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-04-18 03:13:04 +02:00
|
|
|
func p_partie(file *os.File) {
|
|
|
|
reader := bufio.NewReader(file)
|
2021-04-18 12:48:36 +02:00
|
|
|
var nouvMaison bool
|
2021-04-18 03:13:04 +02:00
|
|
|
var pred Coord
|
2021-04-18 14:51:12 +02:00
|
|
|
pred.x, pred.y = 0, 0
|
2021-04-18 12:48:36 +02:00
|
|
|
nbMaisons := 1
|
2021-04-18 03:13:04 +02:00
|
|
|
listeMaison := list.New()
|
|
|
|
listeMaison.PushFront(pred)
|
|
|
|
|
2021-04-18 12:33:01 +02:00
|
|
|
for char, _, err := reader.ReadRune(); err != io.EOF; {
|
2021-04-18 12:48:36 +02:00
|
|
|
pred, nouvMaison = nouvCoord(listeMaison, string(char), pred)
|
|
|
|
if nouvMaison {
|
|
|
|
nbMaisons++
|
2021-04-18 03:13:04 +02:00
|
|
|
}
|
|
|
|
char, _, err = reader.ReadRune()
|
|
|
|
}
|
2021-04-18 12:48:36 +02:00
|
|
|
fmt.Printf("Il y a %d maisons déservies\n", nbMaisons)
|
2021-04-18 03:13:04 +02:00
|
|
|
}
|
|
|
|
|
2021-04-18 14:51:12 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2021-04-18 03:13:04 +02:00
|
|
|
func main() {
|
|
|
|
inputFile, err := os.Open("input")
|
2021-04-18 14:51:12 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2021-04-18 03:13:04 +02:00
|
|
|
|
2021-04-18 14:51:12 +02:00
|
|
|
fmt.Printf("Traitement partie 1…\n")
|
2021-04-18 03:13:04 +02:00
|
|
|
p_partie(inputFile)
|
2021-04-18 14:51:12 +02:00
|
|
|
inputFile.Seek(0, 0)
|
|
|
|
fmt.Printf("Traitement partie 2…\n")
|
|
|
|
d_partie(inputFile)
|
2021-04-18 03:13:04 +02:00
|
|
|
|
|
|
|
inputFile.Close()
|
|
|
|
}
|
|
|
|
|