2022 Day 14 Complete!

This commit is contained in:
2022-12-14 11:45:19 -06:00
parent 6d22393da3
commit 194d2875a8
6 changed files with 567 additions and 2 deletions

View File

@@ -18,6 +18,27 @@ func (c Coordinate) Relative(t Coordinate) Coordinate {
return Coordinate{X: c.X + t.X, Y: c.Y + t.Y}
}
func (c *Coordinate) MoveSouth() { c.Y++ }
func (c *Coordinate) MoveNorth() { c.Y-- }
func (c *Coordinate) MoveEast() { c.X++ }
func (c *Coordinate) MoveWest() { c.X-- }
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) North() Coordinate {
return Coordinate{X: c.X, Y: c.Y - 1}
}
@@ -75,6 +96,9 @@ func CoordinateFromString(str string) *Coordinate {
c := Coordinate{}
r := strings.NewReader(str)
_, err := fmt.Fscanf(r, "[%d, %d]", &c.X, &c.Y)
if err != nil {
_, err = fmt.Fscanf(r, "%d,%d", &c.X, &c.Y)
}
if err != nil {
panic(err)
}

View File

@@ -3,6 +3,7 @@ package aoc
import (
"errors"
"fmt"
"math"
)
type CoordByteMap struct {
@@ -14,11 +15,20 @@ type CoordByteMap struct {
TLX, TLY int
// The Bottom-Right-most X/Y
BRX, BRY int
// Options for the 'String' method
StringEmptyIsSpace bool
StringEmptyByte byte
}
func NewCoordByteMap() CoordByteMap {
return CoordByteMap{
Field: make(map[Coordinate]byte),
Field: make(map[Coordinate]byte),
TLX: math.MaxInt,
TLY: math.MaxInt,
BRX: math.MinInt,
BRY: math.MinInt,
StringEmptyByte: ' ',
}
}
@@ -196,6 +206,9 @@ func (m *CoordByteMap) Put(pos Coordinate, val byte) {
m.Height = m.BRY - m.TLY + 1
}
}
func (m *CoordByteMap) Delete(pos Coordinate) {
delete(m.Field, pos)
}
func (m *CoordByteMap) GrowNorth(size int, val byte) {
tlY := m.TLY - 1
@@ -249,7 +262,11 @@ func (m CoordByteMap) String() string {
var ret string
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}])
if m.StringEmptyIsSpace {
ret = ret + string(m.Opt(Coordinate{X: x, Y: y}, m.StringEmptyByte))
} else {
ret = ret + string(m.Field[Coordinate{X: x, Y: y}])
}
}
ret = ret + "\n"
}