aboutsummaryrefslogtreecommitdiff
path: root/2023
diff options
context:
space:
mode:
Diffstat (limited to '2023')
-rw-r--r--2023/src/day19.cpp36
1 files changed, 12 insertions, 24 deletions
diff --git a/2023/src/day19.cpp b/2023/src/day19.cpp
index a1d4eab..192e066 100644
--- a/2023/src/day19.cpp
+++ b/2023/src/day19.cpp
@@ -101,35 +101,23 @@ part2(map<string, tuple<vector<tuple<string, string, long, string>>, string>> ru
101 101
102 long result = 0; 102 long result = 0;
103 103
104 bool run_trough = true;
105 for ( const auto& [key, cmp, value, target]: sub_rules ) { 104 for ( const auto& [key, cmp, value, target]: sub_rules ) {
106 const auto [lo, hi] = ranges[key]; 105 auto sub_ranges = ranges;
107 pair<long, long> T; // NOLINT 106 auto [lo, hi] = ranges[key];
108 pair<long, long> F; // NOLINT 107
109 if ( cmp == "<" ) { 108 if ( cmp == "<" ) {
110 T = { lo, min(value - 1, hi) }; 109 sub_ranges[key] = { lo, value - 1 };
111 F = { max(value, lo), hi }; 110 ranges[key] = { value, hi };
112 }
113 else {
114 T = { max(value + 1, lo), hi };
115 F = { lo, min(value, hi) };
116 }
117 if ( T.first <= T.second ) {
118 auto copy = ranges;
119 copy[key] = T;
120 result += count(copy, target);
121 }
122 if ( F.first <= F.second ) {
123 ranges[key] = F;
124 } 111 }
125 else { 112 else /* if ( cmp == ">" ) */ {
126 run_trough = false; 113 ranges[key] = { lo, value };
127 break; 114 sub_ranges[key] = { value + 1, hi };
128 } 115 }
116
117 result += count(sub_ranges, target);
129 } 118 }
130 if ( run_trough ) { 119
131 result += count(ranges, fallback); 120 result += count(ranges, fallback);
132 }
133 121
134 return result; 122 return result;
135 }; 123 };