Skip to content

Commit 2ce81d1

Browse files
committed
2024 day 4
1 parent 822ec13 commit 2ce81d1

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

2024/4/Makefile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
main1:
2+
go build -o main1 main1.go common.go
3+
4+
main2:
5+
go build -o main2 main2.go common.go
6+
7+
.PHONY: run1 run2 clean
8+
9+
run1: main1
10+
./main1 <input
11+
12+
run2: main2
13+
./main2 <input
14+
15+
clean:
16+
rm -f main1 main2
17+

2024/4/common.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"os"
6+
)
7+
8+
const Size = 140
9+
10+
type P struct{ x, y int }
11+
12+
func (p P) Add(d P, times int) P { return P{p.x + times*d.x, p.y + times*d.y} }
13+
14+
func input() map[P]byte {
15+
scanner := bufio.NewScanner(os.Stdin)
16+
17+
out := map[P]byte{}
18+
for i := 0; scanner.Scan(); i++ {
19+
line := scanner.Text()
20+
for j := 0; j < len(line); j++ {
21+
out[P{i, j}] = line[j]
22+
}
23+
}
24+
return out
25+
}

2024/4/main1.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
m := input()
7+
count := 0
8+
for x := 0; x < Size; x++ {
9+
for y := 0; y < Size; y++ {
10+
for dx := -1; dx <= 1; dx++ {
11+
for dy := -1; dy <= 1; dy++ {
12+
if dx != 0 || dy != 0 {
13+
if findXMas(m, P{x, y}, P{dx, dy}) {
14+
count++
15+
}
16+
}
17+
}
18+
}
19+
}
20+
}
21+
fmt.Println(count)
22+
}
23+
24+
func findXMas(m map[P]byte, start, delta P) bool {
25+
return m[start] == 'X' &&
26+
m[start.Add(delta, 1)] == 'M' &&
27+
m[start.Add(delta, 2)] == 'A' &&
28+
m[start.Add(delta, 3)] == 'S'
29+
}

2024/4/main2.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
m := input()
7+
8+
count := 0
9+
for x := 0; x < Size; x++ {
10+
for y := 0; y < Size; y++ {
11+
if findCrossMas(m, P{x, y}) {
12+
count++
13+
}
14+
}
15+
}
16+
fmt.Println(count)
17+
}
18+
19+
func findCrossMas(m map[P]byte, start P) bool {
20+
if m[start] != 'A' {
21+
return false
22+
}
23+
s := string([]byte{
24+
m[P{start.x - 1, start.y - 1}],
25+
m[P{start.x - 1, start.y + 1}],
26+
m[P{start.x + 1, start.y + 1}],
27+
m[P{start.x + 1, start.y - 1}],
28+
})
29+
return s == "MMSS" || s == "MSSM" || s == "SSMM" || s == "SMMS"
30+
}

0 commit comments

Comments
 (0)