yet another hackerrank task
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
clock-numbers/main.cpp

86 lines
1.8 KiB

#include <iostream>
#include <array>
#include <functional>
#include <unordered_set>
typedef union {
struct {
uint8_t hours_tens;
uint8_t hours_ones;
uint8_t minutes_tens;
uint8_t minutes_ones;
} clk;
uint32_t data;
} clk_t;
template<>
struct std::hash<clk_t> {
std::size_t operator()(clk_t const &clk) const noexcept {
return clk.data;
}
};
template<>
struct std::equal_to<clk_t> {
bool operator()(clk_t const &lhs, clk_t const &rhs) const noexcept {
return lhs.data == rhs.data;
}
};
bool is_clock_valid(clk_t const &clk) {
auto hours_tens = clk.clk.hours_tens;
if (hours_tens > 2) {
return false;
}
auto hours_ones = clk.clk.hours_ones;
if (hours_tens == 2 && hours_ones > 3) {
return false;
}
auto minutes_tens = clk.clk.minutes_tens;
if (minutes_tens > 5) {
return false;
}
return true;
}
int solution(int A, int B, int C, int D) {
uint8_t a = A, b = B, c = C, d = D;
std::unordered_set<clk_t> uniqs = {
{a, b, c, d},
{a, b, d, c},
{a, c, b, d},
{a, c, d, b},
{a, d, b, c},
{a, d, c, b},
{b, a, c, d},
{b, a, d, c},
{b, c, a, d},
{b, c, d, a},
{b, d, a, c},
{b, d, c, a},
{c, a, b, d},
{c, a, d, b},
{c, b, a, d},
{c, b, d, a},
{c, d, a, b},
{c, d, b, a},
{d, a, b, c},
{d, a, c, b},
{d, b, a, c},
{d, b, c, a},
{d, c, a, b},
{d, c, b, a},
};
return std::count_if(uniqs.begin(), uniqs.end(), is_clock_valid);
}
int main() {
std::cout << solution(2, 3, 3, 2) << std::endl;
return 0;
}