diff options
| author | Thomas Schmucker <ts@its1.de> | 2024-01-04 13:37:37 +0100 |
|---|---|---|
| committer | Thomas Schmucker <ts@its1.de> | 2024-01-04 13:37:37 +0100 |
| commit | 3ce3a813e312bc61455dd73f32d6ae13bdf78c35 (patch) | |
| tree | 9796d1ebe535e0f9dbd8f6348af48c901667eafe | |
| parent | 52ac586dcd7c78184d5884b0587f6b2c6fae98c4 (diff) | |
| download | advent-of-code-3ce3a813e312bc61455dd73f32d6ae13bdf78c35.tar.gz advent-of-code-3ce3a813e312bc61455dd73f32d6ae13bdf78c35.tar.bz2 advent-of-code-3ce3a813e312bc61455dd73f32d6ae13bdf78c35.zip | |
branchless solution
| -rw-r--r-- | 2020/src/day02.cpp | 8 |
1 files changed, 2 insertions, 6 deletions
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<tuple<long, long, char, string>>& data) | |||
| 30 | long sum = 0; | 30 | long sum = 0; |
| 31 | for ( const auto& [min, max, chr, password]: data ) { | 31 | for ( const auto& [min, max, chr, password]: data ) { |
| 32 | const auto amount = count(password.begin(), password.end(), chr); | 32 | const auto amount = count(password.begin(), password.end(), chr); |
| 33 | if ( min <= amount && amount <= max ) { | 33 | sum += static_cast<long>(min <= amount && amount <= max); |
| 34 | ++sum; | ||
| 35 | } | ||
| 36 | } | 34 | } |
| 37 | cout << sum << endl; | 35 | cout << sum << endl; |
| 38 | } | 36 | } |
| @@ -42,9 +40,7 @@ part2(vector<tuple<long, long, char, string>>& data) | |||
| 42 | { | 40 | { |
| 43 | long sum = 0; | 41 | long sum = 0; |
| 44 | for ( const auto& [min, max, chr, password]: data ) { | 42 | for ( const auto& [min, max, chr, password]: data ) { |
| 45 | if ( (password[size_t(min - 1)] == chr) ^ (password[size_t(max - 1)] == chr) ) { | 43 | sum += ((password[size_t(min - 1)] == chr) ^ (password[size_t(max - 1)] == chr)); |
| 46 | ++sum; | ||
| 47 | } | ||
| 48 | } | 44 | } |
| 49 | cout << sum << endl; | 45 | cout << sum << endl; |
| 50 | } | 46 | } |
