From 2d399a8e8eb3b69a35c03bcb5058970d5e25ae76 Mon Sep 17 00:00:00 2001 From: Wataru Otsubo Date: Wed, 2 Oct 2024 17:33:38 +0900 Subject: [PATCH] init --- .clang-format | 2 ++ .clangd | 2 ++ .gitignore | 1 + Makefile | 8 +++++ main.cc | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 96 insertions(+) create mode 100644 .clang-format create mode 100644 .clangd create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 main.cc diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..80d3293 --- /dev/null +++ b/.clang-format @@ -0,0 +1,2 @@ +BasedOnStyle: LLVM +IndentWidth: 4 diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..6418b4e --- /dev/null +++ b/.clangd @@ -0,0 +1,2 @@ +CompileFlags: + Add: [-std=c++23] diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba2906d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +main diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8fd1957 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +CFLAGS = -Wall -Wextra + +main: main.cc + g++ -std=c++23 $(CFLAGS) -o $@ $^ + +.PHONY: clean +clean: + -rm main diff --git a/main.cc b/main.cc new file mode 100644 index 0000000..d816a42 --- /dev/null +++ b/main.cc @@ -0,0 +1,83 @@ +#include +#include +#include +// #include +#include +#include +#include +#include +#include +#include + +auto parse_line(std::string line) -> std::tuple { + auto is = std::stringstream{line}; + double_t time, high, low; + is >> time >> high >> low; + return std::tuple{time, high}; +} + +int main(int argc, char **argv) { + if (argc != 2) { + // std::println("Usage: {} ", argv[0]); + exit(0); + } + auto file = std::ifstream{argv[1]}; + + // main logic + + auto re = std::regex{"^\\d+\\.\\d+ \\d+\\.\\d+ \\d+\\.\\d+$"}; + std::string expectedFirst = "10.000000000000000 0.000000 0.000000"; + std::string expectedLast = "16.000000000000000 1000.000000 0.000000"; + + bool is_in_high = false; + bool is_first_line = true; + bool is_valid_last_line = false; + double_t last_low_time = 0; + double_t riseup_time = 0; + int32_t rise_count = 0; + std::string line; + while (std::getline(file, line)) { + if (is_first_line) { + if (line != expectedFirst) { + std::println("Invalid first line: {}", line); + return 2; + } + is_first_line = false; + } + if (!std::regex_match(line, re)) { + std::println("Invalid line!: {}", line); + return 2; + } + is_valid_last_line = (line == expectedLast); + + auto temp = parse_line(line); + auto time = std::get<0>(temp); + auto high = std::get<1>(temp); + + if (high == 0) { + last_low_time = time; + } else if (!is_in_high && high > 500) { + riseup_time = time; + rise_count++; + is_in_high = true; + } else if (is_in_high && high < 500) { + is_in_high = false; + } + } + if (!is_valid_last_line) { + std::println("Invalid last line: {}", line); + return 2; + } + if (rise_count > 1) { + std::println("Too many rise counts: {}", rise_count); + return 2; + } + + auto transition_is_too_long = (riseup_time - last_low_time) > (1. / 57 * 6); + if (transition_is_too_long) { + std::println("Too long transition: {} ns", riseup_time - last_low_time); + return 2; + } + + return 1; +}