From 0c77ec229fb715d91ad4b64fb37ff295542f6e93 Mon Sep 17 00:00:00 2001 From: dzwdz Date: Tue, 13 Dec 2022 14:27:23 +0100 Subject: day 13 part 1 --- 22.13/main.cpp | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 22.13/main.cpp (limited to '22.13/main.cpp') 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 +#include +#include +#include +#include +#include +#include +using namespace std; + +struct El +{ + variant>, int> v; + + template + El(T v) + { + this->v = v; + } + + void + print() + { + if (holds_alternative(v)) { + cout << get(v); + } else { + cout << "["; + for (auto &e : *get>>(v)) { + e.print(); + cout << ","; + } + cout << "]"; + } + } +}; + +El +parse(istream &s) +{ + char c = s.get(); + if (c == '[') { + auto v = make_unique>(); + 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(); +} -- cgit 1.4.1-2-gfad0