6
6
"os"
7
7
)
8
8
9
- const NEEDLE = "XMAS "
9
+ const NEEDLE = "MAS "
10
10
11
11
type point struct {
12
12
y int
@@ -35,16 +35,16 @@ func generateMatchPoints(at point, dy, dx int) []point {
35
35
return result
36
36
}
37
37
38
+ func transpose (p point , dy , dx int ) point {
39
+ return point {y : p .y + dy , x : p .x + dx }
40
+ }
41
+
38
42
func matchPointGenerators () []func (point ) []point {
39
43
return []func (point ) []point {
40
- func (p point ) []point { return generateMatchPoints (p , - 1 , 0 ) },
41
- func (p point ) []point { return generateMatchPoints (p , - 1 , - 1 ) },
42
- func (p point ) []point { return generateMatchPoints (p , 0 , - 1 ) },
43
- func (p point ) []point { return generateMatchPoints (p , 1 , - 1 ) },
44
- func (p point ) []point { return generateMatchPoints (p , 1 , 0 ) },
45
- func (p point ) []point { return generateMatchPoints (p , 1 , 1 ) },
46
- func (p point ) []point { return generateMatchPoints (p , 0 , 1 ) },
47
- func (p point ) []point { return generateMatchPoints (p , - 1 , 1 ) },
44
+ func (p point ) []point { return generateMatchPoints (transpose (p , - 1 , - 1 ), 1 , 1 ) },
45
+ func (p point ) []point { return generateMatchPoints (transpose (p , 1 , 1 ), - 1 , - 1 ) },
46
+ func (p point ) []point { return generateMatchPoints (transpose (p , - 1 , 1 ), 1 , - 1 ) },
47
+ func (p point ) []point { return generateMatchPoints (transpose (p , 1 , - 1 ), - 1 , 1 ) },
48
48
}
49
49
}
50
50
@@ -64,13 +64,21 @@ func countMatchesFrom(source point, lines []string, matches [][]byte) int {
64
64
if ! isAMatch (matchPoints , lines ) {
65
65
continue
66
66
}
67
-
67
+ matchCount ++
68
+ }
69
+ if matchCount != 2 {
70
+ return 0
71
+ }
72
+ for _ , generator := range matchPointGenerators () {
73
+ matchPoints := generator (source )
74
+ if ! isAMatch (matchPoints , lines ) {
75
+ continue
76
+ }
68
77
for i , p := range matchPoints {
69
78
matches [p.y ][p.x ] = NEEDLE [i ]
70
79
}
71
- matchCount ++
72
80
}
73
- return matchCount
81
+ return 1
74
82
}
75
83
76
84
func main () {
@@ -98,8 +106,8 @@ func main() {
98
106
}
99
107
}
100
108
101
- fmt .Fprintf (os .Stderr , "matches: %d\n " , matchCount )
102
109
for _ , lineMatches := range matches {
103
110
fmt .Printf ("%s\n " , string (lineMatches ))
104
111
}
112
+ fmt .Fprintf (os .Stderr , "matches: %d\n " , matchCount )
105
113
}
0 commit comments