aoc-utils.core
binary-search
(binary-search hi target func)(binary-search lo hi target func)Find a number between lo and hi which gives target as a result of applying func to it.
create-grid
(create-grid v preds)Create a hashmap representation of a grid from a 2D vector, where preds is a map from a predicate (set, function, character) to its name, e.g. {\# :walls}.
The resulting hashmap has the following keys:
:height:width:size- if height and width are the same, otherwisenil- each predicate defined in the
predsparameter: - if a predicate was a character, returns a set of points
- otherwise, returns a map of points to matching characters
create-hashed-grid
(create-hashed-grid v preds)(create-hashed-grid v preds multi)Create a hashmap representation of a grid from a 2D vector, where each coordinate is represented as x + multi*y (default multi: 1000) and where preds is a map from a predicate (set, function, character) to its name, e.g. {\# :walls}.
The resulting hashmap has the following keys:
:height:width:size- if height and width are the same, otherwisenil- each predicate defined in the
predsparameter: - if a predicate was a character, returns an int-set of point hashes
- otherwise, returns an int-map of point hashes to matching characters
diagonals
(diagonals [x y])(diagonals pt pred)Four diagonal neighbours of a 2D point.
If pred is specified, returns only those neighbours that satisfy it.
dijkstra
(dijkstra options)Traverse a graph using the Dijkstra’s alorithm.
See traverse for more details.
do-count
macro
(do-count seq-exprs)Similar to the count-if function above, but allows for a more elaborate predicate, i.e. everything that the doseq built-in does.
find-first
(find-first pred xs)Find first element of a collection which satisfies the predicate.
find-first-index
(find-first-index pred xs)Returns the index of the first element which satisfies the predicate.
grid-get
(grid-get grid [x y :as pt])(grid-get grid pt default)(grid-get grid x y default)Get an element in y row, x col of a vector representation of a grid.
Returns default on a point which is out of bounds, or nil if not specified.
indexed
(indexed coll)(indexed coll start)Create a seq of [idx el] pairs from a coll.
If start is provided, it is used as a first index, otherwise 0.
inside-3d?
(inside-3d? size [x y z])(inside-3d? size x y z)Check if a 3D point (x, y, z) is inside of a cube of a given size.
inside?
(inside? size [x y])(inside? size x y)(inside? size-x size-y x y)Check if a point (x, y) is inside of a square/rectangle of a given size.
integers
(integers s & {:keys [negative?], :or {negative? true}})Extracts all integers from a string. It can ignore - if negative=false, e.g. for ranges 23-45.
manhattan
(manhattan pt)(manhattan [x1 y1] [x2 y2])Manhattan distance of a 2D point or between two 2D points.
manhattan-3d
(manhattan-3d p)(manhattan-3d [x1 y1 z1] [x2 y2 z2])Manhattan distance of a 3D point or between two 3D points.
max-map
(max-map f xs)Map a function to a collection and find a maximum value of the results.
max-pmap
(max-pmap f xs)Parallel map a function to a collection and find a maximum value of the results.
neighbours-3d
(neighbours-3d [x y z])Six neighbours of a 3D point, two in each direction.
neighbours-4
(neighbours-4 [x y])(neighbours-4 pt pred)Four neighbours of a 2D point.
If pred is specified, returns only those neighbours that satisfy it.
neighbours-8
(neighbours-8 pt)(neighbours-8 [x y :as pt] pred)Eight neighbours of a 2D point.
If pred is specified, returns only those neighbours that satisfy it.
parse-input
(parse-input s & [parse-fn word-sep])Parse input string, based on parse-fn, which can be a custom function or one of the following:
:int- parse a single integer:ints- get all integers:digits- extract all single digits:chars- make a list of chars:words- make a list of words
parse-lines
(parse-lines input & [parse-fn word-sep nl-sep])Parse each line of input by applying parse-fn to it.
We can pass any function as parse-fn or use one of the following:
:int- parse a single integer:ints- get all integers:digits- extract all single digits:chars- make a list of chars:words- make a list of words
parse-paragraphs
(parse-paragraphs input & [parse-fn word-sep])Split input into paragraphs (separated by a blank line). Parse each paragraph based on parse-fn.
prod-map
(prod-map f xs)Map a function to a collection and take a product of the results.
read-input
(read-input file)Read contents of an input file.
Assumes:
- inputs are in the sibling
../inputsdirectory - inputs have
.txtextension - if passing a single digit as a parameter, it zero-pads it
show-grid
(show-grid points)Convert a map/set representation of a grid to a printable string of points.
string->digits
(string->digits s)Extracts digits from a string. Ignores non-digit characters.
sum-map-indexed
(sum-map-indexed f xs)Map a function (which takes two arguments idx and el) to a collection and take a sum of the results.
sum-pmap
(sum-pmap f xs)Parallel map a function to a collection and take a sum of the results.
traverse
(traverse algo {:keys [start end walls size size-x size-y nb-func nb-num nb-cond end-cond cost-fn heuristic steps-limit allow-revisits? side-effect], :or {nb-num 4, walls #{}, nb-cond (constantly true), cost-fn (constantly 1), side-effect (constantly nil), steps-limit ##Inf, end-cond (fn* [p1__3856#] (= end p1__3856#)), heuristic (if end (fn [pt] (manhattan pt end)) (constantly 0))}})General graph traversal function. Not very performant. Not very pretty.
The simplest version needs just :start, :end and :walls keys, and either :nb-num (number of neighbours to consider) or a custom :nb-func.
The list of possible keys passed in the options map is quite large:
[start end walls size size-x size-y
nb-func nb-num nb-cond end-cond
cost-fn heuristic steps-limit
allow-revisits? side-effect]
It’s best to read the function’s source code to understand what each does.