-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathd18.go
91 lines (77 loc) · 1.46 KB
/
d18.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
)
const (
Wall string = "#"
Empty string = "."
)
type Pair struct{ x, y int }
type Map struct {
W, H int
Tiles [][]string
Keys map[string]Pair
Doors map[string]Pair
Entry Pair
}
func (m Map) At(p Pair, def string) string {
if m.H > p.y && p.y >= 0 && m.W > p.x && p.x >= 0 {
return m.Tiles[p.y][p.x]
}
return def
}
func (m Map) ShowAround(p Pair, w, h int) {
fmt.Println()
for j := 0; j < h; j++ {
for i := 0; i < w; i++ {
x, y := p.x+i-w/2, p.y+j-h/2
fmt.Print(m.Tiles[y][x])
}
fmt.Println()
}
fmt.Println()
}
func (m Map) Show() {
fmt.Println()
for j := 0; j < m.H; j++ {
for i := 0; i < m.W; i++ {
fmt.Print(m.Tiles[j][i])
}
fmt.Println()
}
fmt.Println()
}
func LoadMap(path string) Map {
raw, _ := ioutil.ReadFile(path)
lines := strings.Split(strings.Trim(string(raw), "\n "), "\n")
doors := make(map[string]Pair)
keys := make(map[string]Pair)
rows := make([][]string, 0)
var entry Pair
for j, line := range lines {
row := make([]string, 0, len(line))
for i, c := range line {
cur := string(c)
row = append(row, cur)
if c >= 'a' && c <= 'z' {
keys[cur] = Pair{i, j}
}
if c >= 'A' && c <= 'Z' {
doors[cur] = Pair{i, j}
}
if c == '@' {
entry = Pair{i, j}
}
}
rows = append(rows, row)
}
return Map{len(rows[0]), len(rows), rows, keys, doors, entry}
}
func main() {
m := LoadMap(os.Args[1])
m.Show()
fmt.Println(m)
}