diff options
| author | Thomas Schmucker <ts@its1.de> | 2024-01-09 18:35:54 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2024-01-09 18:35:54 +0100 |
| commit | 6e28207b97502f1c00c91a06f4d5826369dca77a (patch) | |
| tree | 53792a1e901ac921934d6d27f30c0ee26eecfc6a /2023/src/day19.cpp | |
| parent | 542b8eed6d970ebf5fdc05964268ffb48e868069 (diff) | |
| download | advent-of-code-6e28207b97502f1c00c91a06f4d5826369dca77a.tar.gz advent-of-code-6e28207b97502f1c00c91a06f4d5826369dca77a.tar.bz2 advent-of-code-6e28207b97502f1c00c91a06f4d5826369dca77a.zip | |
better solution for day 19
Diffstat (limited to '2023/src/day19.cpp')
| -rw-r--r-- | 2023/src/day19.cpp | 36 |
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 | }; |
