File tree Expand file tree Collapse file tree 1 file changed +70
-0
lines changed Expand file tree Collapse file tree 1 file changed +70
-0
lines changed Original file line number Diff line number Diff line change
1
+ package main
2
+
3
+ import (
4
+ "fmt"
5
+ "os"
6
+ "strings"
7
+ )
8
+
9
+ var chars [][]string
10
+
11
+ func main () {
12
+ filenames := []string {"testInput" , "input" }
13
+ for _ , fileName := range filenames {
14
+ fileContents , err := os .ReadFile (fileName )
15
+ if err != nil {
16
+ panic (err )
17
+ }
18
+ fmt .Println (fileName )
19
+ lines := strings .Split (string (fileContents ), "\n " )
20
+ chars = make ([][]string , len (lines ))
21
+ for index , line := range lines {
22
+ lineChars := strings .Split (line , "" )
23
+ chars [index ] = lineChars
24
+ }
25
+ var xmases int
26
+ for y := 0 ; y < len (chars ); y ++ {
27
+ for x := 0 ; x < len (chars [y ]); x ++ {
28
+ xmases += countXMasAtAPosition (x , y )
29
+ }
30
+ }
31
+ println (xmases )
32
+ }
33
+ }
34
+ func countXMasAtAPosition (x int , y int ) int {
35
+ if chars [y ][x ] != "A" {
36
+ return 0
37
+ }
38
+
39
+ if x - 1 < 0 || x + 1 >= len (chars [y ]) || y - 1 < 0 || y + 1 >= len (chars ) {
40
+ return 0
41
+ }
42
+ var xmases int
43
+ //M.S
44
+ //.A.
45
+ //M.S
46
+ if chars [y - 1 ][x - 1 ] == "M" && chars [y + 1 ][x - 1 ] == "M" && chars [y + 1 ][x + 1 ] == "S" && chars [y - 1 ][x + 1 ] == "S" {
47
+ xmases ++
48
+ }
49
+ //S.M
50
+ //.A.
51
+ //S.M
52
+ if chars [y - 1 ][x - 1 ] == "S" && chars [y + 1 ][x - 1 ] == "S" && chars [y + 1 ][x + 1 ] == "M" && chars [y - 1 ][x + 1 ] == "M" {
53
+ xmases ++
54
+ }
55
+
56
+ //M.M
57
+ //.A.
58
+ //S.S
59
+ if chars [y - 1 ][x - 1 ] == "M" && chars [y + 1 ][x - 1 ] == "S" && chars [y + 1 ][x + 1 ] == "S" && chars [y - 1 ][x + 1 ] == "M" {
60
+ xmases ++
61
+ }
62
+
63
+ //S.S
64
+ //.A.
65
+ //M.M
66
+ if chars [y - 1 ][x - 1 ] == "S" && chars [y + 1 ][x - 1 ] == "M" && chars [y + 1 ][x + 1 ] == "M" && chars [y - 1 ][x + 1 ] == "S" {
67
+ xmases ++
68
+ }
69
+ return xmases
70
+ }
You can’t perform that action at this time.
0 commit comments