Module day12

Source
Expand description

§JSAbacusFramework.io

§Part One

The utility iter_signed method extracts numbers from surrounding text and is used directly.

§Part Two

We build a tiny custom JSON parser using a parser combinator approach, making some simplifying assumptions:

  • The input is always well formed and does not contain any whitespace.
  • Arrays and objects contain at least one item.
  • We don’t care about the content of strings, only if they equal “red” or not.

Each parsing function returns a Result struct which has 3 fields:

  • next The index of the character after this object. For example parsing “123,” returns a value of 3 for next.
  • ignore: Only true for strings that exactly equals “red”, false otherwise and always false for numbers, arrays and objects.
  • value: For numbers the literal value, for string zero, for arrays the sum of child items, for objects the sum of child items if no “red” property is present, otherwise zero.

Structs§

Result 🔒

Constants§

RED 🔒

Functions§

parse
parse_array 🔒
Parse array assuming it contains at least one element.
parse_json 🔒
Parse JSON that has no whitespace.
parse_number 🔒
Parse an integer value.
parse_object 🔒
Parse object assuming it contains at least one key/value pair.
parse_string 🔒
Parse a string evaluating only if it equals “red”.
part1
part2