aboutsummaryrefslogtreecommitdiff
path: root/2023
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2024-01-09 23:23:29 +0100
committerThomas Schmucker <ts@its1.de>2024-01-09 23:23:29 +0100
commitb68f250e3a88113362eeb628db5e1f9c3b7bfc5b (patch)
tree99662d9ef3dc768904cb48e850e4bb5579f6535c /2023
parent6e28207b97502f1c00c91a06f4d5826369dca77a (diff)
downloadadvent-of-code-b68f250e3a88113362eeb628db5e1f9c3b7bfc5b.tar.gz
advent-of-code-b68f250e3a88113362eeb628db5e1f9c3b7bfc5b.tar.bz2
advent-of-code-b68f250e3a88113362eeb628db5e1f9c3b7bfc5b.zip
even more compact code
Diffstat (limited to '2023')
-rw-r--r--2023/src/day19.cpp13
1 files changed, 6 insertions, 7 deletions
diff --git a/2023/src/day19.cpp b/2023/src/day19.cpp
index 192e066..78306c9 100644
--- a/2023/src/day19.cpp
+++ b/2023/src/day19.cpp
@@ -2,6 +2,7 @@
2#include <functional> 2#include <functional>
3#include <iostream> 3#include <iostream>
4#include <map> 4#include <map>
5#include <numeric>
5#include <regex> 6#include <regex>
6#include <string> 7#include <string>
7#include <vector> 8#include <vector>
@@ -83,18 +84,16 @@ part1(map<string, tuple<vector<tuple<string, string, long, string>>, string>> ru
83void 84void
84part2(map<string, tuple<vector<tuple<string, string, long, string>>, string>> rules) 85part2(map<string, tuple<vector<tuple<string, string, long, string>>, string>> rules)
85{ 86{
86 function<long(map<string, tuple<long, long>>, string)> count = [&](map<string, tuple<long, long>> ranges, const string& name) -> long { 87 function<long(map<string, tuple<long, long>>, string)> count = [&](auto ranges, const string& name) -> long {
87 if ( name == "R" ) { 88 if ( name == "R" ) {
88 return 0; 89 return 0;
89 } 90 }
90 91
91 if ( name == "A" ) { 92 if ( name == "A" ) {
92 long result = 1; 93 return accumulate(ranges.begin(), ranges.end(), 1L, [](auto lhs, auto rhs) {
93 for ( const auto& range: ranges ) { 94 const auto [lo, hi] = rhs.second;
94 const auto [lo, hi] = range.second; 95 return lhs * (hi - lo + 1);
95 result *= hi - lo + 1; 96 });
96 }
97 return result;
98 } 97 }
99 98
100 const auto [sub_rules, fallback] = rules[name]; 99 const auto [sub_rules, fallback] = rules[name];