Why not join the fun 🎄
Links to the solutions:
Timing is done on a 2021 M1 Pro with 32 GB RAM.
Manipulating strings in Swift is pretty slow because of unicode support. AsciiString uses one byte per character and allows for much better performance when accessing elements and iterating characters. For AoC we only need Ascii support anyway.
On UInt8 some static vars are available with ascii values for common characters used in AoC.
asAsciiArray-> [UInt8]: Converts string to ascii values in byte array.
allLines(includeEmpty: Bool = false)-> [String] -> Returns all lines in the String as an array of Strings.
parseCommaSeparatedInts(filterInvalid: Bool = true)-> [Int]: Turns a comma separated integer list into an array of integers.
getCapturedValues-> [String]?: All captures regex values into an array of Strings.
In older AoC challenges md5 is often needed.
md5AsHexfunction available to return a hash as a hex string.
md5AsBytesis faster as it does not convert the resulting hash to a string but instead returns the raw bytes.
SeededRandomNumberGenerator to generate predictable random numbers.
Direction for north, east, south and west navigation.
leftgives the direction when turning 90 degrees left
rightgives the direction when turning 90 degrees right
oppositegives the direction when turning 180 degrees
Point2D type for storing 2D integer coordinates.
yare the coordinates
moved(to: Direction, steps: Int = 1)will move the coordinates in the given direction, with the given number of steps. For example x = 3, y = 5 move to .right with 3 steps will result in x = 6, y = 5.
neighborswill give the four neighboring coordinates of the current Point2D (in the north, east, south, west direction).
Point3D but expanded to 3 dimensions:
Currently does not have much more functionality. Can be expanded when needed.
LoopedLinkedListSet is a not yet fully realized (but functional enough) looped linked list for unique values.
A type to store elements and edges of a graph.
The BFS type wraps two solvers using the BFS algorithm.
mostCommonElement-> Element: finds most common element in an array
leastCommonElement-> Element: finds the least common element in an array
negated: Toggle all boolean elements in a Bool Array
combinationsWithoutRepetition(length: Int)-> [[Element]]: returns all combinations of length elements of an array, for example
[1,2,3,4,5], length 4= [[1, 2, 3, 4], [1, 2, 3, 5], [1, 2, 4, 5], [1, 3, 4, 5], [2, 3, 4, 5]]
permutations-> [[Element]]: Returns all permutations of an array using the heap algorithm.
subsets(minLength: Int, maxLength: Int)-> [[Element]]: Returns all subsets of given lengths of an array.