@@ -7,25 +7,25 @@ import (
7
7
)
8
8
9
9
var grid [][]byte
10
- var history map [ byte ][]position
10
+ var visited [ ][]bool
11
11
12
12
func main () {
13
13
file := util .FileFromArgs ()
14
14
scan := bufio .NewScanner (file )
15
15
16
16
for scan .Scan () {
17
17
grid = append (grid , scan .Bytes ())
18
+ visited = append (visited , make ([]bool , len (grid [len (grid )- 1 ])))
18
19
}
19
20
20
- history = map [byte ][]position {}
21
21
var area , perimeter , sum int
22
22
for y := range grid {
23
23
for x := range grid [y ] {
24
24
area , perimeter = measure (x , y )
25
25
sum += area * perimeter
26
26
27
27
// Debug: print the found area
28
- if area > 0 {
28
+ if area != 0 {
29
29
fmt .Println (string (grid [y ][x ]), area , perimeter )
30
30
}
31
31
}
@@ -34,27 +34,14 @@ func main() {
34
34
fmt .Println (sum )
35
35
}
36
36
37
- type position struct {
38
- X , Y int
39
- }
40
-
41
37
// measure returns the area and peremiter this position is part of
42
38
func measure (x , y int ) (int , int ) {
43
- // Have we passed here yet?
44
- if _ , exists := history [grid [y ][x ]]; exists {
45
- for i := range history [grid [y ][x ]] {
46
- if history [grid [y ][x ]][i ].X == x && history [grid [y ][x ]][i ].Y == y {
47
- return 0 , 0
48
- }
49
- }
50
-
51
- history [grid [y ][x ]] = append (history [grid [y ][x ]], position {x , y })
52
- } else {
53
- history [grid [y ][x ]] = []position {
54
- {x , y },
55
- }
39
+ if visited [y ][x ] {
40
+ return 0 , 0
56
41
}
57
42
43
+ visited [y ][x ] = true
44
+
58
45
// Measure shapes
59
46
area , perimeter := 1 , 0
60
47
var dA , dP int
0 commit comments