File tree Expand file tree Collapse file tree 1 file changed +15
-16
lines changed Expand file tree Collapse file tree 1 file changed +15
-16
lines changed Original file line number Diff line number Diff line change @@ -39,23 +39,20 @@ func main() {
39
39
}
40
40
for y := 0 ; y <= maxY ; y ++ {
41
41
for x := 0 ; x <= maxX ; {
42
- c := grid [Coord {x , y }]
43
- if c < '0' || c > '9' {
44
- x ++
45
- continue
46
- }
47
- // We've found a number. Keep reading to the right.
48
- num := int (c - '0' )
49
- length := 1
50
- for i := 1 ; ; i ++ {
51
- n := grid [Coord {x + i , y }]
52
- if n < '0' || n > '9' {
42
+ var length , num int
43
+ for {
44
+ c := grid [Coord {x + length , y }]
45
+ if c < '0' || c > '9' {
53
46
break
54
47
}
55
- num = 10 * num + int (n - '0' )
48
+ num = 10 * num + int (c - '0' )
56
49
length ++
57
50
}
58
- outer:
51
+ if length == 0 {
52
+ x ++
53
+ continue
54
+ }
55
+ var added bool
59
56
for j := y - 1 ; j <= y + 1 ; j ++ {
60
57
for i := x - 1 ; i <= x + length ; i ++ {
61
58
nCoord := Coord {i , j }
@@ -66,14 +63,16 @@ func main() {
66
63
if neigh >= '0' && neigh <= '9' {
67
64
continue
68
65
}
69
- sum += num
66
+ if ! added {
67
+ sum += num
68
+ added = true
69
+ }
70
70
if neigh == '*' {
71
71
gears [nCoord ] = append (gears [nCoord ], num )
72
72
}
73
- break outer
74
73
}
75
74
}
76
- x = x + length
75
+ x += length
77
76
}
78
77
}
79
78
fmt .Println (sum )
You can’t perform that action at this time.
0 commit comments