-
Notifications
You must be signed in to change notification settings - Fork 1
/
16.go
76 lines (71 loc) · 2 KB
/
16.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
67
68
69
70
71
72
73
74
75
76
package main
import "fmt"
import "bufio"
import "os"
type beam struct { x, y, dx, dy int }
var grid = make([]string, 0, 100)
func energized(b beam) int {
var result = make(map[struct{x, y int}]bool)
var processed = make(map[beam]bool)
var queue = make([]beam, 0, 10000)
var move = func (b beam, dx int, dy int) {
queue = append(queue, beam{b.x + dx, b.y + dy, dx, dy})
}
queue = append(queue, b)
for len(queue) > 0 {
var b = queue[0]; queue = queue[1:]
if b.x < 0 || b.x >= len(grid[0]) || b.y < 0 || b.y >= len(grid) { continue }
if processed[b] { continue }
processed[b] = true
result[struct{x, y int}{b.x, b.y}] = true
switch (grid[b.y][b.x]) {
case '.': move(b, b.dx, b.dy)
case '-':
if b.dy == 0 {
move(b, b.dx, b.dy)
} else {
move(b, 1, 0)
move(b, -1, 0)
}
case '|':
if b.dx == 0 {
move(b, b.dx, b.dy)
} else {
move(b, 0, 1)
move(b, 0, -1)
}
case '/':
if b.dx == 1 && b.dy == 0 { move(b, 0, -1) } else
if b.dx == 0 && b.dy == 1 { move(b, -1, 0) } else
if b.dx == -1 && b.dy == 0 { move(b, 0, 1) } else
if b.dx == 0 && b.dy == -1 { move(b, 1, 0) }
case '\\':
if b.dx == 1 && b.dy == 0 { move(b, 0, 1) } else
if b.dx == 0 && b.dy == 1 { move(b, 1, 0) } else
if b.dx == -1 && b.dy == 0 { move(b, 0, -1) } else
if b.dx == 0 && b.dy == -1 { move(b, -1, 0) }
default: panic("unknown symbol " + string(grid[b.y][b.x]))
}
}
return len(result)
}
func maxi(a, b int) int {
if a > b {
return a
}
return b
}
func main() {
var scanner = bufio.NewScanner(os.Stdin)
for scanner.Scan() { grid = append(grid, scanner.Text()) }
var result2 = 0
for y, _ := range grid {
result2 = maxi(result2, energized(beam{0, y, 1, 0}))
result2 = maxi(result2, energized(beam{len(grid[0]) -1, y, -1, 0}))
}
for x, _ := range grid[0] {
result2 = maxi(result2, energized(beam{x, 0, 0, 1}))
result2 = maxi(result2, energized(beam{x, len(grid) - 1, 0, -1}))
}
fmt.Println(energized(beam{0, 0, 1, 0}), result2)
}