2022 Days 12 & 13 Complete!

This commit is contained in:
2022-12-13 10:08:27 -06:00
parent a4567a12c3
commit 6d22393da3
10 changed files with 1261 additions and 0 deletions

View File

@@ -42,6 +42,9 @@ func (c *Coordinate) SW() Coordinate {
func (c *Coordinate) SE() Coordinate {
return Coordinate{X: c.X + 1, Y: c.Y + 1}
}
func (c *Coordinate) GetOrthNeighbors() []Coordinate {
return []Coordinate{c.North(), c.East(), c.South(), c.West()}
}
func (c *Coordinate) GetNorthCoord() *Coordinate {
return &Coordinate{

View File

@@ -35,6 +35,24 @@ func StringSliceToCoordByteMap(input []string) CoordByteMap {
return ret
}
func (m *CoordByteMap) ToByteSlices() [][]byte {
var ret [][]byte
for y := m.TLY; y <= m.BRY; y++ {
var line []byte
for x := m.TLX; x <= m.BRX; x++ {
line = append(line, m.Get(Coordinate{X: x, Y: y}))
}
ret = append(ret, line)
}
return ret
}
// ContainsCoord returns true if the passed coordinate is in the map
func (m *CoordByteMap) ContainsCoord(c Coordinate) bool {
return c.X >= m.TLX && c.X <= m.BRX &&
c.Y >= m.TLY && c.Y <= m.BRY
}
// FindFirst searches left to right, top to bottom for the first
// instance of b.
func (m *CoordByteMap) FindFirst(b byte) (Coordinate, error) {
@@ -85,6 +103,52 @@ func (m *CoordByteMap) FindAllNot(b ...byte) map[Coordinate]byte {
return ret
}
// FindAdjacentOrth takes a coordinate and a desired byte, and returns
// all adjacent coordinates that contain that byte
func (m *CoordByteMap) FindAdjacent(st Coordinate, val byte) []Coordinate {
return m.AnyContain([]Coordinate{st.North(), st.NE(), st.East(), st.SE(), st.South(), st.SW(), st.West(), st.NW()}, val)
}
// FindAdjacentOrth takes a coordinate and a desired byte, and returns
// all adjacent coordinates that contain that byte, Orthogonally
func (m *CoordByteMap) FindAdjacentOrth(st Coordinate, val byte) []Coordinate {
return m.AnyContain([]Coordinate{st.North(), st.East(), st.South(), st.West()}, val)
}
func (m *CoordByteMap) AnyContain(check []Coordinate, val byte) []Coordinate {
var ret []Coordinate
for _, wrk := range check {
if m.Get(wrk) == val {
ret = append(ret, wrk)
}
}
return ret
}
// FindAnyAdjacentOrth takes a coordinate and a list of desired bytes, and returns
// all adjacent coordinates that contain those bytes
func (m *CoordByteMap) FindAnyAdjacent(st Coordinate, val []byte) []Coordinate {
return m.AnyContainAny([]Coordinate{st.North(), st.NE(), st.East(), st.SE(), st.South(), st.SW(), st.West(), st.NW()}, val)
}
// FindAnyAdjacentOrth takes a coordinate and a desired byte, and returns
// all adjacent coordinates that contain that byte, Orthogonally
func (m *CoordByteMap) FindAnyAdjacentOrth(st Coordinate, val []byte) []Coordinate {
return m.AnyContainAny([]Coordinate{st.North(), st.East(), st.South(), st.West()}, val)
}
func (m *CoordByteMap) AnyContainAny(check []Coordinate, val []byte) []Coordinate {
var ret []Coordinate
for _, wrk := range check {
for _, bt := range val {
if m.Get(wrk) == bt {
ret = append(ret, wrk)
}
}
}
return ret
}
func (m *CoordByteMap) Count(b byte) int {
var ret int
for y := m.TLY; y <= m.BRY; y++ {