main
Nikita Tokarchuk 3 years ago
parent 1ce9084686
commit c7e4b797a0
Signed by: mainnika
GPG Key ID: A595FB7E3E56911C
  1. 6
      CMakeLists.txt
  2. 86
      main.cpp

@ -0,0 +1,6 @@
cmake_minimum_required(VERSION 3.21)
project(clock_numbers)
set(CMAKE_CXX_STANDARD 14)
add_executable(clock_numbers main.cpp)

@ -0,0 +1,86 @@
#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;
}
Loading…
Cancel
Save