2023 Day 21 Complete

This commit is contained in:
2023-12-26 10:25:53 -06:00
parent b393c79247
commit 3d42462d4c
4 changed files with 230 additions and 169 deletions

View File

@@ -17,6 +17,10 @@ type CoordByteMap struct {
// Options for the 'String' method
StringEmptyIsSpace bool
StringEmptyByte byte
// If 'Repeats' is true, then any coordinate request will be
// modded by BRX/BRY
Repeats bool
}
func NewCoordByteMap() CoordByteMap {
@@ -49,6 +53,9 @@ func (m *CoordByteMap) ColCount() int {
return m.BRX - m.TLX
}
func (m *CoordByteMap) GetRow(y int) []byte {
if m.Repeats {
y = (y + m.BRY) % m.BRY
}
var resp []byte
for x := m.TLX; x <= m.BRX; x++ {
resp = append(resp, m.Get(Coordinate{X: x, Y: y}))
@@ -56,6 +63,9 @@ func (m *CoordByteMap) GetRow(y int) []byte {
return resp
}
func (m *CoordByteMap) GetCol(x int) []byte {
if m.Repeats {
x = (x + m.BRX) % m.BRX
}
var resp []byte
for y := m.TLY; y <= m.BRY; y++ {
resp = append(resp, m.Get(Coordinate{X: x, Y: y}))
@@ -76,10 +86,18 @@ func (m *CoordByteMap) AddCol(col []byte) {
}
func (m *CoordByteMap) GetPos(x, y int) byte {
if m.Repeats {
x = (x + m.BRX) % m.BRX
y = (y + m.BRY) % m.BRY
}
return m.Get(Coordinate{X: x, Y: y})
}
func (m *CoordByteMap) Get(pos Coordinate) byte {
if m.Repeats {
pos.X = (pos.X + m.BRX) % m.BRX
pos.Y = (pos.Y + m.BRY) % m.BRY
}
if v, ok := m.Field[pos]; ok {
return v
}
@@ -87,6 +105,10 @@ func (m *CoordByteMap) Get(pos Coordinate) byte {
}
func (m *CoordByteMap) Opt(pos Coordinate, def byte) byte {
if m.Repeats {
pos.X = (pos.X + m.BRX) % m.BRX
pos.Y = (pos.Y + m.BRY) % m.BRY
}
if v, ok := m.Field[pos]; ok {
return v
}
@@ -110,6 +132,10 @@ func (m *CoordByteMap) Put(pos Coordinate, val byte) {
}
func (m *CoordByteMap) Delete(pos Coordinate) {
if m.Repeats {
pos.X = (pos.X + m.BRX) % m.BRX
pos.Y = (pos.Y + m.BRY) % m.BRY
}
delete(m.Field, pos)
}
@@ -135,6 +161,10 @@ func (m *CoordByteMap) ToByteSlices() [][]byte {
// ContainsCoord returns true if the passed coordinate is within the bounds
// of the map
func (m *CoordByteMap) ContainsCoord(c Coordinate) bool {
if m.Repeats {
c.X = (c.X + m.BRX) % m.BRX
c.Y = (c.Y + m.BRY) % m.BRY
}
return c.X >= m.TLX && c.X <= m.BRX &&
c.Y >= m.TLY && c.Y <= m.BRY
}