2024 Day 4 Complete!

This commit is contained in:
2024-12-04 08:14:00 -06:00
parent 9a8de11888
commit b852b3951a
6 changed files with 379 additions and 2 deletions

View File

@@ -26,24 +26,29 @@ func (c *Coordinate) MoveNE() {
c.X++
c.Y--
}
func (c *Coordinate) MoveSE() {
c.X++
c.Y++
}
func (c *Coordinate) MoveSW() {
c.X--
c.Y++
}
func (c *Coordinate) MoveNW() {
c.X--
c.Y--
}
func (c Coordinate) Add(o Coordinate) Coordinate {
return Coordinate{
X: c.X + o.X,
Y: c.Y + o.Y,
}
}
func (c Coordinate) Mul(by int) Coordinate {
return Coordinate{
X: c.X * by,
@@ -54,36 +59,46 @@ func (c Coordinate) Mul(by int) Coordinate {
func (c Coordinate) North() Coordinate {
return Coordinate{X: c.X, Y: c.Y - 1}
}
func (c Coordinate) East() Coordinate {
return Coordinate{X: c.X + 1, Y: c.Y}
}
func (c Coordinate) South() Coordinate {
return Coordinate{X: c.X, Y: c.Y + 1}
}
func (c Coordinate) West() Coordinate {
return Coordinate{X: c.X - 1, Y: c.Y}
}
func (c *Coordinate) NW() Coordinate {
return Coordinate{X: c.X - 1, Y: c.Y - 1}
}
func (c *Coordinate) NE() Coordinate {
return Coordinate{X: c.X + 1, Y: c.Y - 1}
}
func (c *Coordinate) SW() Coordinate {
return Coordinate{X: c.X - 1, Y: c.Y + 1}
}
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) GetAllNeighbors() []Coordinate {
return []Coordinate{
c.North(), c.NE(),
c.East(), c.SE(),
c.South(), c.SW(),
c.West(), c.NW()}
c.West(), c.NW(),
}
}
func (c *Coordinate) GetNorthCoord() *Coordinate {
@@ -92,18 +107,21 @@ func (c *Coordinate) GetNorthCoord() *Coordinate {
Y: c.Y - 1,
}
}
func (c *Coordinate) GetEastCoord() *Coordinate {
return &Coordinate{
X: c.X + 1,
Y: c.Y,
}
}
func (c *Coordinate) GetSouthCoord() *Coordinate {
return &Coordinate{
X: c.X,
Y: c.Y + 1,
}
}
func (c *Coordinate) GetWestCoord() *Coordinate {
return &Coordinate{
X: c.X - 1,
@@ -143,6 +161,7 @@ func (c Coordinate) Distance(t Coordinate) int {
func (c Coordinate) Equals(c2 Coordinate) bool {
return c.X == c2.X && c.Y == c2.Y
}
func (c Coordinate) Adjacent(c2 Coordinate) bool {
return c2.Equals(c.North()) ||
c2.Equals(c.NE()) ||
@@ -163,6 +182,7 @@ func GetHighestY(list ...Coordinate) int {
}
return top
}
func GetLowestY(list ...Coordinate) int {
bot := math.MaxInt
for i := range list {
@@ -172,6 +192,7 @@ func GetLowestY(list ...Coordinate) int {
}
return bot
}
func GetLowestX(list ...Coordinate) int {
bot := math.MaxInt
for i := range list {
@@ -181,6 +202,7 @@ func GetLowestX(list ...Coordinate) int {
}
return bot
}
func GetHighestX(list ...Coordinate) int {
top := math.MinInt
for i := range list {

View File

@@ -49,9 +49,11 @@ func StringSliceToCoordByteMap(input []string) CoordByteMap {
func (m *CoordByteMap) RowCount() int {
return m.BRY - m.TLY
}
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
@@ -62,6 +64,7 @@ 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
@@ -72,12 +75,14 @@ func (m *CoordByteMap) GetCol(x int) []byte {
}
return resp
}
func (m *CoordByteMap) AddRow(row []byte) {
y := m.BRY + 1
for x := 0; x < len(row); x++ {
m.Put(Coordinate{X: x + m.TLX, Y: y}, row[x])
}
}
func (m *CoordByteMap) AddCol(col []byte) {
x := m.BRX + 1
for y := 0; y < len(col); y++ {
@@ -142,9 +147,11 @@ func (m *CoordByteMap) Delete(pos Coordinate) {
func (m *CoordByteMap) Height() int {
return m.BRY - m.TLY
}
func (m *CoordByteMap) Width() int {
return m.BRX - m.TLX
}
func (m *CoordByteMap) ReplaceAll(o, n byte) {
for y := m.TLY; y <= m.BRY; y++ {
for x := m.TLX; x <= m.BRX; x++ {
@@ -238,7 +245,7 @@ func (m *CoordByteMap) FindFirst(b byte) (Coordinate, error) {
}
}
}
return Coordinate{}, errors.New("Not Found")
return Coordinate{}, errors.New("not found")
}
// FindLast searches right to left, bottom to top for the last
@@ -355,18 +362,21 @@ func (m *CoordByteMap) GrowNorth(size int, val byte) {
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++ {
@@ -396,6 +406,7 @@ func (m CoordByteMap) Map(l func(c Coordinate, in byte) byte) CoordByteMap {
}
return i
}
func (m CoordByteMap) RowAsString(y int) string {
var res string
for x := m.TLX; x <= m.BRX; x++ {
@@ -403,6 +414,7 @@ func (m CoordByteMap) RowAsString(y int) string {
}
return res
}
func (m CoordByteMap) ColAsString(x int) string {
var res string
for y := m.TLY; y <= m.BRY; y++ {
@@ -470,6 +482,7 @@ func (m *Coord3dByteMap) Get(pos Coordinate3d) byte {
}
return m.Field[pos]
}
func (m *Coord3dByteMap) Opt(pos Coordinate3d, def byte) byte {
if v, ok := m.Field[pos]; ok {
return v