diff --git a/2016/puzzle-23-02.cc b/2016/puzzle-23-02.cc index 8f99a06..0a28376 100644 --- a/2016/puzzle-23-02.cc +++ b/2016/puzzle-23-02.cc @@ -5,11 +5,12 @@ #include #include #include +#include #include #include enum class Register { a, b, c, d }; -Register to_register(char c) +auto to_register(char c) -> Register { switch (c) { case 'a': @@ -27,7 +28,7 @@ Register to_register(char c) enum class Opcode { cpy, inc, dec, jnz, tgl }; -Opcode to_opcode(std::string const& s) +auto to_opcode(std::string const& s) -> Opcode { if (s == "cpy") { return Opcode::cpy; @@ -47,7 +48,7 @@ Opcode to_opcode(std::string const& s) abort(); } -using Int = long; +using Int = long; // NOLINT(google-runtime-int) using Operand = std::variant; struct Instruction @@ -57,9 +58,9 @@ struct Instruction { } - Opcode opcode() const noexcept { return opcode_; } - Operand const& op1() const noexcept { return op1_; } - Operand const& op2() const noexcept { return op2_; } + [[nodiscard]] auto opcode() const noexcept -> Opcode { return opcode_; } + [[nodiscard]] auto op1() const noexcept -> Operand const& { return op1_; } + [[nodiscard]] auto op2() const noexcept -> Operand const& { return op2_; } void toggle() { @@ -89,8 +90,10 @@ struct Instruction { char* end{nullptr}; - auto val{std::strtol(s.data() + idx, &end, 10)}; - if (end != s.data() + idx) { + auto val{ + std::strtol(s.data() + idx, // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) + &end, 10)}; + if (end != s.data() + idx) { // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) op1 = val; idx = end - s.data(); } @@ -103,9 +106,11 @@ struct Instruction if (idx < s.size()) { assert(s[idx] == ' '); ++idx; - char* end{0}; - auto val{std::strtol(s.data() + idx, &end, 10)}; - if (end != s.data() + idx) { + char* end{nullptr}; + auto val{ + std::strtol(s.data() + idx, // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) + &end, 10)}; + if (end != s.data() + idx) { // NOLINT(cppcoreguidelines-pro-bounds-pointer-arithmetic) op2 = val; } else { @@ -113,7 +118,7 @@ struct Instruction } } - return Instruction(opcode, op1, op2); + return {opcode, op1, op2}; } private: @@ -124,10 +129,19 @@ private: struct State { - State(std::vector const& instructions) : instructions_(instructions) {} + explicit State(std::vector instructions) : instructions_(std::move(instructions)) {} - auto reg(Register r) const noexcept -> Int { return registers_[static_cast(r)]; } - void reg(Register r, Int v) noexcept { registers_[static_cast(r)] = v; } + [[nodiscard]] auto reg(Register r) const noexcept -> Int + { + return registers_ + [static_cast( // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) + r)]; + } + void reg(Register r, Int v) noexcept + { + registers_[static_cast( // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) + r)] = v; + } void execute() { @@ -161,10 +175,12 @@ struct State } private: - Int value(Operand const& o) + auto value(Operand const& o) -> Int { if (std::holds_alternative(o)) { - return registers_[static_cast(std::get(o))]; + return registers_ + [static_cast( // NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) + std::get(o))]; } if (std::holds_alternative(o)) { return std::get(o); @@ -175,7 +191,9 @@ private: void set(Operand const& dest, Int value) { if (std::holds_alternative(dest)) { - registers_[static_cast(std::get(dest))] = value; + registers_[// NOLINT(cppcoreguidelines-pro-bounds-constant-array-index) + static_cast(std::get(dest))] = + value; } }