Skip to content

Commit 970ddb9

Browse files
committed
2025 day07
1 parent 71a2b05 commit 970ddb9

File tree

6 files changed

+147
-0
lines changed

6 files changed

+147
-0
lines changed

2025/day07/.bench

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"Lines":[{"Name":"Part 1","N":1244,"NsPerOp":960015,"AllocedBytesPerOp":0,"AllocsPerOp":0,"MBPerS":0,"Measured":1,"Ord":0},{"Name":"Part 2","N":1171,"NsPerOp":1049460,"AllocedBytesPerOp":0,"AllocsPerOp":0,"MBPerS":0,"Measured":1,"Ord":0}],"Measured":1}

2025/day07/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<!-- You can add some comments here if you want to :D -->

2025/day07/main.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+
_ "embed"
5+
"os"
6+
)
7+
8+
//go:embed input.txt
9+
var input string
10+
11+
//go:embed input_test.txt
12+
var inputTest string
13+
14+
func main() {
15+
// Check argv if we use test input or not
16+
if len(os.Args) > 1 && os.Args[1] == "test" {
17+
input = inputTest
18+
}
19+
20+
answer := doPartOne(input)
21+
println(answer)
22+
23+
answer = doPartTwo(input)
24+
println(answer)
25+
}

2025/day07/main_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import "testing"
4+
5+
func BenchmarkPartOne(b *testing.B) {
6+
for n := 0; n < b.N; n++ {
7+
doPartOne(input)
8+
}
9+
}
10+
11+
func BenchmarkPartTwo(b *testing.B) {
12+
for n := 0; n < b.N; n++ {
13+
doPartTwo(input)
14+
}
15+
}

2025/day07/part1.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package main
2+
3+
import (
4+
"image"
5+
"strings"
6+
)
7+
8+
func doPartOne(input string) int {
9+
lines := strings.Split(strings.TrimSpace(input), "\n")
10+
mapper, _, S := makeImagePointMapRect(lines)
11+
12+
// Pre-allocate with reasonable capacity
13+
seen := make(map[image.Point]bool, len(lines)*len(lines[0])/4)
14+
queue := make([]image.Point, 0, 256)
15+
queue = append(queue, S)
16+
ans := 0
17+
head := 0
18+
19+
for head < len(queue) {
20+
P := queue[head]
21+
head++
22+
23+
if seen[P] {
24+
continue
25+
}
26+
seen[P] = true
27+
28+
np := P.Add(image.Point{0, 1})
29+
val, ok := mapper[np]
30+
if !ok {
31+
continue
32+
}
33+
34+
if val != '^' {
35+
queue = append(queue, np)
36+
continue
37+
}
38+
39+
ans++
40+
queue = append(queue, np.Add(image.Point{-1, 0}), np.Add(image.Point{1, 0}))
41+
}
42+
43+
return ans
44+
}
45+
46+
func makeImagePointMapRect(lines []string) (mapping map[image.Point]rune, rect image.Rectangle, S image.Point) {
47+
mapping = make(map[image.Point]rune)
48+
for y, s := range lines {
49+
for x, r := range s {
50+
mapping[image.Point{x, y}] = r
51+
if r == 'S' {
52+
S = image.Point{x, y}
53+
}
54+
}
55+
}
56+
rect = image.Rect(0, 0, len(lines[0]), len(lines))
57+
return
58+
}

2025/day07/part2.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package main
2+
3+
import (
4+
"image"
5+
"strings"
6+
)
7+
8+
type MapRect struct {
9+
m map[image.Point]rune
10+
r image.Rectangle
11+
cache map[image.Point]int
12+
}
13+
14+
func doPartTwo(input string) int {
15+
lines := strings.Split(strings.TrimSpace(input), "\n")
16+
mapper, rect, S := makeImagePointMapRect(lines)
17+
m := MapRect{
18+
m: mapper,
19+
r: rect,
20+
cache: make(map[image.Point]int),
21+
}
22+
23+
return m.score(S)
24+
}
25+
26+
func (m MapRect) score(i image.Point) int {
27+
// Check cache first
28+
if val, ok := m.cache[i]; ok {
29+
return val
30+
}
31+
32+
np := i.Add(image.Point{0, 1})
33+
if !np.In(m.r) {
34+
m.cache[i] = 1
35+
return 1
36+
}
37+
38+
var result int
39+
if m.m[np] == '^' {
40+
result = m.score(np.Add(image.Point{-1, 0})) + m.score(np.Add(image.Point{1, 0}))
41+
} else {
42+
result = m.score(np)
43+
}
44+
45+
m.cache[i] = result
46+
return result
47+
}

0 commit comments

Comments
 (0)