From 6e28207b97502f1c00c91a06f4d5826369dca77a Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Tue, 9 Jan 2024 18:35:54 +0100 Subject: better solution for day 19 --- 2023/src/day19.cpp | 36 ++++++++++++------------------------ 1 file 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>> ru long result = 0; - bool run_trough = true; for ( const auto& [key, cmp, value, target]: sub_rules ) { - const auto [lo, hi] = ranges[key]; - pair T; // NOLINT - pair F; // NOLINT + auto sub_ranges = ranges; + auto [lo, hi] = ranges[key]; + if ( cmp == "<" ) { - T = { lo, min(value - 1, hi) }; - F = { max(value, lo), hi }; - } - else { - T = { max(value + 1, lo), hi }; - F = { lo, min(value, hi) }; - } - if ( T.first <= T.second ) { - auto copy = ranges; - copy[key] = T; - result += count(copy, target); - } - if ( F.first <= F.second ) { - ranges[key] = F; + sub_ranges[key] = { lo, value - 1 }; + ranges[key] = { value, hi }; } - else { - run_trough = false; - break; + else /* if ( cmp == ">" ) */ { + ranges[key] = { lo, value }; + sub_ranges[key] = { value + 1, hi }; } + + result += count(sub_ranges, target); } - if ( run_trough ) { - result += count(ranges, fallback); - } + + result += count(ranges, fallback); return result; }; -- cgit v1.3