Advertisement
Guest User

Untitled

a guest
Jan 11th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 1.02 KB | None | 0 0
  1. module main;
  2.  
  3. import std.stdio;
  4. import std.json;
  5. import std.conv;
  6. import std.file;
  7. import core.exception;
  8. import std.string;
  9.  
  10. int sum(JSONValue j) {
  11.     int result = 0;
  12.     if(j.type() == JSON_TYPE.STRING)
  13.         return 0;
  14.     else if(j.type() == JSON_TYPE.INTEGER)
  15.         return cast(int)j.integer;
  16.     else if(j.type() == JSON_TYPE.OBJECT) {
  17.         string keys[] = j.object.keys;
  18.         foreach(key;keys) {
  19.             JSONValue obj = j.object[key];
  20.             if(obj.type() == JSON_TYPE.STRING && obj.toString() == "\"red\"")
  21.                 return 0;
  22.         }
  23.         foreach(key;keys) {
  24.             JSONValue obj = j.object[key];
  25.             result += sum(obj);
  26.         }
  27.     }
  28.     else if(j.type() == JSON_TYPE.ARRAY) {
  29.         foreach(obj;j.array) {
  30.             result += sum(obj);
  31.         }
  32.     }
  33.     return result;
  34. }
  35.  
  36. int main(string[] args)
  37. {
  38.     char[] jsonString = cast(char[])read("input.txt");
  39.     JSONValue j = parseJSON(jsonString);
  40.     writeln("Sum: ", sum(j));
  41.  
  42.     return 0;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement