matrix

package
v0.0.0-...-7d7b408 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 25, 2024 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Left      = Vec{X: -1, Y: 0}
	Right     = Vec{X: 1, Y: 0}
	Up        = Vec{X: 0, Y: -1}
	Down      = Vec{X: 0, Y: 1}
	UpLeft    = Up.Add(Left)
	UpRight   = Up.Add(Right)
	DownLeft  = Down.Add(Left)
	DownRight = Down.Add(Right)
)
View Source
var ErrInconsistentGeometry = fmt.Errorf("inconsistent geometry")
View Source
var VecName = map[Vec]string{
	Left:      "Left",
	Right:     "Right",
	Up:        "Up",
	Down:      "Down",
	UpLeft:    "UpLeft",
	UpRight:   "UpRight",
	DownLeft:  "DownLeft",
	DownRight: "DownRight",
}

Functions

func SMatrix

func SMatrix(m *Matrix[byte]) string

Types

type Coord

type Coord XY

func (Coord) Add

func (c Coord) Add(v Vec) Coord

func (Coord) Clone

func (c Coord) Clone(v Vec) Coord

func (Coord) Down

func (c Coord) Down() Coord

func (Coord) Left

func (c Coord) Left() Coord

func (*Coord) Move

func (c *Coord) Move(v Vec)

func (Coord) Right

func (c Coord) Right() Coord

func (Coord) String

func (c Coord) String() string

func (Coord) Sub

func (c Coord) Sub(c2 Coord) Vec

func (Coord) Up

func (c Coord) Up() Coord

type Matrix

type Matrix[T comparable] struct {
	Data []T
	Size Vec
}

func New

func New[T comparable](x, y int) *Matrix[T]

New allocates a new Matrix of size x * y.

func NewFromReader

func NewFromReader(input io.Reader) (*Matrix[byte], error)

NewFromReader reads lines from the Reader and builds a new Matrix[byte] where each line is a row of the matrix, starting from the top. Each line must be of the same length, else an error will be returned.

func (*Matrix[T]) At

func (m *Matrix[T]) At(x, y int) T

At returns the value present at a coordinate. It's the responsibility of the user to check that the coordinate exists within the Matrix with In.

func (*Matrix[T]) AtCoord

func (m *Matrix[T]) AtCoord(c Coord) T

AtCoord returns the value present at a coordinate. It's the responsibility of the user to check that the coordinate exists within the Matrix with InCoord.

func (*Matrix[T]) Clone

func (m *Matrix[T]) Clone() *Matrix[T]

func (*Matrix[T]) Coords

func (m *Matrix[T]) Coords() iter.Seq[Coord]

Coords will return an iterator over all the coordinates that exist in the matrix.

func (*Matrix[T]) Copy

func (m *Matrix[T]) Copy(src *Matrix[T]) error

Copy copies data from a matrix into the current matrix. If geometries are different, an error will be returned.

func (*Matrix[T]) Count

func (m *Matrix[T]) Count(value T) int

Count counts the number of occurences of a value in the Matrix.

func (*Matrix[T]) Fill

func (m *Matrix[T]) Fill(value T)

Fill will fill the matrix with a given value.

func (*Matrix[T]) Find

func (m *Matrix[T]) Find(value T) (Coord, bool)

Find searches for a value in the Matrix and returns a coordinate of the first match and a boolean indicating if the value was found.

func (*Matrix[T]) In

func (m *Matrix[T]) In(x, y int) bool

In checks that a coordinate exists within the Matrix.

func (*Matrix[T]) InCoord

func (m *Matrix[T]) InCoord(c Coord) bool

InCoord checks that a coordinate exists within the Matrix.

func (*Matrix[T]) SetAt

func (m *Matrix[T]) SetAt(x, y int, value T)

SetAt sets the value present at a coordinate. It's the responsibility of the user to check that the coordinate exists within the Matrix with In.

func (*Matrix[T]) SetAtCoord

func (m *Matrix[T]) SetAtCoord(c Coord, value T)

SetAtCoord sets the value present at a coordinate. It's the responsibility of the user to check that the coordinate exists within the Matrix with InCoord.

func (*Matrix[T]) String

func (m *Matrix[T]) String() string

type Vec

type Vec XY

func (Vec) Add

func (v Vec) Add(v2 Vec) Vec

Add adds a vector to another vector.

func (Vec) Div

func (v Vec) Div(n int) Vec

Mul multiplies a vector by a factor of n.

func (Vec) Inv

func (v Vec) Inv() Vec

Inv returns the inverse of this vector

func (Vec) Mul

func (v Vec) Mul(n int) Vec

Mul multiplies a vector by a factor of n.

func (Vec) Rotate

func (v Vec) Rotate(deg int) Vec

Rotates sort of rotates a well-known vector. This is mathematically wrong, but is useful for AOC.

func (Vec) String

func (v Vec) String() string

String returns a string representation of Vec.

type XY

type XY struct {
	X, Y int
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL