Day 18 work, but not really progress

This commit is contained in:
Brian Buller 2019-12-18 08:59:20 -06:00
parent 7fd8294b2b
commit 45602e82c2
2 changed files with 79 additions and 45 deletions

View File

@ -1,9 +1,7 @@
package main
import (
"bytes"
"errors"
"fmt"
helpers "git.bullercodeworks.com/brian/adventofcode/helpers"
)
@ -18,32 +16,15 @@ func main() {
if helpers.GetArgNumber(1) != "" {
file = helpers.GetArgNumber(1)
}
inp := helpers.FileToBytes(file)
wrk := bytes.Split(inp, []byte{'\n'})
maxY = len(wrk)
vault = make(map[helpers.Coordinate]byte)
keys = make(map[helpers.Coordinate]byte)
doors = make(map[helpers.Coordinate]byte)
maxX = len(wrk[0])
for y, yv := range wrk {
for x, xv := range yv {
vault[*helpers.NewCoordinate(x, y)] = xv
if isKey(xv) {
keys[*helpers.NewCoordinate(x, y)] = xv
} else if isDoor(xv) {
doors[*helpers.NewCoordinate(x, y)] = xv
}
}
}
PrintVault()
v := NewVault(file)
v.Print()
}
func isDoor(b byte) bool {
return b >= 'A' && b <= 'Z'
return 'A' <= b && b <= 'Z'
}
func isKey(b byte) bool {
return b >= 'a' && b <= 'z'
return 'a' <= b && b <= 'z'
}
func keyToDoor(b byte) byte {
@ -53,25 +34,3 @@ func keyToDoor(b byte) byte {
func doorToKey(b byte) byte {
return (b - 'A') + 'a'
}
func findKey(b byte) (helpers.Coordinate, error) {
var r helpers.Coordinate
if !isDoor(b) {
return r, errors.New("Invalid Door")
}
return r, nil
}
func PrintVault() {
for y := 0; y < maxY; y++ {
for x := 0; x < maxX; x++ {
fmt.Print(string(vault[*helpers.NewCoordinate(x, y)]))
}
fmt.Println()
}
}
func c(x, y int) string {
return fmt.Sprintf("[%d, %d]", x, y)
}

75
2019/day18/vault.go Normal file
View File

@ -0,0 +1,75 @@
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()
}
}