-
Notifications
You must be signed in to change notification settings - Fork 0
/
part_1.go
65 lines (52 loc) · 1021 Bytes
/
part_1.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
package main
import (
"bufio"
"fmt"
"os"
)
type Point struct {
R int
C int
S int
}
func SixtyFourElfSteps() {
file, _ := os.Open("input.txt")
scanner := bufio.NewScanner(file)
var grid []string
for scanner.Scan() {
grid = append(grid, scanner.Text())
}
sr, sc := 0, 0
for r, row := range grid {
for c, ch := range row {
if ch == 'S' {
sr, sc = r, c
break
}
}
}
ans := make(map[Point]bool)
seen := make(map[Point]bool)
q := []Point{{sr, sc, 64}}
for len(q) > 0 {
current := q[0]
q = q[1:]
r, c, s := current.R, current.C, current.S
if s%2 == 0 {
ans[current] = true
}
if s == 0 {
continue
}
moves := [][2]int{{1, 0}, {-1, 0}, {0, 1}, {0, -1}}
for _, move := range moves {
nr, nc := r+move[0], c+move[1]
if nr < 0 || nr >= len(grid) || nc < 0 || nc >= len(grid[0]) || grid[nr][nc] == '#' || seen[Point{nr, nc, 0}] {
continue
}
seen[Point{nr, nc, 0}] = true
q = append(q, Point{nr, nc, s - 1})
}
}
fmt.Println(len(ans) - 1)
}