-
Notifications
You must be signed in to change notification settings - Fork 0
/
day25.go
66 lines (59 loc) · 1.05 KB
/
day25.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
66
package main
import (
"bufio"
"bytes"
"encoding/gob"
"fmt"
"os"
)
func deepcopy(out, in interface{}) {
buf := new(bytes.Buffer)
gob.NewEncoder(buf).Encode(in)
gob.NewDecoder(buf).Decode(out)
}
func stepAlong(grid [][]byte, c byte, di, dj int) bool {
n := len(grid)
m := len(grid[0])
var cpy [][]byte
deepcopy(&cpy, grid)
upd := false
for i := 0; i < n; i++ {
for j := 0; j < m; j++ {
if grid[i][j] == c && cpy[(i+di)%n][(j+dj)%m] == '.' {
upd = true
grid[i][j] = '.'
grid[(i+di)%n][(j+dj)%m] = '*'
}
}
}
for i := 0; i < n; i++ {
for j := 0; j < m; j++ {
if grid[i][j] == '*' {
grid[i][j] = c
}
}
}
return upd
}
func step(grid [][]byte) bool {
d := stepAlong(grid, '>', 0, 1)
r := stepAlong(grid, 'v', 1, 0)
return d || r
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
var grid [][]byte
for scanner.Scan() {
line := scanner.Text()
row := make([]byte, len(line))
for j := range row {
row[j] = line[j]
}
grid = append(grid, row)
}
ans := 1
for step(grid) {
ans++
}
fmt.Println(ans)
}