Skip to content

Commit bb4ed90

Browse files
committed
2024 day 4 pt2
1 parent 82cbc01 commit bb4ed90

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

2024/day4/day4_part2.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy