aoc-utils.core
bidi-bfs
(bidi-bfs {:keys [start end walls size size-x size-y nb-func nb-num nb-cond], :or {nb-num 4, walls #{}}})Find the length of the shortest path between two points with bidirectional BFS.
For a simple case of a known start and end point and when only the length of the path is needed, this is faster than the regular, unidirectional, BFS. If you need more flexibility, use traverse.
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.
count-digits
(count-digits n)Number of digits.
Slightly faster than ((comp count str) n).
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 algorithm.
See traverse for more details.
do-count
macro
(do-count seq-exprs)Similar to the count-if function, but allows for a more elaborate predicate, i.e. everything that the doseq built-in does.
find-first
(find-first pred xs)Find the first element of a collection which satisfies the predicate.
find-first-index
(find-first-index pred xs)Find 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 the specified default value 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 the 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 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 a square/rectangle of a given size.
integers
(integers s)(integers s negative?)(integers s negative? neg-char)Extracts all integers from a string.
It can ignore - if you pass false as a second argument, useful e.g. for ranges like 23-45. You can pass a custom character as a third argument, denoting a negative number, e.g. \L.
left-turn
(left-turn [x y])Assumes left-hand coord system (positive y goes down).
Down -> Right -> Up -> Left.
manhattan
(manhattan pt)(manhattan [x1 y1] [x2 y2])Manhattan distance of a 2D point or between two 2D points.
manhattan-3d
(manhattan-3d pt)(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 over a collection and find the maximum value of the results.
max-pmap
(max-pmap f xs)Parallel map a function over a collection and find the maximum value of the results.
neighbours-27
(neighbours-27 pt)(neighbours-27 [x y z] pred)All neighbours of a 3D point, including the point itself.
If pred is specified, returns only those neighbours that satisfy it.
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 [x y])(neighbours-8 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:nats- get all natural numbers, i.e. ignore the-sign:digits- extract all single digits:chars- make a vector of chars:words- make a vector of words:keywords- make a vector of keywords
parse-lines
(parse-lines input & [parse-fn word-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:nats- get all natural numbers, i.e. ignore the-sign:digits- extract all single digits:chars- make a vector of chars:words- make a vector of words:keywords- make a vector of keywords
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 over a collection and take the product of the results.
read-input
(read-input file)Read the 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
right-turn
(right-turn [x y])Assumes left-hand coord system (positive y goes down).
Down -> Left -> Up -> Right.
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) over a collection and take the sum of the results.
sum-pmap
(sum-pmap f xs)Parallel map a function over a collection and take the 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 #{}, steps-limit 999999}})General graph traversal function.
The first argument, algo, is one of: :dfs, :bfs, :dijk or :a-star.
The second argument is a map with various (mostly optional) keys. At minimum, you need to provide :start and :end points, and a set of :walls.
Optional keys:
:nb-numor:nb-func: number of neighbours (4 (default), 8 or:xfor diagonals) or a custom function from a point to its neighbours:nb-cond: a valid neighbour needs to satisfy this predicate:sizeor:size-x+:size-y: outer bounds; outside points are skipped:end-cond: stop if a point satisfies this predicate (default::end):steps-limit: stop when the steps reach this limit:cost-fn: cost function of going from a point to a point (default: 1):heuristic: for A* (default: Manhattan distance to:end):allow-revisits?: revisit points (default: false):side-effect: function to be called on every step (it can beprintlnfor debugging)
When the traversal stops, returns a map with:
:current: the current point (ornilif the queue ran out):steps: number of steps taken:queue: the remaining queue:count: number of all visited points:seen: a function returning a set of all visited points:costs: a function returning a map of each point and its cost:path: a function returning the path from:startto:current
words
(words s)(words s word-sep)Split a string into words, based on word-sep. If word-sep is not provided, it splits on (multiple) whitespace.