Helper classes and routines for Advent of Code
hex string to int value. lowercase letters assumed! no error checking here!
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| integer, | public, | parameter | :: | wp | = | real64 |
default real kind [8 bytes] |
| integer, | public, | parameter | :: | ip | = | int64 |
default int kind |
| integer, | private, | parameter | :: | chunk_size | = | 100 |
for dynamic allocations |
| type(clock), | public | :: | clk |
a public clock to use for timing in the problems |
Sorts an integer array ivec in increasing order.
Uses a basic recursive quicksort
(with insertion sort for partitions with 20 elements).
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(inout), | dimension(:) | :: | ivec |
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=ip), | intent(inout), | dimension(:) | :: | ivec |
parse space-deliminated ip sequence (positive or negative)
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | line |
Split a character string using a token. This routine is inspired by the Python split function.
character(len=:),allocatable :: s
type(string),dimension(:),allocatable :: vals
s = '1,2,3,4,5'
call split(s,',',vals)
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | str | |||
| character(len=*), | intent(in) | :: | token |
Split a string, given a token.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(string), | intent(in) | :: | s | |||
| character(len=*), | intent(in) | :: | token |
Basic string to integer routine
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(string), | intent(in) | :: | me |
Basic string to integer routine
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | str |
Basic string to integer(ip) routine. Hacky hack just so we can overload as int()
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | str | |||
| integer, | intent(in) | :: | kind |
Character array to integer routine
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=1), | intent(in), | dimension(:) | :: | str_array |
example ['1','3'] --> 13 |
Basic string to integer routine
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(string), | intent(in) | :: | me |
Return only the unique values from vec.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(in), | dimension(:) | :: | vec |
Return only the unique values from vec.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=ip), | intent(in), | dimension(:) | :: | vec |
test if a string starts with a specified substring
starts with function for strings
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | str | |||
| character(len=*), | intent(in) | :: | substring |
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(string), | intent(in) | :: | str | |||
| type(string), | intent(in) | :: | substring |
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(string), | intent(in) | :: | str | |||
| character(len=*), | intent(in) | :: | substring |
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | str | |||
| type(string), | intent(in) | :: | substring |
Swap two integer values.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(inout) | :: | i1 | |||
| integer, | intent(inout) | :: | i2 |
Swap two integer values.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=ip), | intent(inout) | :: | i1 | |||
| integer(kind=ip), | intent(inout) | :: | i2 |
Swap two character string values
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(inout) | :: | i1 | |||
| character(len=*), | intent(inout) | :: | i2 |
Manhattan distance between two ip points.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=ip), | intent(in) | :: | x1 | |||
| integer(kind=ip), | intent(in) | :: | y1 | |||
| integer(kind=ip), | intent(in) | :: | x2 | |||
| integer(kind=ip), | intent(in) | :: | y2 |
to get all the indices in a 2d array that match a value
return the indices of all the val elements in 2d array.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=1), | intent(in), | dimension(:,:) | :: | array | ||
| character(len=1), | intent(in) | :: | val | |||
| integer, | intent(out), | dimension(:), allocatable | :: | iloc | ||
| integer, | intent(out), | dimension(:), allocatable | :: | jloc |
return the indices of all the val elements in 2d array.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(in), | dimension(:,:) | :: | array | ||
| integer, | intent(in) | :: | val | |||
| integer, | intent(out), | dimension(:), allocatable | :: | iloc | ||
| integer, | intent(out), | dimension(:), allocatable | :: | jloc |
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| integer(kind=ip), | private | :: | begin | ||||
| integer(kind=ip), | private | :: | end | ||||
| integer(kind=ip), | private | :: | rate |
| procedure, public :: tic => clock_start | |
| procedure, public :: toc => clock_end |
a type containing an allocatable character string. so we can have an array of strings of different lengths.
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| character(len=:), | public, | allocatable | :: | str |
| procedure, public :: to_int => string_to_int | ../../ convert to integer |
| procedure, public :: to_int_64 => string_to_int_64 |
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| integer, | public | :: | iunit | = | 0 |
| private function open_file (filename) |
| final :: close_file |
| procedure, public :: n_lines | |
| procedure, public :: read_line => read_line_from_file |
an type that contains an allocatable ip array. so we can have an array of these.
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| integer(kind=ip), | public, | dimension(:), allocatable | :: | vals |
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | filename |
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | filename |
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(file_t), | intent(in) | :: | me |
Basic string to integer routine
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | str |
Basic string to integer routine
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(string), | intent(in) | :: | me |
Basic string to integer routine
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(string), | intent(in) | :: | me |
Basic string to integer(ip) routine. Hacky hack just so we can overload as int()
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | str | |||
| integer, | intent(in) | :: | kind |
integer to string
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=ip), | intent(in) | :: | i |
integer to string
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=ip), | intent(in) | :: | i |
Character array to integer routine
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=1), | intent(in), | dimension(:) | :: | str_array |
example ['1','3'] --> 13 |
integer array to Character array
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(in), | dimension(:,:) | :: | iarray |
Read a file into a 2d character array.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | filename | |||
| character(len=1), | intent(in), | optional | :: | border |
if true, extra border is added with this char |
read a file that is single string of ints into a vector
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | filename |
Read a file into a 2d int array. Uses the '(*(I1))' format.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | filename |
Read a file into an integer array (one element per line)
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | filename |
Read a file into an ip integer array (one element per line)
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | filename |
Returns the number of lines in a file (assumed to be open)
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(in) | :: | iunit |
the file unit number (assumed to be open) |
the number of lines in the file
Split a character string using a token. This routine is inspired by the Python split function.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | str | |||
| character(len=*), | intent(in) | :: | token |
Reads the next line from a file.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(in) | :: | iunit | |||
| logical, | intent(out), | optional | :: | status_ok |
true if no problems |
Return only the unique values from vec.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(in), | dimension(:) | :: | vec |
Return only the unique values from vec.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=ip), | intent(in), | dimension(:) | :: | vec |
parse positive ints from a string that also includes text
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | line |
Parse positive ints from a string that also includes text
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | line |
parse space-deliminated ip sequence (positive or negative)
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | line |
starts with function for strings
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | str | |||
| character(len=*), | intent(in) | :: | substring |
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(string), | intent(in) | :: | str | |||
| character(len=*), | intent(in) | :: | substring |
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | str | |||
| type(string), | intent(in) | :: | substring |
returns true if the character is a number from 0 to 9.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=1), | intent(in) | :: | c |
returns true if the character is not a number.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=1), | intent(in) | :: | c |
convert the character string to an array of characters
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | s |
LCM. based on code from NCAR Command Language
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=ip), | intent(in) | :: | i | |||
| integer(kind=ip), | intent(in) | :: | j |
Reverse an ip vector
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=ip), | intent(in), | dimension(:) | :: | ivals |
Difference ip vector
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=ip), | intent(in), | dimension(:) | :: | ivals |
given a sequence of nb points (x(i),y(i)). parea computes the area bounded by the closed polygonal curve which passes through the points in the order that they are indexed. the final point of the curve is assumed to be the first point given. therefore, it need not be listed at the end of x and y. the curve is not required to be simple.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=wp), | intent(in) | :: | x(nb) | |||
| real(kind=wp), | intent(in) | :: | y(nb) | |||
| integer, | intent(in) | :: | nb |
Manhattan distance between two ip points.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=ip), | intent(in) | :: | x1 | |||
| integer(kind=ip), | intent(in) | :: | y1 | |||
| integer(kind=ip), | intent(in) | :: | x2 | |||
| integer(kind=ip), | intent(in) | :: | y2 |
Convert a string to a numeric array by mapping characters to integers (user-specified)
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | str | |||
| character(len=1), | intent(in), | dimension(:) | :: | ichars |
characters to process |
|
| integer, | intent(in), | dimension(:) | :: | iints |
int values of the chars |
Convert a string to a numeric array by mapping characters to integers (user-specified)
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | str | |||
| character(len=1), | intent(in), | dimension(:) | :: | ichars |
characters to process |
|
| integer(kind=ip), | intent(in), | dimension(:) | :: | iints |
int values of the chars |
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | hex |
Cross product of two real 3x1 vectors
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=wp), | intent(in), | dimension(3) | :: | r | ||
| real(kind=wp), | intent(in), | dimension(3) | :: | v |
return the number of digits in the integer
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=ip), | intent(in) | :: | i |
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| type(file_t), | intent(inout) | :: | me |
Print runtime in milliseconds form the start of the clock.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| class(clock), | intent(inout) | :: | me | |||
| character(len=*), | intent(in) | :: | case_str |
description of the case |
Sorts an integer array ivec in increasing order.
Uses a basic recursive quicksort
(with insertion sort for partitions with 20 elements).
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(inout), | dimension(:) | :: | ivec |
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=ip), | intent(inout), | dimension(:) | :: | ivec |
Swap two integer values.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(inout) | :: | i1 | |||
| integer, | intent(inout) | :: | i2 |
Swap two integer values.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=ip), | intent(inout) | :: | i1 | |||
| integer(kind=ip), | intent(inout) | :: | i2 |
Swap two character string values
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(inout) | :: | i1 | |||
| character(len=*), | intent(inout) | :: | i2 |
Add elements to the integer vector in chunks.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(inout), | dimension(:), allocatable | :: | vec | ||
| integer, | intent(inout) | :: | n |
counter for last element added to |
||
| integer, | intent(in), | optional | :: | val |
the value to add to |
|
| logical, | intent(in), | optional | :: | finished |
set to true to return |
given a polygonal line connecting the vertices (x(i),y(i)) (i = 1,...,n) taken in this order. it is assumed that the polygonal path is a loop, where (x(n),y(n)) = (x(1),y(1)) or there is an arc from (x(n),y(n)) to (x(1),y(1)).
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=wp), | intent(in) | :: | x0 | |||
| real(kind=wp), | intent(in) | :: | y0 | |||
| real(kind=wp), | intent(in), | dimension(n) | :: | x | ||
| real(kind=wp), | intent(in), | dimension(n) | :: | y | ||
| integer, | intent(in) | :: | n | |||
| integer, | intent(out) | :: | l | |||
| integer, | intent(out) | :: | m |
inverse of a 2x2 matrix.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=wp), | intent(in), | dimension(2,2) | :: | a | ||
| real(kind=wp), | intent(out), | dimension(2,2) | :: | ainv | ||
| logical, | intent(out) | :: | status_ok |
return the indices of all the val elements in 2d array.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=1), | intent(in), | dimension(:,:) | :: | array | ||
| character(len=1), | intent(in) | :: | val | |||
| integer, | intent(out), | dimension(:), allocatable | :: | iloc | ||
| integer, | intent(out), | dimension(:), allocatable | :: | jloc |
return the indices of all the val elements in 2d array.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer, | intent(in), | dimension(:,:) | :: | array | ||
| integer, | intent(in) | :: | val | |||
| integer, | intent(out), | dimension(:), allocatable | :: | iloc | ||
| integer, | intent(out), | dimension(:), allocatable | :: | jloc |