From f137feefe42e09d2fb0fa0048ce995f1e5a1f4e3 Mon Sep 17 00:00:00 2001 From: Pascal Lais Date: Sat, 18 Dec 2021 15:36:15 +0100 Subject: [PATCH] Ad function description --- day-18/day-18.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/day-18/day-18.py b/day-18/day-18.py index 3b1bd5b..acbde30 100644 --- a/day-18/day-18.py +++ b/day-18/day-18.py @@ -3,6 +3,15 @@ from pathlib import Path def parse(line): + """This function parses an input line to nested tuples of length 2. It works by stripping the + outermost bracket-pair from the string and counting the 'depth' of the current brackets. After + we got back to the initial level 0 the next comma marks the index where the left and right part + are seperated. Those will be parsed by a recursive parse call each. If there is no comma on a + line, we are on the deepest level of this branch and return the value as integer. Else the left + and right return value will be returned in a tuple. The result will bea binary tree of the + snailfish number. + Alternatives for parsing would be eval() - which can be unsafe and is slow - or json.parse(). + """ if ',' not in line: return int(line) line = line[1:-1]