/
ragnarok.go
64 lines (53 loc) · 1004 Bytes
/
ragnarok.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
package main
import (
"fmt"
"github.com/glendc/cgreader"
)
func GetDirection(x, y int) <-chan int {
ch := make(chan int)
go func() {
difference := x - y
switch {
case difference < 0:
ch <- -1
case difference > 0:
ch <- 1
default:
ch <- 0
}
close(ch)
}()
return ch
}
func GetDirectionLetter(a, b string, v int) string {
switch v {
case -1:
return a
case 1:
return b
default:
return ""
}
}
var TX, TY, PX, PY, E int
func Initialize(input <-chan string) {
fmt.Sscanf(<-input, "%d %d %d %d", &PX, &PY, &TX, &TY)
}
func Update(input <-chan string, output chan string) {
fmt.Sscanf(<-input, "%d", &E)
chx := GetDirection(PX, TX)
chy := GetDirection(PY, TY)
dx, dy := <-chx, <-chy
x := GetDirectionLetter("W", "E", dx)
y := GetDirectionLetter("N", "S", dy)
TX, TY = TX+dx, TY+dy
output <- y + x
}
func main() {
cgreader.RunInteractivePrograms(
"ragnarok",
cgreader.GetFileList("../../input/ragnarok_%d.txt", 4),
true,
Initialize,
Update)
}