Advent Of Code — JSAbacusFramework.io — Puzzle 12

Part 1

The Problem

Solution

#include <nlohmann/json.hpp>
std::ifstream file (pathToJsonDocument);
nlohmann::json jsonDocument;
file >> jsonDocument;
int getSum (const nlohmann::json& jsonDocument)
{
auto sum{0};
for (const auto& [key, value] : jsonDocument.items())
{
if(value.is_number())
{
sum += value.get<int>();
}
else if(value.is_array() || value.is_object())
{
sum += getSum (value);
}
}
return sum;
}

Part 2

Problem

Solution

for (const auto& [key, value] : jsonDocument.items())
{
// other conditions
else if(value.is_string() && jsonDocument.is_object())
{
if(value.get<std::string>() == "red")
{
sum = 0;
break;
}
}
}

Conclusion

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Help us solve the developing world’s energy needs with software

ForVag com Cable kkl 409.1

Dependency Injection and Inversion of Control

.NET Core-Part 4: Continuous Code Quality with Sonar Cloud

Who hasn’t joined our discord #FinanceProtocol ?

Python’s Collections Module — High-performance container data types.

Kotlin LinkedHashMap

Interface in Java

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Xavier Jouvenot

Xavier Jouvenot

More from Medium

C OPERATORS-9 TYPES WITH DETAILED EXPLANATION

C- Programming : Operators, Precedence and type conversion

C Programming Language Algorithm Examples/Exercises

A Byte of Coding Issue #205