-
Notifications
You must be signed in to change notification settings - Fork 0
/
advent8-1.go
59 lines (53 loc) · 1.5 KB
/
advent8-1.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package main
import("fmt";"bufio";"os";"strings")
type dirs struct{
left string
right string
}
func main(){
fmt.Println("Input map:")
starter:=1
var dir string
var directions = make(map[string]dirs)
reader:= bufio.NewReader(os.Stdin)
for{
s,_ := reader.ReadString('\n')
s=strings.TrimSpace(s)
if(s=="end"){break}
if(starter==1){
dir=s
starter=0
}else if(s!=""){
s = strings.ReplaceAll(s," ","")
ss := strings.Split(s,"=")
key := ss[0]
sss := strings.ReplaceAll(ss[1],"(","")
sss = strings.ReplaceAll(sss,")","")
vals := strings.Split(sss,",")
directions[key] = dirs{left:vals[0],right:vals[1]}
}
}
dest := "ZZZ"
start := "AAA"
movements := 0
fmt.Println("START:",start)
for{
if(start!=dest){
fmt.Printf("%s ",start)
for _,c := range dir{
if(c=='L'){
start=directions[start].left
fmt.Printf("L:%s ",start)
movements = movements+1
}else if(c=='R'){
start=directions[start].right
fmt.Printf("R:%s ",start)
movements = movements+1
}
}
fmt.Printf("%d\n",movements)
}else{break}
}
fmt.Println("DEST:",start)
fmt.Println("Total moves from AAA to ZZZ:",movements)
}