summary refs log tree commit diff
path: root/22.13/main.cpp
diff options
context:
space:
mode:
authordzwdz2022-12-13 14:27:23 +0100
committerdzwdz2022-12-13 14:27:23 +0100
commit0c77ec229fb715d91ad4b64fb37ff295542f6e93 (patch)
treeb1cf2537b4fd52b5cc167321094722bbbdc7d1c7 /22.13/main.cpp
parent26dd7e867ab392771b4b9a095ac409a18b5e7a71 (diff)
day 13 part 1
Diffstat (limited to '22.13/main.cpp')
-rw-r--r--22.13/main.cpp68
1 files changed, 68 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();
+}