Skip to content

Commit a7c5d3f

Browse files
committed
2025 day 4
1 parent 663e84f commit a7c5d3f

File tree

4 files changed

+110
-0
lines changed

4 files changed

+110
-0
lines changed

2025/4/Makefile

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
input:
2+
http "https://adventofcode.com/2025/day/4/input" "Cookie:session=${AOC_SESSION};" >input
3+
4+
main1:
5+
go build -o main1 main1.go common.go
6+
7+
main2:
8+
go build -o main2 main2.go common.go
9+
10+
.PHONY: run1 run2 clean
11+
12+
run1: main1 input
13+
./main1 <input
14+
15+
run2: main2 input
16+
./main2 <input
17+
18+
clean:
19+
rm -f main1 main2 input
20+

2025/4/common.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"os"
6+
)
7+
8+
const Size = 136
9+
10+
type P struct{ x, y int }
11+
12+
func parseMap() map[P]struct{} {
13+
m := map[P]struct{}{}
14+
scanner := bufio.NewScanner(os.Stdin)
15+
for i := 0; scanner.Scan(); i++ {
16+
for j, c := range scanner.Text() {
17+
if c == '@' {
18+
m[P{i, j}] = struct{}{}
19+
}
20+
}
21+
}
22+
return m
23+
}
24+
25+
func adjacentRolls(m map[P]struct{}, x, y int) int {
26+
count := 0
27+
for dx := -1; dx <= 1; dx++ {
28+
for dy := -1; dy <= 1; dy++ {
29+
if dx == 0 && dy == 0 {
30+
continue
31+
}
32+
33+
nx, ny := x+dx, y+dy
34+
if nx < 0 || nx >= Size ||
35+
ny < 0 || ny >= Size {
36+
continue
37+
}
38+
39+
if _, ok := m[P{nx, ny}]; ok {
40+
count++
41+
}
42+
}
43+
}
44+
return count
45+
}

2025/4/main1.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
m := parseMap()
7+
count := 0
8+
for i := 0; i < Size; i++ {
9+
for j := 0; j < Size; j++ {
10+
if _, ok := m[P{i, j}]; ok && adjacentRolls(m, i, j) < 4 {
11+
count++
12+
}
13+
}
14+
}
15+
fmt.Println(count)
16+
}

2025/4/main2.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 := parseMap()
7+
count := 0
8+
for {
9+
toDelete := make([]P, 0)
10+
for i := 0; i < Size; i++ {
11+
for j := 0; j < Size; j++ {
12+
if _, ok := m[P{i, j}]; ok && adjacentRolls(m, i, j) < 4 {
13+
toDelete = append(toDelete, P{i, j})
14+
}
15+
}
16+
}
17+
18+
if len(toDelete) == 0 {
19+
break
20+
}
21+
22+
for _, p := range toDelete {
23+
delete(m, P{p.x, p.y})
24+
count++
25+
}
26+
}
27+
28+
fmt.Println(count)
29+
}

0 commit comments

Comments
 (0)