aoc-utils.core

a-star

(a-star options)

Traverse a graph using the A* alorithm.

See traverse for more details.

array-none?

(array-none? pred arr)

Much much faster version of not-any? for long-arrays.

assoc-2

(assoc-2 m k1 k2 v)

Usually faster than the assoc-in built-in.

assoc-3

(assoc-3 m k1 k2 k3 v)

Usually faster than the assoc-in built-in.

bfs

(bfs options)

Traverse a graph using the BFS alorithm.

See traverse for more details.

count-digits

(count-digits n)

Slightly faster than ((comp count str) n).

count-if

(count-if pred xs)

An alternative to (count (filter ...)).

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, otherwise nil
  • each predicate defined in the preds parameter:
  • 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, otherwise nil
  • each predicate defined in the preds parameter:
  • if a predicate was a character, returns an int-set of point hashes
  • otherwise, returns an int-map of point hashes to matching characters

dfs

(dfs options)

Traverse a graph using the DFS alorithm.

See traverse for more details.

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.

divisible?

(divisible? n d)

Check if n is divisible by n.

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.

empty-queue

An easier-to-type way to create an empty queue.

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.

gcd

(gcd)(gcd x)(gcd a b)

Greatest common divisor.

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.

lcm

(lcm)(lcm x)(lcm a b)

Least common multiple.

left-turn

(left-turn [x y])

Assumes left-hand coord system (positive y goes down).

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.

none?

(none? pred xs)

A faster version of not-any?.

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.

pt*

(pt* magnitude [x y])

Multiply each coordinate of a 2D point by magnitude.

pt+

(pt+ [x1 y1] [x2 y2])

Sum of two 2D points.

pt-

(pt- [x1 y1] [x2 y2])

Difference between two 2D points.

pt-3d*

(pt-3d* magnitude [x y z])

Multiply each coordinate of a 3D point by magnitude.

pt-3d+

(pt-3d+ [x1 y1 z1] [x2 y2 z2])

Sum of two 3D points.

pt-3d-

(pt-3d- [x1 y1 z1] [x2 y2 z2])

Difference between two 3D points.

read-input

(read-input file)

Read contents of an input file.

Assumes:

  • inputs are in the sibling ../inputs directory
  • inputs have .txt extension
  • 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).

show-grid

(show-grid points)

Convert a map/set representation of a grid to a printable string of points.

sign

(sign x)

Sign of a number.

string->digits

(string->digits s)

Extracts digits from a string. Ignores non-digit characters.

sum-map

(sum-map f xs)

Map a function to a collection and take a sum of the results.

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.

transpose

(transpose matrix)

Transform a matrix of rows into a matrix of columns.

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.

update-2

(update-2 m k1 k2 f)

Usually faster than the update-in built-in.