diff options
| author | Thomas Schmucker <ts@its1.de> | 2024-11-15 16:57:33 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2024-11-15 16:57:33 +0100 |
| commit | 566dbf51f27b6e181826b8de6c1cb98b4a680fe8 (patch) | |
| tree | 6b618efcaf38875172f611f774507c9a873e3edc /2015/src/day12.cpp | |
| parent | 6f75463d197bf7802cca4059e404ed98b1c12062 (diff) | |
| download | advent-of-code-566dbf51f27b6e181826b8de6c1cb98b4a680fe8.tar.gz advent-of-code-566dbf51f27b6e181826b8de6c1cb98b4a680fe8.tar.bz2 advent-of-code-566dbf51f27b6e181826b8de6c1cb98b4a680fe8.zip | |
aoc 2015, days 12, 13 and 14
Diffstat (limited to '2015/src/day12.cpp')
| -rw-r--r-- | 2015/src/day12.cpp | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/2015/src/day12.cpp b/2015/src/day12.cpp new file mode 100644 index 0000000..2bb2e78 --- /dev/null +++ b/2015/src/day12.cpp | |||
| @@ -0,0 +1,78 @@ | |||
| 1 | #include <fstream> | ||
| 2 | #include <iostream> | ||
| 3 | #include <iterator> | ||
| 4 | #include <string> | ||
| 5 | |||
| 6 | // JSON parsing | ||
| 7 | #include <nlohmann/json.hpp> | ||
| 8 | |||
| 9 | using namespace std; | ||
| 10 | using json = nlohmann::json; | ||
| 11 | |||
| 12 | string | ||
| 13 | read_file(string_view filename) | ||
| 14 | { | ||
| 15 | fstream input{ filename }; | ||
| 16 | return { istreambuf_iterator{ input }, {} }; | ||
| 17 | } | ||
| 18 | |||
| 19 | template<class UnaryFunction> | ||
| 20 | void | ||
| 21 | recursive_iterate(const json& j, UnaryFunction f) // NOLINT | ||
| 22 | { | ||
| 23 | for ( auto it = j.begin(); it != j.end(); ++it ) { | ||
| 24 | if ( it->is_structured() ) { | ||
| 25 | recursive_iterate(*it, f); | ||
| 26 | } | ||
| 27 | else { | ||
| 28 | f(it); | ||
| 29 | } | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | void | ||
| 34 | part1(string_view document) | ||
| 35 | { | ||
| 36 | auto json = json::parse(document); | ||
| 37 | |||
| 38 | long sum = 0; | ||
| 39 | recursive_iterate(json, [&](json::const_iterator iter) { | ||
| 40 | if ( iter.value().is_number() ) { | ||
| 41 | sum += iter.value().get<long>(); | ||
| 42 | } | ||
| 43 | }); | ||
| 44 | cout << sum << endl; | ||
| 45 | } | ||
| 46 | |||
| 47 | void | ||
| 48 | part2(string_view document) | ||
| 49 | { | ||
| 50 | json::parser_callback_t parser_callback = [](int /*depth*/, json::parse_event_t event, json& parsed) { | ||
| 51 | if ( event == json::parse_event_t::object_end ) { | ||
| 52 | for ( auto it = parsed.begin(); it != parsed.end(); ++it ) { | ||
| 53 | if ( it.value().is_string() && it.value().get<string>() == "red" ) { | ||
| 54 | return false; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | } | ||
| 58 | return true; | ||
| 59 | }; | ||
| 60 | |||
| 61 | auto json = json::parse(document, parser_callback); | ||
| 62 | |||
| 63 | long sum = 0; | ||
| 64 | recursive_iterate(json, [&](json::const_iterator iter) { | ||
| 65 | if ( iter.value().is_number() ) { | ||
| 66 | sum += iter.value().get<long>(); | ||
| 67 | } | ||
| 68 | }); | ||
| 69 | cout << sum << endl; | ||
| 70 | } | ||
| 71 | |||
| 72 | int | ||
| 73 | main() | ||
| 74 | { | ||
| 75 | auto document = read_file("data/day12.txt"); | ||
| 76 | part1(document); | ||
| 77 | part2(document); | ||
| 78 | } | ||
