From 3ce3a813e312bc61455dd73f32d6ae13bdf78c35 Mon Sep 17 00:00:00 2001 From: Thomas Schmucker Date: Thu, 4 Jan 2024 13:37:37 +0100 Subject: branchless solution --- 2020/src/day02.cpp | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to '2020/src/day02.cpp') diff --git a/2020/src/day02.cpp b/2020/src/day02.cpp index d7e28f1..cb70054 100644 --- a/2020/src/day02.cpp +++ b/2020/src/day02.cpp @@ -30,9 +30,7 @@ part1(vector>& data) long sum = 0; for ( const auto& [min, max, chr, password]: data ) { const auto amount = count(password.begin(), password.end(), chr); - if ( min <= amount && amount <= max ) { - ++sum; - } + sum += static_cast(min <= amount && amount <= max); } cout << sum << endl; } @@ -42,9 +40,7 @@ part2(vector>& data) { long sum = 0; for ( const auto& [min, max, chr, password]: data ) { - if ( (password[size_t(min - 1)] == chr) ^ (password[size_t(max - 1)] == chr) ) { - ++sum; - } + sum += ((password[size_t(min - 1)] == chr) ^ (password[size_t(max - 1)] == chr)); } cout << sum << endl; } -- cgit v1.3