aocutils

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 31, 2024 License: GPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package aocutils is a collection of commonly used functions that might be helpful for Advent of Code solving. User puzzle input can be obtained without importing this sub-package if you wish to handle all of that yourself.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AverageListOfNums

func AverageListOfNums[n Number](elements []n) n

AverageListOfNums takes a list of Number elements and returns the average of all of them.

func Distance2D

func Distance2D[n Number](x1, y1, x2, y2 n) float64

Distance2D calculates the Euclidean distance between two points in 2D space. Returns the distance as a float64.

func Distance3D

func Distance3D[n Number](x1, y1, z1, x2, y2, z2 n) float64

Distance3D calculates the Euclidean distance between two points in 3D space. Returns the distance as a float64.

func ExtractIntsFromString

func ExtractIntsFromString(input string) []int

ExtractIntsFromString will find all groups of consecutive digits in a string. Returns a slice of all integers extracted from the input string.

func FindDuplicates

func FindDuplicates[v Value](elements []v) []v

FindDuplicates will find any elements that appear more than once in the slice. Returns a slice containing all duplicates that appear in the input slice.

func FindSubstringsOfLength

func FindSubstringsOfLength(s string, length int) []string

FindSubstringsOfLength takes a string and a desired length, and returns all unique substrings of that length. Returns a slice of substrings.

func FlattenList

func FlattenList[v Value](nested [][]v) []v

FlattenList takes a slice matrix of values and will turn it into a 1D slice. Returns a 1D slice, constructed by appending each row of the matrix to one another in order.

func ManhattanDistance2D

func ManhattanDistance2D(x1, y1, x2, y2 int) int

ManhattanDistance2D calculates the Manhattan distance between two points in 2D space. Returns the distance as an int.

func ManhattanDistance3D

func ManhattanDistance3D(x1, y1, z1, x2, y2, z2 int) int

ManhattanDistance3D calculates the Manhattan distance between two points in 3D space. Returns the distance as an int.

func NthElements

func NthElements[v Value](elements []v, n int) []v

NthElements takes a slice of elements and an interval. Returns a slice of the elements occuring every `n` spots of the original array.

func NumOccurrences

func NumOccurrences[v Value](elements []v, target v) int

NumOccurrences counts the number of times a specific value occurs in a slice. Returns the count of occurrences of the target value in the input slice.

func RemoveDuplicates

func RemoveDuplicates[v Value](elements []v) []v

RemoveDuplicates removes duplicate elements from a slice. Returns a new slice with only unique elements, preserving the order of their first occurrence.

func ReverseSlice

func ReverseSlice[v Value](elements []v) []v

ReverseSlice reverses the order of elements in a slice. Returns a new slice with the elements in reverse order.

func TransposeMatrix

func TransposeMatrix[v Value](matrix [][]v) [][]v

TransposeMatrix takes a 2D slice (matrix) and transposes it (swaps rows and columns). Returns the transposed matrix as a new 2D slice.

Types

type Number

type Number interface {
	int | int64 | float64
}

Number interface is a Generic wrapper around numeric types that data is commonly used in

type Value

type Value interface {
	int | int64 | float64 | rune | string
}

Value interface is a Generic wrapper around types that data is commonly used in

Jump to

Keyboard shortcuts

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