Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module main;
- import std.stdio;
- import std.json;
- import std.conv;
- import std.file;
- import core.exception;
- import std.string;
- int sum(JSONValue j) {
- int result = 0;
- if(j.type() == JSON_TYPE.STRING)
- return 0;
- else if(j.type() == JSON_TYPE.INTEGER)
- return cast(int)j.integer;
- else if(j.type() == JSON_TYPE.OBJECT) {
- string keys[] = j.object.keys;
- foreach(key;keys) {
- JSONValue obj = j.object[key];
- if(obj.type() == JSON_TYPE.STRING && obj.toString() == "\"red\"")
- return 0;
- }
- foreach(key;keys) {
- JSONValue obj = j.object[key];
- result += sum(obj);
- }
- }
- else if(j.type() == JSON_TYPE.ARRAY) {
- foreach(obj;j.array) {
- result += sum(obj);
- }
- }
- return result;
- }
- int main(string[] args)
- {
- char[] jsonString = cast(char[])read("input.txt");
- JSONValue j = parseJSON(jsonString);
- writeln("Sum: ", sum(j));
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement