2021 Day 20 Complete
This commit is contained in:
@@ -1,22 +1,43 @@
|
||||
package aoc
|
||||
|
||||
import "fmt"
|
||||
|
||||
type CoordByteMap struct {
|
||||
Field map[Coordinate]byte
|
||||
Height int
|
||||
Width int
|
||||
|
||||
// The Top-Left-most X/Y
|
||||
TLX, TLY int
|
||||
// The Bottom-Right-most X/Y
|
||||
BRX, BRY int
|
||||
}
|
||||
|
||||
func NewCoordByteMap() CoordByteMap {
|
||||
return CoordByteMap{
|
||||
Field: make(map[Coordinate]byte),
|
||||
}
|
||||
}
|
||||
|
||||
func StringSliceToCoordByteMap(input []string) CoordByteMap {
|
||||
ret := CoordByteMap{
|
||||
Field: make(map[Coordinate]byte),
|
||||
Height: len(input),
|
||||
Width: 0,
|
||||
}
|
||||
for y := range input {
|
||||
for x := range input[y] {
|
||||
ret.Field[Coordinate{X: x, Y: y}] = input[y][x]
|
||||
if x+1 > ret.Width {
|
||||
ret.Width = x + 1
|
||||
ret.Put(Coordinate{X: x, Y: y}, input[y][x])
|
||||
}
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
func (m *CoordByteMap) Count(b byte) int {
|
||||
var ret int
|
||||
for y := m.TLY; y <= m.BRY; y++ {
|
||||
for x := m.TLX; x <= m.BRX; x++ {
|
||||
if m.Get(Coordinate{X: x, Y: y}) == b {
|
||||
ret++
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -38,19 +59,79 @@ func (m *CoordByteMap) Opt(pos Coordinate, def byte) byte {
|
||||
}
|
||||
|
||||
func (m *CoordByteMap) Put(pos Coordinate, val byte) {
|
||||
if pos.X < m.TLX {
|
||||
m.TLX = pos.X
|
||||
}
|
||||
if pos.Y < m.TLY {
|
||||
m.TLY = pos.Y
|
||||
}
|
||||
if pos.X > m.BRX {
|
||||
m.BRX = pos.X
|
||||
}
|
||||
if pos.Y > m.BRY {
|
||||
m.BRY = pos.Y
|
||||
}
|
||||
m.Field[pos] = val
|
||||
if pos.X+1 > m.Width {
|
||||
m.Width = pos.X + 1
|
||||
m.Width = m.BRX - m.TLX + 1
|
||||
}
|
||||
if pos.Y+1 > m.Height {
|
||||
m.Height = pos.Y + 1
|
||||
m.Height = m.BRY - m.TLY + 1
|
||||
}
|
||||
}
|
||||
|
||||
func (m *CoordByteMap) GrowNorth(size int, val byte) {
|
||||
tlY := m.TLY - 1
|
||||
for x := m.TLX; x <= m.BRX; x++ {
|
||||
m.Put(Coordinate{X: x, Y: tlY}, val)
|
||||
}
|
||||
}
|
||||
func (m *CoordByteMap) GrowEast(size int, val byte) {
|
||||
brX := m.BRX + 1
|
||||
for y := m.TLY; y <= m.BRY; y++ {
|
||||
m.Put(Coordinate{X: brX, Y: y}, val)
|
||||
}
|
||||
}
|
||||
func (m *CoordByteMap) GrowSouth(size int, val byte) {
|
||||
tlY := m.BRY + 1
|
||||
for x := m.TLX; x <= m.BRX; x++ {
|
||||
m.Put(Coordinate{X: x, Y: tlY}, val)
|
||||
}
|
||||
}
|
||||
func (m *CoordByteMap) GrowWest(size int, val byte) {
|
||||
brX := m.TLX - 1
|
||||
for y := m.TLY; y <= m.BRY; y++ {
|
||||
m.Put(Coordinate{X: brX, Y: y}, val)
|
||||
}
|
||||
}
|
||||
|
||||
// This grows the map in all directions
|
||||
func (m *CoordByteMap) Grow(size int, val byte) {
|
||||
m.GrowNorth(size, val)
|
||||
m.GrowEast(size, val)
|
||||
m.GrowSouth(size, val)
|
||||
m.GrowWest(size, val)
|
||||
}
|
||||
|
||||
// Runs the passed function on every point in the map
|
||||
// returning a new bytemap
|
||||
func (m CoordByteMap) Map(l func(c Coordinate, in byte) byte) CoordByteMap {
|
||||
i := m
|
||||
for y := m.TLY; y <= m.BRY; y++ {
|
||||
for x := m.TLX; x <= m.BRX; x++ {
|
||||
c := Coordinate{X: x, Y: y}
|
||||
nV := l(c, m.Get(c))
|
||||
fmt.Println("MAP: Putting", string(nV), "at", c)
|
||||
i.Put(c, l(c, m.Get(c)))
|
||||
}
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
func (m CoordByteMap) String() string {
|
||||
var ret string
|
||||
for y := 0; y <= m.Height; y++ {
|
||||
for x := 0; x <= m.Width; x++ {
|
||||
for y := m.TLY; y <= m.BRY; y++ {
|
||||
for x := m.TLX; x <= m.BRX; x++ {
|
||||
ret = ret + string(m.Field[Coordinate{X: x, Y: y}])
|
||||
}
|
||||
ret = ret + "\n"
|
||||
|
Reference in New Issue
Block a user