-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadvent3.go
85 lines (74 loc) · 2.25 KB
/
advent3.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import("fmt";"bufio";"os";"strings";"strconv")
type val struct{
sum, r1, c1, c2 int
}
func main(){
fmt.Println("Input Engine Matrix:")
var sarr []string
var narr []val
rows:=-1
reader := bufio.NewReader(os.Stdin)
for{
s,_ := reader.ReadString('\n')
s=strings.TrimSpace(s)
if(s=="end"){break}
sarr = append(sarr,s)
rows=rows+1
}
for row,ss := range sarr{
inside:=0
var asset = val{sum:0,r1:row,c1:-1,c2:-1}
for col,c := range ss{
intv,err := strconv.Atoi(string(c))
if err==nil {
//fmt.Printf("r%dc%d ",row,col)
if(inside==0){asset.c1=col}
asset.sum = (asset.sum*10)+intv
inside=1
}else{
//fmt.Printf("-")
if(inside==1){
asset.c2 = col-1
narr = append(narr,asset)
}
inside=0
asset = val{sum:0,r1:row,c1:-1,c2:-1}
}
}
//fmt.Printf("\n")
if(inside==1){
asset.c2 = len(ss)-1
narr = append(narr,asset)
}
}
// type val struct{
// sum, r1, c1, c2 int
// }
summer := 0
grsum := 0
for row,ss := range sarr{
for col,c := range ss{
_,err := strconv.Atoi(string(c))
if(err!=nil&&c!='.'){
gr:=1
adj:=0
//fmt.Println(string(c),row,col)
for _,part := range narr{
if(part.r1==row-1||part.r1==row||part.r1==row+1){
if(part.c1==col-1||part.c1==col||part.c1==col+1||part.c2==col-1||part.c2==col||part.c2==col+1){
summer=summer+part.sum
//fmt.Println(string(c),row,col,part)
adj=adj+1
gr=gr*part.sum
}
}
}
if(adj==2&&c=='*'){grsum=grsum+gr}
}
}
}
fmt.Println("Total Enginer Schema:",summer)
fmt.Println("Gear Ratio Sum:",grsum)
//fmt.Println(narr)
}