76 lines
1.5 KiB
Go
76 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"fmt"
|
|
|
|
helpers "git.bullercodeworks.com/brian/adventofcode/helpers"
|
|
)
|
|
|
|
type Vault struct {
|
|
maxX, maxY int
|
|
vault map[helpers.Coordinate]byte
|
|
keys map[helpers.Coordinate]byte
|
|
doors map[helpers.Coordinate]byte
|
|
}
|
|
|
|
func NewVault(file string) *Vault {
|
|
inp := helpers.FileToBytes(file)
|
|
wrk := bytes.Split(inp, []byte{'\n'})
|
|
|
|
v := Vault{
|
|
vault: make(map[helpers.Coordinate]byte),
|
|
keys: make(map[helpers.Coordinate]byte),
|
|
doors: make(map[helpers.Coordinate]byte),
|
|
}
|
|
v.maxY = len(wrk)
|
|
v.maxX = len(wrk[0])
|
|
for y, yv := range wrk {
|
|
for x, xv := range yv {
|
|
v.vault[*helpers.NewCoordinate(x, y)] = xv
|
|
if isKey(xv) {
|
|
v.keys[*helpers.NewCoordinate(x, y)] = xv
|
|
} else if isDoor(xv) {
|
|
v.doors[*helpers.NewCoordinate(x, y)] = xv
|
|
}
|
|
}
|
|
}
|
|
return &v
|
|
}
|
|
|
|
func (v *Vault) findKey(door byte) (helpers.Coordinate, error) {
|
|
var r helpers.Coordinate
|
|
if !isDoor(door) {
|
|
return r, errors.New("Invalid Door")
|
|
}
|
|
for k := range v.keys {
|
|
if v.keys[k] == doorToKey(door) {
|
|
return k, nil
|
|
}
|
|
}
|
|
return r, errors.New("No key for that door")
|
|
}
|
|
|
|
func (v *Vault) findDoor(key byte) (helpers.Coordinate, error) {
|
|
var r helpers.Coordinate
|
|
if !isKey(key) {
|
|
return r, errors.New("Invalid Key")
|
|
}
|
|
for k := range v.doors {
|
|
if v.doors[k] == keyToDoor(key) {
|
|
return k, nil
|
|
}
|
|
}
|
|
return r, errors.New("No door for that key")
|
|
}
|
|
|
|
func (v *Vault) Print() {
|
|
for y := 0; y < v.maxY; y++ {
|
|
for x := 0; x < v.maxX; x++ {
|
|
fmt.Print(string(v.vault[*helpers.NewCoordinate(x, y)]))
|
|
}
|
|
fmt.Println()
|
|
}
|
|
}
|