utils

package
v0.0.0-...-359cf57 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2022 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package utils contains some util functions and types for Advent Of Code.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Abs

func Abs(number int) int

Abs returns the absolute value of an int

func CopyIntSlice

func CopyIntSlice(original []int) []int

CopyIntSlice takes a slice of ints and returns a copy of it.

func Dist

func Dist(size int, value int) [][]int

Dist gives all the combination of lists with the given size that have value as sum

func GetFileAsString

func GetFileAsString(filepath string) (string, error)

GetFileAsString reads a file on the given path and returns its contents as a string with whitespace trimmed.

func GetReaderAsString

func GetReaderAsString(r io.Reader) (string, error)

GetReaderAsString reads a file on the given path and returns its contents as a string with whitespace trimmed.

func GroupByEmptyLines

func GroupByEmptyLines(fileContents string) ([][]string, error)

GroupByEmptyLines reads the contents fo the string and returns the lines in the file, grouped by empty lines separating the,

func IntPermutations

func IntPermutations(arr []int) [][]int

IntPermutations generates all the permutations of integers using Heap's algorithm

func LinesAsInts

func LinesAsInts(fileContents string) ([]int, error)

LinesAsInts reads the string as a list of numbers.

func MakeIntMatrix

func MakeIntMatrix(rows int, columns int) [][]int

MakeIntMatrix makes a matrix with the specified dimensions. The matrix is given as slices of slices.

func ManhattanDistance

func ManhattanDistance(p1 Point2D, p2 Point2D) int

ManhattanDistance computes the Manhattan distance between 2 points

func Max

func Max(a, b int) int

Max returns the max between two numbers

func Min

func Min(a, b int) int

Min returns the min between two numbers

func MinMax

func MinMax(a, b int) (int, int)

MinMax returns the min and max between two numbers

func MustAtoi

func MustAtoi(strNum string) int

MustAtoi converts a string to a number, but panics if the conversion fails. This avoids error handling, but must be used when the calling code is sure the string represents a number.

func StringDigits

func StringDigits(str string) []int

StringDigits returns a slice of ints with the digits in a string. The argument must be a string only with runes that represent digits

func StringRunes

func StringRunes(str string) []rune

StringRunes returns a slice of runes in a string

func TrimmedLines

func TrimmedLines(fileContents string) []string

TrimmedLines takes a string with the contents of a file and divides into it'elems lines. Whitespace is trimmed from every line.

func TrimmedLinesNoEmpty

func TrimmedLinesNoEmpty(fileContents string) []string

TrimmedLinesNoEmpty takes a string with the contents of a file and divides into it'elems lines. Whitespace is trimmed from every line.

Types

type ErrorTolerantWriter

type ErrorTolerantWriter struct {
	// contains filtered or unexported fields
}

ErrorTolerantWriter is an io.Writer that is a wrapper around a regular io.Writer that has its writes write to the underlying writer, until an error occurs, at which point, further writes will do nothing. To check if an error occurred in any of the writes and how many bytes were written up until the first error happened, call w.Error() and w.Bytes() respectively.

func NewErrorTolerantWriter

func NewErrorTolerantWriter(w io.Writer) *ErrorTolerantWriter

NewErrorTolerantWriter returns a new ErrorTolerantWriter with the given writer as the underlying writer.

func (*ErrorTolerantWriter) Bytes

func (w *ErrorTolerantWriter) Bytes() int

Bytes returns the number of bytes written to the underlying writer. If an error occurred in any of the writes, returns the bytes written until the first error happened.

func (*ErrorTolerantWriter) Error

func (w *ErrorTolerantWriter) Error() error

Error returns nil if no error occurred in any of the writes, otherwise returns the first error that happened.

func (*ErrorTolerantWriter) Write

func (w *ErrorTolerantWriter) Write(p []byte) (int, error)

Write writes to the underlying writer if none of the previous writes returned an error, does nothing otherwise. The returned error is always nil, to check if any error happened in any of the writes, call w.Error().

type IntSet

type IntSet struct {
	Set map[int]bool
}

IntSet represents a set of integers

func NewIntSet

func NewIntSet() IntSet

NewIntSet returns a newly created int set

func (IntSet) Add

func (s IntSet) Add(element int)

Add adds an element to the int set

func (IntSet) Elements

func (s IntSet) Elements(element string) []int

Elements returns the elements of this set as a slice of ints

func (IntSet) Has

func (s IntSet) Has(element int) bool

Has checks if an element exists on the int set

func (IntSet) Remove

func (s IntSet) Remove(element int)

Remove removes an element from the int set

type Point2D

type Point2D struct {
	X int
	Y int
}

Point2D represents a point in 2 dimensions with integer coordinates

func (*Point2D) Add

func (p *Point2D) Add(another Point2D) *Point2D

func (Point2D) ManhattanDistance

func (p Point2D) ManhattanDistance(otherPoint Point2D) int

ManhattanDistance computes the manhattan distance between this point and another

type Rectangle

type Rectangle struct {
	Corner1 Point2D
	Corner2 Point2D
}

Rectangle represents a rectangle in a grid of integer coordinates

func (*Rectangle) AmplitudeX

func (r *Rectangle) AmplitudeX() int

AmplitudeX returns how large is the rectangle in the X (horizontal) axis

func (*Rectangle) AmplitudeY

func (r *Rectangle) AmplitudeY() int

AmplitudeY returns how large is the rectangle in the Y (vertical) axis

func (*Rectangle) BottomLeftCorner

func (r *Rectangle) BottomLeftCorner() Point2D

BottomLeftCorner returns the bottom left corner of the rectangle

func (*Rectangle) BottomRightCorner

func (r *Rectangle) BottomRightCorner() Point2D

BottomRightCorner returns the bottom right corner of the rectangle

func (*Rectangle) TopLeftCorner

func (r *Rectangle) TopLeftCorner() Point2D

TopLeftCorner returns the top bottom corner of the rectangle

func (*Rectangle) TopRightCorner

func (r *Rectangle) TopRightCorner() Point2D

TopRightCorner returns the top right corner of the rectangle

type StringSet

type StringSet struct {
	Set map[string]bool
}

StringSet represents a set of strings

func NewStringSet

func NewStringSet(elems ...string) StringSet

NewStringSet returns a newly created string set

func StringSetIntersection

func StringSetIntersection(sets ...StringSet) StringSet

func (StringSet) Add

func (s StringSet) Add(element string)

Add adds an element to the string set

func (StringSet) Elements

func (s StringSet) Elements() []string

Elements returns the elements of this set as a slice of strings

func (StringSet) Has

func (s StringSet) Has(element string) bool

Has checks if an element exists on the string set

func (StringSet) Intersect

func (s StringSet) Intersect(otherSets ...StringSet) StringSet

Intersect returns the intersection of a set with other sets

func (StringSet) Remove

func (s StringSet) Remove(element string)

Remove removes an element from the string set

func (StringSet) Union

func (s StringSet) Union(otherSets ...StringSet) StringSet

Union merges a set with other sets, so the resulting values in the set wil be the values present in all sets

type StringStack

type StringStack struct {
	// contains filtered or unexported fields
}

func NewStringStack

func NewStringStack() *StringStack

func (*StringStack) Elems

func (s *StringStack) Elems() int

func (*StringStack) IsEmpty

func (s *StringStack) IsEmpty() bool

func (*StringStack) Pop

func (s *StringStack) Pop() string

func (*StringStack) Push

func (s *StringStack) Push(elem string)

type Vector2D

type Vector2D struct {
	Origin      Point2D
	Destination Point2D
}

Vector2D is a vector in 2 dimensions

func (Vector2D) AngleWith

func (v Vector2D) AngleWith(u Vector2D) float64

func (Vector2D) ComponentX

func (v Vector2D) ComponentX() int

func (Vector2D) ComponentY

func (v Vector2D) ComponentY() int

func (Vector2D) DotProduct

func (v Vector2D) DotProduct(u Vector2D) int

func (Vector2D) Norm

func (v Vector2D) Norm() float64

Jump to

Keyboard shortcuts

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