-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
56 lines (40 loc) · 928 Bytes
/
main.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
package main
import (
"fmt"
"github.com/devkvlt/aoc"
)
var grid = aoc.Lines("input")
var size = len(grid)
type Pos struct{ x, y int }
var dirs = []Pos{{0, -1}, {0, 1}, {-1, 0}, {1, 0}}
func isValid(p Pos) bool { return p.x >= 0 && p.x < size && p.y >= 0 && p.y < size }
func advance(p, dir Pos) Pos { return Pos{p.x + dir.x, p.y + dir.y} }
type PosDist struct {
pos Pos
dist int
}
func part1() {
steps := 64
reached := map[Pos]bool{}
seen := map[Pos]bool{}
start := Pos{size / 2, size / 2}
queue := []PosDist{{start, 0}}
for len(queue) > 0 {
p := queue[0].pos
d := queue[0].dist
queue = queue[1:]
if isValid(p) && grid[p.y][p.x] != '#' && !seen[p] {
seen[p] = true
if d <= steps && d%2 == 0 {
reached[p] = true
}
for _, dir := range dirs {
queue = append(queue, PosDist{advance(p, dir), d + 1})
}
}
}
fmt.Println(len(reached))
}
func main() {
part1() // 3651
}