diff options
Diffstat (limited to '22.13')
-rw-r--r-- | 22.13/main.cpp | 68 | ||||
-rw-r--r-- | 22.13/main.py | 30 |
2 files changed, 98 insertions, 0 deletions
diff --git a/22.13/main.cpp b/22.13/main.cpp new file mode 100644 index 0000000..4ed0e84 --- /dev/null +++ b/22.13/main.cpp @@ -0,0 +1,68 @@ +/* broken, i don't understand c++ templates */ + +#include <cctype> +#include <iostream> +#include <memory> +#include <stdexcept> +#include <string> +#include <variant> +#include <vector> +using namespace std; + +struct El +{ + variant<unique_ptr<vector<El>>, int> v; + + template<typename T> + El(T v) + { + this->v = v; + } + + void + print() + { + if (holds_alternative<int>(v)) { + cout << get<int>(v); + } else { + cout << "["; + for (auto &e : *get<unique_ptr<vector<El>>>(v)) { + e.print(); + cout << ","; + } + cout << "]"; + } + } +}; + +El +parse(istream &s) +{ + char c = s.get(); + if (c == '[') { + auto v = make_unique<vector<El>>(); + for (;;) { + v->push_back(parse(s)); + do { + c = s.get(); + } while (isspace(c)); + if (c == ']') break; + if (c != ',') throw invalid_argument("array sep"); + } + return El(move(v)); + } + if (isdigit(c)) { + int v; + s.putback(c); + s >> v; + return El(v); + } + throw invalid_argument("unexpected char"); +} + +int +main() +{ + El a = parse(cin); + a.print(); +} diff --git a/22.13/main.py b/22.13/main.py new file mode 100644 index 0000000..25b09a1 --- /dev/null +++ b/22.13/main.py @@ -0,0 +1,30 @@ +import json +import sys + +lines = [json.loads(s) for s in sys.stdin.readlines() if s.strip()] + +def cmp(a, b): + def tolist(v): + return v if type(v) == list else [v] + i = 0 + while True: + if len(a) <= i and len(b) <= i: return 0 + if len(a) <= i: return -1 + if len(b) <= i: return 1 + x = a[i] + y = b[i] + if type(x) == int and type(y) == int: + if x < y: return -1 + if x > y: return 1 + else: + r = cmp(tolist(x), tolist(y)) + if r != 0: return r + i += 1 + +partOne = 0 +for i in range(0, len(lines), 2): + a = lines[i] + b = lines[i+1] + if cmp(a,b) != 1: + partOne += i//2+1 +print(partOne) |