-
Notifications
You must be signed in to change notification settings - Fork 0
/
day4.go
156 lines (132 loc) · 2.76 KB
/
day4.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"bufio"
"os"
"time"
)
func day4() {
println("Solving AoC 2024 Day 4:")
input := loadInputDay4()
day4part1(input)
day4part2(input)
}
func loadInputDay4() []string {
defer timeTrack(time.Now(), "Loading/Parsing Input")
file, err := os.Open("inputs/day4.txt")
if err != nil {
panic(err)
}
scanner := bufio.NewScanner(file)
lines := make([]string, 0)
for scanner.Scan() {
var line = scanner.Text()
lines = append(lines, line)
}
return lines
}
var maxX int
var maxY int
func day4part1(lines []string) {
defer timeTrack(time.Now(), "Part1")
var totalFound = 0
maxX = len(lines[0])
maxY = len(lines)
for x := 0; x < maxX; x++ {
for y := 0; y < maxY; y++ {
char := lines[x][y]
if char == 'X' {
totalFound += checkXMASSpot(lines, x, y)
}
}
}
println(totalFound)
}
func checkXMASSpot(lines []string, x int, y int) int {
var found = 0
// UP
if tryXMAS(lines, x, y, 1, 0) {
found++
}
// Down
if tryXMAS(lines, x, y, -1, 0) {
found++
}
// Forward
if tryXMAS(lines, x, y, 0, 1) {
found++
}
// Backward
if tryXMAS(lines, x, y, 0, -1) {
found++
}
// Up Left
if tryXMAS(lines, x, y, -1, -1) {
found++
}
// Up Right
if tryXMAS(lines, x, y, -1, 1) {
found++
}
// Down Left
if tryXMAS(lines, x, y, 1, -1) {
found++
}
// Down Right
if tryXMAS(lines, x, y, 1, 1) {
found++
}
return found
}
func tryXMAS(lines []string, startX int, startY int, deltaX int, deltaY int) bool {
// 'X' is at [x,y]
// Search for 'M' at [x+deltaX, y+deltaY]
var nextX, nextY = startX + deltaX, startY + deltaY
if checkChar(lines, nextX, nextY, 'M') {
nextX, nextY = nextX+deltaX, nextY+deltaY
if checkChar(lines, nextX, nextY, 'A') {
nextX, nextY = nextX+deltaX, nextY+deltaY
if checkChar(lines, nextX, nextY, 'S') {
return true
}
}
}
return false
}
func checkChar(lines []string, x int, y int, char uint8) bool {
if x >= 0 && x < maxX && y >= 0 && y < maxY {
nextChar := lines[x][y]
if nextChar == char {
return true
} else {
return false
}
}
return false
}
func day4part2(lines []string) {
defer timeTrack(time.Now(), "Part2")
var totalFound = 0
maxX = len(lines[0])
maxY = len(lines)
for x := 0; x < maxX; x++ {
for y := 0; y < maxY; y++ {
char := lines[x][y]
if char == 'A' {
totalFound += checkMASSpot(lines, x, y)
}
}
}
println(totalFound)
}
func checkMASSpot(lines []string, x int, y int) int {
if x >= 1 && x < maxX-1 && y >= 1 && y < maxY-1 {
topLeft, topRight := lines[x-1][y-1], lines[x-1][y+1]
botLeft, botRight := lines[x+1][y-1], lines[x+1][y+1]
if (topLeft == 'M' && botRight == 'S') || (topLeft == 'S' && botRight == 'M') {
if (botLeft == 'M' && topRight == 'S') || (botLeft == 'S' && topRight == 'M') {
return 1
}
}
}
return 0
}