Skip to content

Commit 26398c2

Browse files
committed
refactor
1 parent 381f20a commit 26398c2

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

2023/day03.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,20 @@ func main() {
3939
}
4040
for y := 0; y <= maxY; y++ {
4141
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' {
5346
break
5447
}
55-
num = 10*num + int(n-'0')
48+
num = 10*num + int(c-'0')
5649
length++
5750
}
58-
outer:
51+
if length == 0 {
52+
x++
53+
continue
54+
}
55+
var added bool
5956
for j := y - 1; j <= y+1; j++ {
6057
for i := x - 1; i <= x+length; i++ {
6158
nCoord := Coord{i, j}
@@ -66,14 +63,16 @@ func main() {
6663
if neigh >= '0' && neigh <= '9' {
6764
continue
6865
}
69-
sum += num
66+
if !added {
67+
sum += num
68+
added = true
69+
}
7070
if neigh == '*' {
7171
gears[nCoord] = append(gears[nCoord], num)
7272
}
73-
break outer
7473
}
7574
}
76-
x = x + length
75+
x += length
7776
}
7877
}
7978
fmt.Println(sum)

0 commit comments

Comments
 (0)