Skip to main content

Advent of Code Parsers

pyversions

A collection of convenient Python parsers for Advent of Code problems.

Installation

pip install aocp

Quickstart

You can import parsers from the base module. There are two main types of parsers:

  • Iterable parsers, which return a sequence of elements from parsing a str, such as list, tuple or dict
  • Transform parsers, which return a single object from parsing a str, such as an int, bool or another str

Iterable parsers can be composed with other parsers nested within, including Transform parsers and other Iterable parsers. They can also be nested with some base types such as int.

Transform parsers cannot have nested parsers, but they can be composed with other parsers in a sequence using ChainParser.

This way, the structure of the output data mirrors that of the expression used to instantiate the parser transform.

Here is a basic usage example:

raw_data = "46,79,77,45,57,34,44,13,32,88,86,82,91,97"
parser = ListParser(IntParser)
parser.parse(raw_data)

Which results in:

[46, 79, 77, 45, 57, 34, 44, 13, 32, 88, 86, 82, 91, 97]

And here is a more advanced example, from AoC 2021 day 4:

raw_data = """7,4,9,5,11,17,23,2,0,14,21,24,10,16,13,6,15,25,12,22,18,20,8,19,3,26,1

22 13 17 11  0
 8  2 23  4 24
21  9 14 16  7
 6 10  3 18  5
 1 12 20 15 19

 3 15  0  2 22
 9 18 13 17  5
19  8  7 25 23
20 11 10 24  4
14 21 16 12  6

14 21 17 24  4
10 16 15  9 19
18  8 23 26 20
22 11 13  6  5
 2  0 12  3  7"""
parser = TupleParser(
    (
        IntListParser(),
        ListParser(ListParser(IntListParser())),
    )
)
parser.parse(raw_data)

Which results in:

(
    [7, 4, 9, 5, 11, 17, 23, 2, 0, 14, 21, 24, 10, 16, 13, 6, 15, 25, 12, 22, 18, 20, 8, 19, 3, 26, 1], 
    [[[22, 13, 17, 11, 0],
     [8, 2, 23, 4, 24],
     [21, 9, 14, 16, 7],
     [6, 10, 3, 18, 5],
     [1, 12, 20, 15, 19]],
    [[3, 15, 0, 2, 22],
     [9, 18, 13, 17, 5],
     [19, 8, 7, 25, 23],
     [20, 11, 10, 24, 4],
     [14, 21, 16, 12, 6]],
    [[14, 21, 17, 24, 4],
     [10, 16, 15, 9, 19],
     [18, 8, 23, 26, 20],
     [22, 11, 13, 6, 5],
     [2, 0, 12, 3, 7]]]
)

By default, the splitting elements in an iterable are guessed from the string provided. However, you can provide them through the splitter argument. This can be a strings or a sequence of strings, which will all act as splitters.

A notebook with more examples from Aoc 2021 is available here.

To be done

  • Full testing coverage
  • Documentation page (full docstrings are available, though)
  • More examples from other previous years
  • Regex Parser
  • Defaults in tuple parser in case of missing components in origin string

Source code

https://github.com/miguel-bm/advent-of-code-parsers

Metadata

Release files for aocp 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aocp 1.0.0
File Size Uploaded
aocp-1.0.0.tar.gz 6.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aocp 1.0.0
File Interpreter ABI Platform
aocp-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.8 kB

Release files / aocp-1.0.0.tar.gz

Download URL aocp-1.0.0.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
28ca6503577f97e9af99fdde7e67e20e8fb065f8a96fad58af4b082b1da205b9
BLAKE2b-256 checksum
How to use checksums
698289e095f77f4e8b319b566a3d45e799407010766eaffe2fbe0a94f8de3909
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.9.8 Darwin/20.6.0

Release files / aocp-1.0.0-py3-none-any.whl

Download URL aocp-1.0.0-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f164d4f35d7397005fdd086e175ead88885cd9602aae568f1c22df192e35f60d
BLAKE2b-256 checksum
How to use checksums
e51c3118c4af83210ff65c0b1eaa3b066baab1457265822a5750208898db7255
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.12 CPython/3.9.8 Darwin/20.6.0

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page