aoc-utils.core

a-star

(a-star options)

Traverse a graph using the A* algorithm.

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 algorithm.

See traverse for more details.

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.

count-digits

(count-digits n)

Number of digits.

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 algorithm.

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 algorithm.

See traverse for more details.

divisible?

(divisible? n d)

Check if n is divisible by d.

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.

empty-queue

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

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.

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 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.

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).

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.

memo

(memo f)

A faster variant of the built-in memoize function. Not thread-safe.

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.

none?

(none? pred xs)

A faster version of not-any?.

offsets-27

All 27 offsets of a 3D point (including the point itself).

offsets-8

Eight neighbouring offsets of a 2D point.

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.

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 the 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).

Down -> Left -> Up -> Right.

show-grid

(show-grid points)

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

sign

(sign x)

Sign of an integer.

string->digits

(string->digits s)

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

sum-map

(sum-map f xs)

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

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.

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 #{}, 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-num or :nb-func: number of neighbours (4 (default), 8 or :x for diagonals) or a custom function from a point to its neighbours
  • :nb-cond: a valid neighbour needs to satisfy this predicate
  • :size or :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 be println for debugging)

When the traversal stops, returns a map with:

  • :current: the current point (or nil if 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 :start to :current

update-2

(update-2 m k1 k2 f)

Usually faster than the update-in built-in.

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.