From cd5e2538df75d468bba9966bedf0aaf5cd06077a Mon Sep 17 00:00:00 2001 From: Matthew Gretton-Dann Date: Thu, 2 Dec 2021 07:18:16 +0000 Subject: [PATCH] Add .clang-format and apply it --- .clang-format | 54 +++++++ 2015/puzzle-01-01.cc | 3 +- 2015/puzzle-01-02.cc | 3 +- 2015/puzzle-02-01.cc | 12 +- 2015/puzzle-02-02.cc | 15 +- 2015/puzzle-03-01.cc | 16 +- 2015/puzzle-03-02.cc | 19 ++- 2015/puzzle-04-01.cc | 25 +-- 2015/puzzle-04-02.cc | 25 +-- 2015/puzzle-05-01.cc | 10 +- 2015/puzzle-05-02.cc | 6 +- 2015/puzzle-06-01.cc | 43 +++-- 2015/puzzle-06-02.cc | 43 +++-- 2015/puzzle-07-01.cc | 172 +++++++++++--------- 2015/puzzle-07-02.cc | 172 +++++++++++--------- 2015/puzzle-08-01.cc | 23 +-- 2015/puzzle-08-02.cc | 11 +- 2015/puzzle-09-01.cc | 12 +- 2015/puzzle-09-02.cc | 19 ++- 2015/puzzle-10-01.cc | 12 +- 2015/puzzle-10-02.cc | 12 +- 2015/puzzle-11-01.cc | 24 +-- 2015/puzzle-11-02.cc | 21 ++- 2015/puzzle-12-01.cc | 6 +- 2015/puzzle-12-02.cc | 27 ++-- 2015/puzzle-13-01.cc | 25 +-- 2015/puzzle-13-02.cc | 25 +-- 2015/puzzle-14-01.cc | 13 +- 2015/puzzle-14-02.cc | 43 ++--- 2015/puzzle-15-01.cc | 50 +++--- 2015/puzzle-15-02.cc | 50 +++--- 2015/puzzle-16-01.cc | 9 +- 2015/puzzle-16-02.cc | 9 +- 2015/puzzle-17-01.cc | 12 +- 2015/puzzle-17-02.cc | 51 +++--- 2015/puzzle-18-01.cc | 37 +++-- 2015/puzzle-18-02.cc | 40 ++--- 2015/puzzle-19-01.cc | 15 +- 2015/puzzle-19-02.cc | 38 +++-- 2015/puzzle-20-01.cc | 58 +++---- 2015/puzzle-20-02.cc | 65 ++++---- 2015/puzzle-21-01.cc | 207 ++++++++++++------------ 2015/puzzle-21-02.cc | 204 ++++++++++++------------ 2015/puzzle-22-01.cc | 358 +++++++++++++++++++++-------------------- 2015/puzzle-22-02.cc | 372 +++++++++++++++++++++++-------------------- 2015/puzzle-23-01.cc | 241 ++++++++++++++-------------- 2015/puzzle-23-02.cc | 241 ++++++++++++++-------------- 2015/puzzle-24-01.cc | 131 ++++++++------- 2015/puzzle-24-02.cc | 131 ++++++++------- 2015/puzzle-25-01.cc | 72 ++++----- 2020/puzzle-01-01.cc | 3 +- 2020/puzzle-01-02.cc | 7 +- 2020/puzzle-02-01.cc | 15 +- 2020/puzzle-02-02.cc | 19 ++- 2020/puzzle-03-01.cc | 6 +- 2020/puzzle-03-02.cc | 6 +- 2020/puzzle-04-01.cc | 23 +-- 2020/puzzle-04-02.cc | 72 +++++---- 2020/puzzle-05-01.cc | 24 +-- 2020/puzzle-05-02.cc | 36 +++-- 2020/puzzle-06-01.cc | 22 +-- 2020/puzzle-06-02.cc | 21 ++- 2020/puzzle-07-01.cc | 28 ++-- 2020/puzzle-07-02.cc | 20 ++- 2020/puzzle-08-01.cc | 45 ++++-- 2020/puzzle-08-02.cc | 54 ++++--- 2020/puzzle-09-01.cc | 13 +- 2020/puzzle-09-02.cc | 22 +-- 2020/puzzle-10-01.cc | 6 +- 2020/puzzle-10-02.cc | 6 +- 2020/puzzle-11-01.cc | 20 ++- 2020/puzzle-11-02.cc | 24 +-- 2020/puzzle-12-01.cc | 24 ++- 2020/puzzle-12-02.cc | 28 ++-- 2020/puzzle-13-01.cc | 9 +- 2020/puzzle-13-02.cc | 13 +- 2020/puzzle-14-01.cc | 19 ++- 2020/puzzle-14-02.cc | 30 ++-- 2020/puzzle-15-01.cc | 12 +- 2020/puzzle-15-02.cc | 12 +- 2020/puzzle-16-01.cc | 27 ++-- 2020/puzzle-16-02.cc | 68 ++++---- 2020/puzzle-17-01.cc | 24 +-- 2020/puzzle-17-02.cc | 26 +-- 2020/puzzle-18-01.cc | 47 +++--- 2020/puzzle-18-02.cc | 50 +++--- 2020/puzzle-19-01.cc | 36 +++-- 2020/puzzle-19-02.cc | 38 +++-- 2020/puzzle-20-01.cc | 74 +++++---- 2020/puzzle-20-02.cc | 109 +++++++------ 2020/puzzle-21-01.cc | 33 ++-- 2020/puzzle-21-02.cc | 47 +++--- 2020/puzzle-22-01.cc | 18 ++- 2020/puzzle-22-02.cc | 38 +++-- 2020/puzzle-23-01.cc | 19 +-- 2020/puzzle-23-02.cc | 48 +++--- 2020/puzzle-24-01.cc | 15 +- 2020/puzzle-24-02.cc | 46 ++++-- 2020/puzzle-25-01.cc | 12 +- 2021/puzzle-01-01.cc | 27 ++-- 2021/puzzle-01-02.cc | 41 ++--- 2021/puzzle-02-01.cc | 38 ++--- 2021/puzzle-02-02.cc | 44 ++--- 103 files changed, 2714 insertions(+), 2132 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..1428611 --- /dev/null +++ b/.clang-format @@ -0,0 +1,54 @@ +--- +AlwaysBreakTemplateDeclarations: true +BreakBeforeBraces: Custom +BraceWrapping: + AfterClass: true + AfterFunction: true + AfterStruct: true + AfterUnion: true + IndentBraces: false + BeforeElse: true + BeforeCatch: true +BreakStringLiterals: true +ColumnLimit: 100 +CompactNamespaces: true +ContinuationIndentWidth: 2 +Cpp11BracedListStyle: true +FixNamespaceComments: true +IncludeBlocks: Regroup +IncludeCategories: + - Regex: '^".*-messages\.hh"$' + Priority: 3 + - Regex: '' + Priority: 100 +IndentPPDirectives: AfterHash +IndentWidth: 2 +IndentWrappedFunctionNames: false +KeepEmptyLinesAtTheStartOfBlocks: false +Language: Cpp +MaxEmptyLinesToKeep: 1 +NamespaceIndentation: None +PointerAlignment: Left +ReflowComments: true +SortIncludes: true +SortUsingDeclarations: true +SpaceAfterCStyleCast: false +SpaceAfterLogicalNot: false +SpaceAfterTemplateKeyword: false +SpaceBeforeAssignmentOperators: true +SpaceBeforeCpp11BracedList: false +SpaceBeforeCtorInitializerColon: true +SpaceBeforeInheritanceColon: true +SpaceBeforeParens: ControlStatements +SpacesBeforeTrailingComments: 2 +SpacesInAngles: false +Standard: Cpp11 +... diff --git a/2015/puzzle-01-01.cc b/2015/puzzle-01-01.cc index ec8d52e..b66b9ed 100644 --- a/2015/puzzle-01-01.cc +++ b/2015/puzzle-01-01.cc @@ -2,7 +2,8 @@ #include #include -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ for (std::string line; std::getline(std::cin, line);) { int floor = 0; for (auto c : line) { diff --git a/2015/puzzle-01-02.cc b/2015/puzzle-01-02.cc index 804c132..fcafe91 100644 --- a/2015/puzzle-01-02.cc +++ b/2015/puzzle-01-02.cc @@ -2,7 +2,8 @@ #include #include -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ for (std::string line; std::getline(std::cin, line);) { int floor = 0; std::string::size_type pos = 0; diff --git a/2015/puzzle-02-01.cc b/2015/puzzle-02-01.cc index a5c4c8c..df7c0d7 100644 --- a/2015/puzzle-02-01.cc +++ b/2015/puzzle-02-01.cc @@ -3,11 +3,13 @@ #include #include -struct Box { +struct Box +{ /** Construct box. * \param s String representation of dimensions 'lxwxh' */ - Box(std::string const &s) { + Box(std::string const& s) + { std::size_t pos = 0; l_ = std::stoul(s, &pos, 10); assert(s[pos] == 'x'); @@ -20,7 +22,8 @@ struct Box { } // How much paper does this box need? - unsigned long paper_needed() const { + unsigned long paper_needed() const + { unsigned long s1 = l_ * w_; unsigned long s2 = w_ * h_; unsigned long s3 = h_ * l_; @@ -32,7 +35,8 @@ struct Box { unsigned long h_; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ unsigned long total = 0; for (std::string line; std::getline(std::cin, line);) { Box b(line); diff --git a/2015/puzzle-02-02.cc b/2015/puzzle-02-02.cc index 3eea86d..d086298 100644 --- a/2015/puzzle-02-02.cc +++ b/2015/puzzle-02-02.cc @@ -3,11 +3,13 @@ #include #include -struct Box { +struct Box +{ /** Construct box. * \param s String representation of dimensions 'lxwxh' */ - Box(std::string const &s) { + Box(std::string const& s) + { std::size_t pos = 0; l_ = std::stoul(s, &pos, 10); assert(s[pos] == 'x'); @@ -20,7 +22,8 @@ struct Box { } // How much paper does this box need? - unsigned long paper_needed() const { + unsigned long paper_needed() const + { unsigned long s1 = l_ * w_; unsigned long s2 = w_ * h_; unsigned long s3 = h_ * l_; @@ -28,7 +31,8 @@ struct Box { } // How much ribbon do we need? - unsigned long ribbon_needed() const { + unsigned long ribbon_needed() const + { // The various side perimeters - we want the min of these multiplied by // volume. unsigned long p1 = 2 * (l_ + w_); @@ -42,7 +46,8 @@ struct Box { unsigned long h_; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ unsigned long total = 0; for (std::string line; std::getline(std::cin, line);) { Box b(line); diff --git a/2015/puzzle-03-01.cc b/2015/puzzle-03-01.cc index 15e696d..0276ef1 100644 --- a/2015/puzzle-03-01.cc +++ b/2015/puzzle-03-01.cc @@ -4,18 +4,19 @@ #include #include -struct Pos { +struct Pos +{ Pos(int x, int y) : x_(x), y_(y) {} - bool operator<(Pos const &rhs) const noexcept { + bool operator<(Pos const& rhs) const noexcept + { return x_ < rhs.x_ || (x_ == rhs.x_ && y_ < rhs.y_); } - bool operator==(Pos const &rhs) const noexcept { - return x_ == rhs.x_ && y_ == rhs.y_; - } + bool operator==(Pos const& rhs) const noexcept { return x_ == rhs.x_ && y_ == rhs.y_; } - void move(char c) { + void move(char c) + { switch (c) { case '>': ++x_; @@ -39,7 +40,8 @@ struct Pos { int y_; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ for (std::string line; std::getline(std::cin, line);) { std::set visited; Pos santa(0, 0); diff --git a/2015/puzzle-03-02.cc b/2015/puzzle-03-02.cc index 556d6b5..e913c08 100644 --- a/2015/puzzle-03-02.cc +++ b/2015/puzzle-03-02.cc @@ -4,18 +4,19 @@ #include #include -struct Pos { +struct Pos +{ Pos(int x, int y) : x_(x), y_(y) {} - bool operator<(Pos const &rhs) const noexcept { + bool operator<(Pos const& rhs) const noexcept + { return x_ < rhs.x_ || (x_ == rhs.x_ && y_ < rhs.y_); } - bool operator==(Pos const &rhs) const noexcept { - return x_ == rhs.x_ && y_ == rhs.y_; - } + bool operator==(Pos const& rhs) const noexcept { return x_ == rhs.x_ && y_ == rhs.y_; } - void move(char c) noexcept { + void move(char c) noexcept + { switch (c) { case '>': ++x_; @@ -39,7 +40,8 @@ struct Pos { int y_; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ for (std::string line; std::getline(std::cin, line);) { std::set visited; Pos santa(0, 0); @@ -51,7 +53,8 @@ int main(int argc, char **argv) { if (do_robo) { robo_santa.move(c); visited.insert(robo_santa); - } else { + } + else { santa.move(c); visited.insert(santa); } diff --git a/2015/puzzle-04-01.cc b/2015/puzzle-04-01.cc index 73b5df3..972c0a6 100644 --- a/2015/puzzle-04-01.cc +++ b/2015/puzzle-04-01.cc @@ -8,26 +8,29 @@ using MD5Digest = unsigned char[EVP_MAX_MD_SIZE]; -unsigned int md5(MD5Digest digest, std::string const &s) { - EVP_MD const* md{EVP_md5()}; - unsigned int md_len; +unsigned int md5(MD5Digest digest, std::string const& s) +{ + EVP_MD const* md{EVP_md5()}; + unsigned int md_len; - EVP_MD_CTX* md_ctxt{EVP_MD_CTX_new()}; - assert(md_ctxt != NULL); - EVP_DigestInit_ex2(md_ctxt, md, NULL); - EVP_DigestUpdate(md_ctxt, s.data(), s.length()); - EVP_DigestFinal_ex(md_ctxt, digest, &md_len); - return md_len; + EVP_MD_CTX* md_ctxt{EVP_MD_CTX_new()}; + assert(md_ctxt != NULL); + EVP_DigestInit_ex2(md_ctxt, md, NULL); + EVP_DigestUpdate(md_ctxt, s.data(), s.length()); + EVP_DigestFinal_ex(md_ctxt, digest, &md_len); + return md_len; } -bool is_valid(std::string const &s) { +bool is_valid(std::string const& s) +{ MD5Digest digest; auto len = md5(digest, s); assert(len >= 3); return digest[0] == 0 && digest[1] == 0 && (digest[2] & 0xf0) == 0; } -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ for (std::string line; std::getline(std::cin, line);) { unsigned i = 0; while (!is_valid(line + std::to_string(i))) { diff --git a/2015/puzzle-04-02.cc b/2015/puzzle-04-02.cc index d5f613a..b4847a5 100644 --- a/2015/puzzle-04-02.cc +++ b/2015/puzzle-04-02.cc @@ -8,26 +8,29 @@ using MD5Digest = unsigned char[EVP_MAX_MD_SIZE]; -unsigned int md5(MD5Digest digest, std::string const &s) { - EVP_MD const* md{EVP_md5()}; - unsigned int md_len; +unsigned int md5(MD5Digest digest, std::string const& s) +{ + EVP_MD const* md{EVP_md5()}; + unsigned int md_len; - EVP_MD_CTX* md_ctxt{EVP_MD_CTX_new()}; - assert(md_ctxt != NULL); - EVP_DigestInit_ex2(md_ctxt, md, NULL); - EVP_DigestUpdate(md_ctxt, s.data(), s.length()); - EVP_DigestFinal_ex(md_ctxt, digest, &md_len); - return md_len; + EVP_MD_CTX* md_ctxt{EVP_MD_CTX_new()}; + assert(md_ctxt != NULL); + EVP_DigestInit_ex2(md_ctxt, md, NULL); + EVP_DigestUpdate(md_ctxt, s.data(), s.length()); + EVP_DigestFinal_ex(md_ctxt, digest, &md_len); + return md_len; } -bool is_valid(std::string const &s) { +bool is_valid(std::string const& s) +{ MD5Digest digest; auto len = md5(digest, s); assert(len >= 3); return digest[0] == 0 && digest[1] == 0 && digest[2] == 0; } -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ for (std::string line; std::getline(std::cin, line);) { unsigned i = 0; while (!is_valid(line + std::to_string(i))) { diff --git a/2015/puzzle-05-01.cc b/2015/puzzle-05-01.cc index c919a06..c11473d 100644 --- a/2015/puzzle-05-01.cc +++ b/2015/puzzle-05-01.cc @@ -9,7 +9,8 @@ // >=3 vowels // At least one double letter // No instances of 'ab', 'cd', 'pq', or 'xy'. -bool is_nice(std::string const &s) noexcept { +bool is_nice(std::string const& s) noexcept +{ unsigned vowel_count = 0; bool repeated = false; char last = '\0'; @@ -22,8 +23,8 @@ bool is_nice(std::string const &s) noexcept { if (c == last) { repeated = true; } - if ((last == 'a' && c == 'b') || (last == 'c' && c == 'd') || - (last == 'p' && c == 'q') || (last == 'x' && c == 'y')) { + if ((last == 'a' && c == 'b') || (last == 'c' && c == 'd') || (last == 'p' && c == 'q') || + (last == 'x' && c == 'y')) { return false; } last = c; @@ -32,7 +33,8 @@ bool is_nice(std::string const &s) noexcept { return repeated && vowel_count >= 3; } -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ unsigned nice_strings; for (std::string line; std::getline(std::cin, line);) { nice_strings += is_nice(line); diff --git a/2015/puzzle-05-02.cc b/2015/puzzle-05-02.cc index e63c908..780e2fa 100644 --- a/2015/puzzle-05-02.cc +++ b/2015/puzzle-05-02.cc @@ -8,7 +8,8 @@ // Nice strings have: // repeated double letters - but not overlapping // repeated letters separated by one other. -bool is_nice(std::string const &s) noexcept { +bool is_nice(std::string const& s) noexcept +{ bool repeated_pair = false; bool repeated_sep = false; @@ -34,7 +35,8 @@ bool is_nice(std::string const &s) noexcept { return repeated_pair && repeated_sep; } -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ unsigned nice_strings; for (std::string line; std::getline(std::cin, line);) { nice_strings += is_nice(line); diff --git a/2015/puzzle-06-01.cc b/2015/puzzle-06-01.cc index a55357f..6729df3 100644 --- a/2015/puzzle-06-01.cc +++ b/2015/puzzle-06-01.cc @@ -10,10 +10,11 @@ enum class Action { TurnOn, Toggle, TurnOff }; using Point = std::pair; /// A command -struct Command { - Command(std::string const &s) { - const char *re = - "(turn on|toggle|turn off)\\s(\\d+),(\\d+)\\sthrough\\s(\\d+),(\\d+)"; +struct Command +{ + Command(std::string const& s) + { + const char* re = "(turn on|toggle|turn off)\\s(\\d+),(\\d+)\\sthrough\\s(\\d+),(\\d+)"; std::smatch m; if (!std::regex_search(s, m, std::regex(re))) { std::cerr << "Unable to interpret:" << s << "\n"; @@ -21,11 +22,14 @@ struct Command { } if (m.str(1) == std::string("turn on")) { act_ = Action::TurnOn; - } else if (m.str(1) == std::string("turn off")) { + } + else if (m.str(1) == std::string("turn off")) { act_ = Action::TurnOff; - } else if (m.str(1) == std::string("toggle")) { + } + else if (m.str(1) == std::string("toggle")) { act_ = Action::Toggle; - } else { + } + else { assert(false); } bottom_left_.first = std::stoul(m.str(2), nullptr, 10); @@ -40,8 +44,11 @@ struct Command { }; /// Array of lights -template struct Array { - Array() noexcept { +template +struct Array +{ + Array() noexcept + { for (unsigned i = 0; i < N; ++i) { for (unsigned j = 0; j < N; ++j) { lights_[i][j] = false; @@ -50,16 +57,15 @@ template struct Array { } /// Apply a command - void apply(Command const &command) noexcept { + void apply(Command const& command) noexcept + { assert(command.bottom_left_.first < N); assert(command.bottom_left_.second < N); assert(command.top_right_.first < N); assert(command.top_right_.second < N); - for (unsigned i = command.bottom_left_.first; i <= command.top_right_.first; - ++i) { - for (unsigned j = command.bottom_left_.second; - j <= command.top_right_.second; ++j) { + for (unsigned i = command.bottom_left_.first; i <= command.top_right_.first; ++i) { + for (unsigned j = command.bottom_left_.second; j <= command.top_right_.second; ++j) { switch (command.act_) { case Action::TurnOn: lights_[i][j] = true; @@ -76,7 +82,8 @@ template struct Array { } /// How many lights are on - unsigned num_on() const noexcept { + unsigned num_on() const noexcept + { unsigned count = 0; for (unsigned i = 0; i < N; ++i) { for (unsigned j = 0; j < N; ++j) { @@ -88,7 +95,8 @@ template struct Array { } /// Output a bitmap - void bitmap() const { + void bitmap() const + { std::cout << "P1\n" << N << " " << N << "\n"; for (unsigned i = 0; i < N; ++i) { for (unsigned j = 0; j < N; ++j) { @@ -104,7 +112,8 @@ template struct Array { bool lights_[N][N]; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ Array<1000> arr; for (std::string line; std::getline(std::cin, line);) { Command cmd(line); diff --git a/2015/puzzle-06-02.cc b/2015/puzzle-06-02.cc index ffd9f35..96a0e76 100644 --- a/2015/puzzle-06-02.cc +++ b/2015/puzzle-06-02.cc @@ -7,10 +7,11 @@ enum class Action { TurnOn, Toggle, TurnOff }; using Point = std::pair; -struct Command { - Command(std::string const &s) { - const char *re = - "(turn on|toggle|turn off)\\s(\\d+),(\\d+)\\sthrough\\s(\\d+),(\\d+)"; +struct Command +{ + Command(std::string const& s) + { + const char* re = "(turn on|toggle|turn off)\\s(\\d+),(\\d+)\\sthrough\\s(\\d+),(\\d+)"; std::smatch m; if (!std::regex_search(s, m, std::regex(re))) { std::cerr << "Unable to interpret:" << s << "\n"; @@ -18,11 +19,14 @@ struct Command { } if (m.str(1) == std::string("turn on")) { act_ = Action::TurnOn; - } else if (m.str(1) == std::string("turn off")) { + } + else if (m.str(1) == std::string("turn off")) { act_ = Action::TurnOff; - } else if (m.str(1) == std::string("toggle")) { + } + else if (m.str(1) == std::string("toggle")) { act_ = Action::Toggle; - } else { + } + else { assert(false); } bottom_left_.first = std::stoul(m.str(2), nullptr, 10); @@ -36,8 +40,11 @@ struct Command { Point top_right_; }; -template struct Array { - Array() noexcept { +template +struct Array +{ + Array() noexcept + { for (unsigned i = 0; i < N; ++i) { for (unsigned j = 0; j < N; ++j) { lights_[i][j] = 0; @@ -45,16 +52,15 @@ template struct Array { } } - void apply(Command const &command) noexcept { + void apply(Command const& command) noexcept + { assert(command.bottom_left_.first < N); assert(command.bottom_left_.second < N); assert(command.top_right_.first < N); assert(command.top_right_.second < N); - for (unsigned i = command.bottom_left_.first; i <= command.top_right_.first; - ++i) { - for (unsigned j = command.bottom_left_.second; - j <= command.top_right_.second; ++j) { + for (unsigned i = command.bottom_left_.first; i <= command.top_right_.first; ++i) { + for (unsigned j = command.bottom_left_.second; j <= command.top_right_.second; ++j) { switch (command.act_) { case Action::TurnOn: ++lights_[i][j]; @@ -72,7 +78,8 @@ template struct Array { } } - unsigned brightness() const noexcept { + unsigned brightness() const noexcept + { unsigned count = 0; for (unsigned i = 0; i < N; ++i) { for (unsigned j = 0; j < N; ++j) { @@ -83,7 +90,8 @@ template struct Array { } /// Output a bitmap - void bitmap() const { + void bitmap() const + { unsigned max = 0; for (unsigned i = 0; i < N; ++i) { for (unsigned j = 0; j < N; ++j) { @@ -103,7 +111,8 @@ template struct Array { unsigned lights_[N][N]; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ Array<1000> arr; for (std::string line; std::getline(std::cin, line);) { Command cmd(line); diff --git a/2015/puzzle-07-01.cc b/2015/puzzle-07-01.cc index d54873b..424de79 100644 --- a/2015/puzzle-07-01.cc +++ b/2015/puzzle-07-01.cc @@ -24,22 +24,28 @@ // implementation) // helper type for the visitor #4 -template struct Overloaded : Ts... { using Ts::operator()...; }; +template +struct Overloaded : Ts... +{ + using Ts::operator()...; +}; // explicit deduction guide (not needed as of C++20) -template Overloaded(Ts...) -> Overloaded; +template +Overloaded(Ts...) -> Overloaded; /// Instruction action enum class Action { - Set, ///< Set value - And, ///< And two values - Or, ///< Or two values - LShift, ///< Left shift - RShift, ///< Right shift - Not ///< Bitwise not + Set, ///< Set value + And, ///< And two values + Or, ///< Or two values + LShift, ///< Left shift + RShift, ///< Right shift + Not ///< Bitwise not }; /// Pretty-print action -std::ostream &operator<<(std::ostream &os, Action act) { +std::ostream& operator<<(std::ostream& os, Action act) +{ switch (act) { case Action::Set: os << "SET"; @@ -63,22 +69,24 @@ std::ostream &operator<<(std::ostream &os, Action act) { return os; } -using Value = std::uint16_t; ///< Value -using Wire = std::string; ///< Wire name (string) -using Signal = std::variant; ///< Either a wire or explicit value +using Value = std::uint16_t; ///< Value +using Wire = std::string; ///< Wire name (string) +using Signal = std::variant; ///< Either a wire or explicit value /// Outputter for a signal -std::ostream &operator<<(std::ostream &os, Signal const &signal) { +std::ostream& operator<<(std::ostream& os, Signal const& signal) +{ return std::visit( - [&os](auto &&arg) -> std::ostream & { - os << arg; - return os; - }, - signal); + [&os](auto&& arg) -> std::ostream& { + os << arg; + return os; + }, + signal); } /** \brief An instruction. */ -struct Instruction { +struct Instruction +{ /** \brief Construct an instruction. * * \subsection Grammar @@ -92,7 +100,8 @@ struct Instruction { * binop := signal op signal '->' wire * instr := binop | not | set */ - Instruction(std::string const &s) { + Instruction(std::string const& s) + { if (parse_bin_op(s)) { return; } @@ -110,20 +119,22 @@ struct Instruction { Action action() const noexcept { return act_; } /// Get the destination wire - Wire const &dest() const noexcept { return dest_; } + Wire const& dest() const noexcept { return dest_; } /// Get the first (or only) source - Signal const &src1() const noexcept { return src1_; } + Signal const& src1() const noexcept { return src1_; } /// Get the second source - Signal const &src2() const noexcept { + Signal const& src2() const noexcept + { assert(act_ != Action::Set && act_ != Action::Not); return src2_; } private: /// Parse a instruction. Return true if successful. - bool parse_not(std::string const &s) { + bool parse_not(std::string const& s) + { if (s.substr(0, 4) == "NOT ") { std::string::size_type pos = 4; while (s[pos] == ' ') { @@ -135,7 +146,8 @@ private: } /// Parse a instruction. Return true if successful. - bool parse_bin_op(std::string const &s) { + bool parse_bin_op(std::string const& s) + { static const std::regex re("^([[:lower:][:digit:]]+) ([[:upper:]]+) " "([[:lower:][:digit:]]+) -> ([[:lower:]]+)"); std::smatch m; @@ -145,13 +157,17 @@ private: if (m.str(2) == "AND") { act_ = Action::And; - } else if (m.str(2) == "OR") { + } + else if (m.str(2) == "OR") { act_ = Action::Or; - } else if (m.str(2) == "LSHIFT") { + } + else if (m.str(2) == "LSHIFT") { act_ = Action::LShift; - } else if (m.str(2) == "RSHIFT") { + } + else if (m.str(2) == "RSHIFT") { act_ = Action::RShift; - } else { + } + else { return false; } dest_ = m.str(4); @@ -165,7 +181,8 @@ private: /// /// Also used for the latter half of parsing. ACT tells you what is /// being parsed. Returns true if parsing successful. - bool parse_set(std::string const &s, Action act = Action::Set) { + bool parse_set(std::string const& s, Action act = Action::Set) + { static const std::regex re("^([[:lower:][:digit:]]+) -> ([[:lower:]]+)"); std::smatch m; if (!std::regex_search(s, m, re)) { @@ -179,23 +196,26 @@ private: } /// Make a Signal from a string. - Signal make_signal(std::string const &s) { + Signal make_signal(std::string const& s) + { if (std::isdigit(s[0])) { auto u = std::stoul(s, nullptr, 10); assert(u <= UINT16_MAX); return Signal(static_cast(u)); - } else { + } + else { return Signal(s); } } - Action act_; ///< Action - Wire dest_; ///< Destination wire - Signal src1_, src2_; ///< Source signals + Action act_; ///< Action + Wire dest_; ///< Destination wire + Signal src1_, src2_; ///< Source signals }; /// Outputter for an instruction. -std::ostream &operator<<(std::ostream &os, Instruction const &instr) { +std::ostream& operator<<(std::ostream& os, Instruction const& instr) +{ os << instr.action() << " " << instr.dest() << ", " << instr.src1(); if (instr.action() != Action::Set && instr.action() != Action::Not) { os << ", " << instr.src2(); @@ -204,56 +224,58 @@ std::ostream &operator<<(std::ostream &os, Instruction const &instr) { } /// Ma -using ValueMap = std::map; ///< Map wires to values -using Instructions = std::vector; ///< Instructions to execute +using ValueMap = std::map; ///< Map wires to values +using Instructions = std::vector; ///< Instructions to execute -struct VM { +struct VM +{ /// Add an instruction the the list we have - void add_instr(Instruction const &instr) { instrs_.push_back(instr); } + void add_instr(Instruction const& instr) { instrs_.push_back(instr); } /// Has this wire a known value? - bool has_value(Wire const &w) const noexcept { - return values_.find(w) != values_.end(); - } + bool has_value(Wire const& w) const noexcept { return values_.find(w) != values_.end(); } /// Has this signal a known value? - bool has_value(Signal const &s) const noexcept { - return std::visit(Overloaded{[](Value v) { return true; }, - [&](Wire const &w) { return has_value(w); }}, - s); + bool has_value(Signal const& s) const noexcept + { + return std::visit( + Overloaded{[](Value v) { return true; }, [&](Wire const& w) { return has_value(w); }}, s); } /// Get the value on the wire - Value value(Wire const &w) const noexcept { + Value value(Wire const& w) const noexcept + { assert(has_value(w)); return values_.find(w)->second; } /// Get the value of a signal - Value value(Signal const &s) const noexcept { - return std::visit(Overloaded{[](Value v) { return v; }, - [&](Wire const &w) { return value(w); }}, - s); + Value value(Signal const& s) const noexcept + { + return std::visit( + Overloaded{[](Value v) { return v; }, [&](Wire const& w) { return value(w); }}, s); } /// Set the value of a wire - void value(Wire const &w, Value value) { + void value(Wire const& w, Value value) + { auto [it, success] = values_.insert({w, value}); assert(success); } /// Set the value of a signal - void value(Signal const &s, Value v) { - std::visit(Overloaded{[v](Value v2) { assert(v == v2); }, - [&, v](Wire const &w) { value(w, v); }}, - s); + void value(Signal const& s, Value v) + { + std::visit( + Overloaded{[v](Value v2) { assert(v == v2); }, [&, v](Wire const& w) { value(w, v); }}, s); } /// Execute the instructions. Returns true if we have updated some wire /// values. - bool execute() { + bool execute() + { bool done_anything = false; - for (auto const &instr : instrs_) { + for (auto const& instr : instrs_) { done_anything |= execute_instr(instr); } @@ -268,15 +290,15 @@ private: * An instruction may not be executed if the incoming signals have not been * set yet. */ - bool execute_instr(Instruction const &instr) { + bool execute_instr(Instruction const& instr) + { std::cout << instr << " # "; // First of all check there is something to do - i.e. that the destination // register has not been set already. Wire dest = instr.dest(); if (has_value(dest)) { - std::cout << "already has value: " << dest << " = " << value(dest) - << "\n"; + std::cout << "already has value: " << dest << " = " << value(dest) << "\n"; return false; } @@ -286,17 +308,13 @@ private: case Action::Not: return execute_single_src(instr, [](Value src) { return ~src; }); case Action::And: - return execute_double_src( - instr, [](Value src1, Value src2) { return src1 & src2; }); + return execute_double_src(instr, [](Value src1, Value src2) { return src1 & src2; }); case Action::Or: - return execute_double_src( - instr, [](Value src1, Value src2) { return src1 | src2; }); + return execute_double_src(instr, [](Value src1, Value src2) { return src1 | src2; }); case Action::LShift: - return execute_double_src( - instr, [](Value src1, Value src2) { return src1 << src2; }); + return execute_double_src(instr, [](Value src1, Value src2) { return src1 << src2; }); case Action::RShift: - return execute_double_src( - instr, [](Value src1, Value src2) { return src1 >> src2; }); + return execute_double_src(instr, [](Value src1, Value src2) { return src1 >> src2; }); } return false; @@ -307,8 +325,8 @@ private: * \param fn How to modify the source value to the dest. * \return True if we executed the function. */ - bool execute_single_src(Instruction const &instr, - std::function fn) { + bool execute_single_src(Instruction const& instr, std::function fn) + { Wire dest = instr.dest(); Signal src = instr.src1(); if (has_value(src)) { @@ -326,8 +344,8 @@ private: * \param fn How to modify the source values to the dest. * \return True if we executed the function. */ - bool execute_double_src(Instruction const &instr, - std::function fn) { + bool execute_double_src(Instruction const& instr, std::function fn) + { Wire dest = instr.dest(); Signal src1 = instr.src1(); Signal src2 = instr.src2(); @@ -345,7 +363,8 @@ private: Instructions instrs_; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ VM vm; // Parse the input @@ -365,7 +384,8 @@ int main(int argc, char **argv) { std::cout << "a = "; if (!vm.has_value(a)) { std::cout << "UNSET\n"; - } else { + } + else { std::cout << vm.value(a) << "\n"; } diff --git a/2015/puzzle-07-02.cc b/2015/puzzle-07-02.cc index d54873b..424de79 100644 --- a/2015/puzzle-07-02.cc +++ b/2015/puzzle-07-02.cc @@ -24,22 +24,28 @@ // implementation) // helper type for the visitor #4 -template struct Overloaded : Ts... { using Ts::operator()...; }; +template +struct Overloaded : Ts... +{ + using Ts::operator()...; +}; // explicit deduction guide (not needed as of C++20) -template Overloaded(Ts...) -> Overloaded; +template +Overloaded(Ts...) -> Overloaded; /// Instruction action enum class Action { - Set, ///< Set value - And, ///< And two values - Or, ///< Or two values - LShift, ///< Left shift - RShift, ///< Right shift - Not ///< Bitwise not + Set, ///< Set value + And, ///< And two values + Or, ///< Or two values + LShift, ///< Left shift + RShift, ///< Right shift + Not ///< Bitwise not }; /// Pretty-print action -std::ostream &operator<<(std::ostream &os, Action act) { +std::ostream& operator<<(std::ostream& os, Action act) +{ switch (act) { case Action::Set: os << "SET"; @@ -63,22 +69,24 @@ std::ostream &operator<<(std::ostream &os, Action act) { return os; } -using Value = std::uint16_t; ///< Value -using Wire = std::string; ///< Wire name (string) -using Signal = std::variant; ///< Either a wire or explicit value +using Value = std::uint16_t; ///< Value +using Wire = std::string; ///< Wire name (string) +using Signal = std::variant; ///< Either a wire or explicit value /// Outputter for a signal -std::ostream &operator<<(std::ostream &os, Signal const &signal) { +std::ostream& operator<<(std::ostream& os, Signal const& signal) +{ return std::visit( - [&os](auto &&arg) -> std::ostream & { - os << arg; - return os; - }, - signal); + [&os](auto&& arg) -> std::ostream& { + os << arg; + return os; + }, + signal); } /** \brief An instruction. */ -struct Instruction { +struct Instruction +{ /** \brief Construct an instruction. * * \subsection Grammar @@ -92,7 +100,8 @@ struct Instruction { * binop := signal op signal '->' wire * instr := binop | not | set */ - Instruction(std::string const &s) { + Instruction(std::string const& s) + { if (parse_bin_op(s)) { return; } @@ -110,20 +119,22 @@ struct Instruction { Action action() const noexcept { return act_; } /// Get the destination wire - Wire const &dest() const noexcept { return dest_; } + Wire const& dest() const noexcept { return dest_; } /// Get the first (or only) source - Signal const &src1() const noexcept { return src1_; } + Signal const& src1() const noexcept { return src1_; } /// Get the second source - Signal const &src2() const noexcept { + Signal const& src2() const noexcept + { assert(act_ != Action::Set && act_ != Action::Not); return src2_; } private: /// Parse a instruction. Return true if successful. - bool parse_not(std::string const &s) { + bool parse_not(std::string const& s) + { if (s.substr(0, 4) == "NOT ") { std::string::size_type pos = 4; while (s[pos] == ' ') { @@ -135,7 +146,8 @@ private: } /// Parse a instruction. Return true if successful. - bool parse_bin_op(std::string const &s) { + bool parse_bin_op(std::string const& s) + { static const std::regex re("^([[:lower:][:digit:]]+) ([[:upper:]]+) " "([[:lower:][:digit:]]+) -> ([[:lower:]]+)"); std::smatch m; @@ -145,13 +157,17 @@ private: if (m.str(2) == "AND") { act_ = Action::And; - } else if (m.str(2) == "OR") { + } + else if (m.str(2) == "OR") { act_ = Action::Or; - } else if (m.str(2) == "LSHIFT") { + } + else if (m.str(2) == "LSHIFT") { act_ = Action::LShift; - } else if (m.str(2) == "RSHIFT") { + } + else if (m.str(2) == "RSHIFT") { act_ = Action::RShift; - } else { + } + else { return false; } dest_ = m.str(4); @@ -165,7 +181,8 @@ private: /// /// Also used for the latter half of parsing. ACT tells you what is /// being parsed. Returns true if parsing successful. - bool parse_set(std::string const &s, Action act = Action::Set) { + bool parse_set(std::string const& s, Action act = Action::Set) + { static const std::regex re("^([[:lower:][:digit:]]+) -> ([[:lower:]]+)"); std::smatch m; if (!std::regex_search(s, m, re)) { @@ -179,23 +196,26 @@ private: } /// Make a Signal from a string. - Signal make_signal(std::string const &s) { + Signal make_signal(std::string const& s) + { if (std::isdigit(s[0])) { auto u = std::stoul(s, nullptr, 10); assert(u <= UINT16_MAX); return Signal(static_cast(u)); - } else { + } + else { return Signal(s); } } - Action act_; ///< Action - Wire dest_; ///< Destination wire - Signal src1_, src2_; ///< Source signals + Action act_; ///< Action + Wire dest_; ///< Destination wire + Signal src1_, src2_; ///< Source signals }; /// Outputter for an instruction. -std::ostream &operator<<(std::ostream &os, Instruction const &instr) { +std::ostream& operator<<(std::ostream& os, Instruction const& instr) +{ os << instr.action() << " " << instr.dest() << ", " << instr.src1(); if (instr.action() != Action::Set && instr.action() != Action::Not) { os << ", " << instr.src2(); @@ -204,56 +224,58 @@ std::ostream &operator<<(std::ostream &os, Instruction const &instr) { } /// Ma -using ValueMap = std::map; ///< Map wires to values -using Instructions = std::vector; ///< Instructions to execute +using ValueMap = std::map; ///< Map wires to values +using Instructions = std::vector; ///< Instructions to execute -struct VM { +struct VM +{ /// Add an instruction the the list we have - void add_instr(Instruction const &instr) { instrs_.push_back(instr); } + void add_instr(Instruction const& instr) { instrs_.push_back(instr); } /// Has this wire a known value? - bool has_value(Wire const &w) const noexcept { - return values_.find(w) != values_.end(); - } + bool has_value(Wire const& w) const noexcept { return values_.find(w) != values_.end(); } /// Has this signal a known value? - bool has_value(Signal const &s) const noexcept { - return std::visit(Overloaded{[](Value v) { return true; }, - [&](Wire const &w) { return has_value(w); }}, - s); + bool has_value(Signal const& s) const noexcept + { + return std::visit( + Overloaded{[](Value v) { return true; }, [&](Wire const& w) { return has_value(w); }}, s); } /// Get the value on the wire - Value value(Wire const &w) const noexcept { + Value value(Wire const& w) const noexcept + { assert(has_value(w)); return values_.find(w)->second; } /// Get the value of a signal - Value value(Signal const &s) const noexcept { - return std::visit(Overloaded{[](Value v) { return v; }, - [&](Wire const &w) { return value(w); }}, - s); + Value value(Signal const& s) const noexcept + { + return std::visit( + Overloaded{[](Value v) { return v; }, [&](Wire const& w) { return value(w); }}, s); } /// Set the value of a wire - void value(Wire const &w, Value value) { + void value(Wire const& w, Value value) + { auto [it, success] = values_.insert({w, value}); assert(success); } /// Set the value of a signal - void value(Signal const &s, Value v) { - std::visit(Overloaded{[v](Value v2) { assert(v == v2); }, - [&, v](Wire const &w) { value(w, v); }}, - s); + void value(Signal const& s, Value v) + { + std::visit( + Overloaded{[v](Value v2) { assert(v == v2); }, [&, v](Wire const& w) { value(w, v); }}, s); } /// Execute the instructions. Returns true if we have updated some wire /// values. - bool execute() { + bool execute() + { bool done_anything = false; - for (auto const &instr : instrs_) { + for (auto const& instr : instrs_) { done_anything |= execute_instr(instr); } @@ -268,15 +290,15 @@ private: * An instruction may not be executed if the incoming signals have not been * set yet. */ - bool execute_instr(Instruction const &instr) { + bool execute_instr(Instruction const& instr) + { std::cout << instr << " # "; // First of all check there is something to do - i.e. that the destination // register has not been set already. Wire dest = instr.dest(); if (has_value(dest)) { - std::cout << "already has value: " << dest << " = " << value(dest) - << "\n"; + std::cout << "already has value: " << dest << " = " << value(dest) << "\n"; return false; } @@ -286,17 +308,13 @@ private: case Action::Not: return execute_single_src(instr, [](Value src) { return ~src; }); case Action::And: - return execute_double_src( - instr, [](Value src1, Value src2) { return src1 & src2; }); + return execute_double_src(instr, [](Value src1, Value src2) { return src1 & src2; }); case Action::Or: - return execute_double_src( - instr, [](Value src1, Value src2) { return src1 | src2; }); + return execute_double_src(instr, [](Value src1, Value src2) { return src1 | src2; }); case Action::LShift: - return execute_double_src( - instr, [](Value src1, Value src2) { return src1 << src2; }); + return execute_double_src(instr, [](Value src1, Value src2) { return src1 << src2; }); case Action::RShift: - return execute_double_src( - instr, [](Value src1, Value src2) { return src1 >> src2; }); + return execute_double_src(instr, [](Value src1, Value src2) { return src1 >> src2; }); } return false; @@ -307,8 +325,8 @@ private: * \param fn How to modify the source value to the dest. * \return True if we executed the function. */ - bool execute_single_src(Instruction const &instr, - std::function fn) { + bool execute_single_src(Instruction const& instr, std::function fn) + { Wire dest = instr.dest(); Signal src = instr.src1(); if (has_value(src)) { @@ -326,8 +344,8 @@ private: * \param fn How to modify the source values to the dest. * \return True if we executed the function. */ - bool execute_double_src(Instruction const &instr, - std::function fn) { + bool execute_double_src(Instruction const& instr, std::function fn) + { Wire dest = instr.dest(); Signal src1 = instr.src1(); Signal src2 = instr.src2(); @@ -345,7 +363,8 @@ private: Instructions instrs_; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ VM vm; // Parse the input @@ -365,7 +384,8 @@ int main(int argc, char **argv) { std::cout << "a = "; if (!vm.has_value(a)) { std::cout << "UNSET\n"; - } else { + } + else { std::cout << vm.value(a) << "\n"; } diff --git a/2015/puzzle-08-01.cc b/2015/puzzle-08-01.cc index a02cf9b..44025ef 100644 --- a/2015/puzzle-08-01.cc +++ b/2015/puzzle-08-01.cc @@ -8,7 +8,8 @@ enum class State { Begin, Normal, Escape, Hex1, Hex2, End }; -std::string unescape(std::string const &s) { +std::string unescape(std::string const& s) +{ std::string unescaped; static const std::string hex = "0123456789abcdef0123456789ABCDEF"; @@ -23,9 +24,11 @@ std::string unescape(std::string const &s) { case State::Normal: if (c == '\\') { state = State::Escape; - } else if (c == '"') { + } + else if (c == '"') { state = State::End; - } else { + } + else { unescaped += c; } break; @@ -33,10 +36,12 @@ std::string unescape(std::string const &s) { if (c == '\\' || c == '"') { state = State::Normal; unescaped += c; - } else if (c == 'x') { + } + else if (c == 'x') { byte = 0; state = State::Hex1; - } else { + } + else { assert(false); } break; @@ -62,16 +67,16 @@ std::string unescape(std::string const &s) { return unescaped; } -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ unsigned len = 0; // Parse the input for (std::string line; std::getline(std::cin, line);) { std::string unescaped = unescape(line); len += line.length() - unescaped.length(); - std::cout << line << ": " << line.length() << " written bytes, " - << unescaped.length() << " memory bytes, difference: " - << line.length() - unescaped.length() << "\n"; + std::cout << line << ": " << line.length() << " written bytes, " << unescaped.length() + << " memory bytes, difference: " << line.length() - unescaped.length() << "\n"; } std::cout << len << "\n"; diff --git a/2015/puzzle-08-02.cc b/2015/puzzle-08-02.cc index 1616099..7119819 100644 --- a/2015/puzzle-08-02.cc +++ b/2015/puzzle-08-02.cc @@ -6,7 +6,8 @@ #include #include -std::string escape(std::string const &s) { +std::string escape(std::string const& s) +{ std::string escaped; escaped += '"'; for (auto c : s) { @@ -19,16 +20,16 @@ std::string escape(std::string const &s) { return escaped; } -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ unsigned len = 0; // Parse the input for (std::string line; std::getline(std::cin, line);) { std::string escaped = escape(line); len += escaped.length() - line.length(); - std::cout << line << ": " << line.length() << " memory bytes, " - << escaped.length() << " escaped bytes, difference: " - << escaped.length() - line.length() << "\n"; + std::cout << line << ": " << line.length() << " memory bytes, " << escaped.length() + << " escaped bytes, difference: " << escaped.length() - line.length() << "\n"; } std::cout << len << "\n"; diff --git a/2015/puzzle-09-01.cc b/2015/puzzle-09-01.cc index 6976508..c154c88 100644 --- a/2015/puzzle-09-01.cc +++ b/2015/puzzle-09-01.cc @@ -13,8 +13,10 @@ using Edge = std::pair; using Nodes = std::set; using Edges = std::map; -struct Graph { - void add_edge(std::string const &s) { +struct Graph +{ + void add_edge(std::string const& s) + { static const std::regex re("(.+) to (.+) = (\\d+)"); std::smatch m; if (!std::regex_search(s, m, re)) { @@ -35,7 +37,8 @@ struct Graph { std::cout << n1 << " <-> " << n2 << " weight: " << w << "\n"; } - Weight solve_tsp() const { + Weight solve_tsp() const + { Weight min_weight = ~0U; std::vector nodes(nodes_.begin(), nodes_.end()); std::sort(nodes.begin(), nodes.end()); @@ -64,7 +67,8 @@ struct Graph { Edges weights_; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ Graph g; // Parse the input diff --git a/2015/puzzle-09-02.cc b/2015/puzzle-09-02.cc index b58a9e8..c4567d2 100644 --- a/2015/puzzle-09-02.cc +++ b/2015/puzzle-09-02.cc @@ -13,8 +13,10 @@ using Edge = std::pair; using Nodes = std::set; using Edges = std::map; -struct Graph { - void add_edge(std::string const &s) { +struct Graph +{ + void add_edge(std::string const& s) + { static const std::regex re("(.+) to (.+) = (\\d+)"); std::smatch m; if (!std::regex_search(s, m, re)) { @@ -35,7 +37,8 @@ struct Graph { std::cout << n1 << " <-> " << n2 << " weight: " << w << "\n"; } - Weight solve_tsp() const { + Weight solve_tsp() const + { Weight min_weight = ~0U; visit_all_perms([&min_weight](Weight w) { if (w < min_weight) { @@ -46,7 +49,8 @@ struct Graph { return min_weight; } - Weight solve_max_tsp() const { + Weight solve_max_tsp() const + { Weight max_weight = 0; visit_all_perms([&max_weight](Weight w) { if (w > max_weight) { @@ -58,7 +62,9 @@ struct Graph { } private: - template void visit_all_perms(Fn fn) const { + template + void visit_all_perms(Fn fn) const + { std::vector nodes(nodes_.begin(), nodes_.end()); std::sort(nodes.begin(), nodes.end()); do { @@ -80,7 +86,8 @@ private: Edges weights_; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ Graph g; // Parse the input diff --git a/2015/puzzle-10-01.cc b/2015/puzzle-10-01.cc index cf07dc0..aa004fa 100644 --- a/2015/puzzle-10-01.cc +++ b/2015/puzzle-10-01.cc @@ -7,7 +7,8 @@ #include #include -std::string look_and_say(std::string const &s) { +std::string look_and_say(std::string const& s) +{ std::string result; for (std::string::size_type i = 0; i < s.length();) { unsigned num = 0; @@ -22,14 +23,13 @@ std::string look_and_say(std::string const &s) { return result; } -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ for (std::string line; std::getline(std::cin, line);) { - std::cout << "Application 0, length = " << line.length() << ": " << line - << "\n"; + std::cout << "Application 0, length = " << line.length() << ": " << line << "\n"; for (int i = 1; i < 41; ++i) { line = look_and_say(line); - std::cout << "Application " << i << ", length = " << line.length() - << "\n"; + std::cout << "Application " << i << ", length = " << line.length() << "\n"; } std::cout << "Length: " << line.length() << "\n"; } diff --git a/2015/puzzle-10-02.cc b/2015/puzzle-10-02.cc index 3c9211b..d64e749 100644 --- a/2015/puzzle-10-02.cc +++ b/2015/puzzle-10-02.cc @@ -7,7 +7,8 @@ #include #include -std::string look_and_say(std::string const &s) { +std::string look_and_say(std::string const& s) +{ std::string result; for (std::string::size_type i = 0; i < s.length();) { unsigned num = 0; @@ -22,14 +23,13 @@ std::string look_and_say(std::string const &s) { return result; } -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ for (std::string line; std::getline(std::cin, line);) { - std::cout << "Application 0, length = " << line.length() << ": " << line - << "\n"; + std::cout << "Application 0, length = " << line.length() << ": " << line << "\n"; for (int i = 1; i < 51; ++i) { line = look_and_say(line); - std::cout << "Application " << i << ", length = " << line.length() - << "\n"; + std::cout << "Application " << i << ", length = " << line.length() << "\n"; } std::cout << "Length: " << line.length() << "\n"; } diff --git a/2015/puzzle-11-01.cc b/2015/puzzle-11-01.cc index c4ba99b..fbe8e53 100644 --- a/2015/puzzle-11-01.cc +++ b/2015/puzzle-11-01.cc @@ -9,7 +9,8 @@ bool illegal_char(char c) { return c == 'i' || c == 'l' || c == 'o'; } -void pre_advance_password(std::string &s) { +void pre_advance_password(std::string& s) +{ std::string::size_type pos = 0; while (pos < s.length() && !illegal_char(s[pos])) { ++pos; @@ -23,14 +24,16 @@ void pre_advance_password(std::string &s) { } } -void advance_password(std::string &s) { +void advance_password(std::string& s) +{ auto pos = s.length() - 1; while (true) { if (s[pos] == 'z') { assert(pos != 0); s[pos] = 'a'; pos -= 1; - } else { + } + else { ++s[pos]; if (illegal_char(s[pos])) { ++s[pos]; @@ -40,7 +43,8 @@ void advance_password(std::string &s) { } } -bool valid_password(std::string const &s) { +bool valid_password(std::string const& s) +{ unsigned double_count = 0; bool run = false; char last2 = '\0'; @@ -48,7 +52,8 @@ bool valid_password(std::string const &s) { for (auto c : s) { if (c == last && last2 != c) { ++double_count; - } else if (c == last + 1 && c == last2 + 2) { + } + else if (c == last + 1 && c == last2 + 2) { run = true; } last2 = last; @@ -58,7 +63,8 @@ bool valid_password(std::string const &s) { return double_count >= 2 && run; } -std::string next_password(std::string const &s) { +std::string next_password(std::string const& s) +{ std::string result = s; pre_advance_password(result); do { @@ -67,11 +73,11 @@ std::string next_password(std::string const &s) { return result; } -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ for (std::string line; std::getline(std::cin, line);) { std::string next = next_password(line); - std::cout << "Current password: " << line << "; Next password: " << next - << "\n"; + std::cout << "Current password: " << line << "; Next password: " << next << "\n"; } return 0; } \ No newline at end of file diff --git a/2015/puzzle-11-02.cc b/2015/puzzle-11-02.cc index 5722a11..e5ec028 100644 --- a/2015/puzzle-11-02.cc +++ b/2015/puzzle-11-02.cc @@ -9,7 +9,8 @@ bool illegal_char(char c) { return c == 'i' || c == 'l' || c == 'o'; } -void pre_advance_password(std::string &s) { +void pre_advance_password(std::string& s) +{ std::string::size_type pos = 0; while (pos < s.length() && !illegal_char(s[pos])) { ++pos; @@ -23,14 +24,16 @@ void pre_advance_password(std::string &s) { } } -void advance_password(std::string &s) { +void advance_password(std::string& s) +{ auto pos = s.length() - 1; while (true) { if (s[pos] == 'z') { assert(pos != 0); s[pos] = 'a'; pos -= 1; - } else { + } + else { ++s[pos]; if (illegal_char(s[pos])) { ++s[pos]; @@ -40,7 +43,8 @@ void advance_password(std::string &s) { } } -bool valid_password(std::string const &s) { +bool valid_password(std::string const& s) +{ unsigned double_count = 0; bool run = false; char last2 = '\0'; @@ -48,7 +52,8 @@ bool valid_password(std::string const &s) { for (auto c : s) { if (c == last && last2 != c) { ++double_count; - } else if (c == last + 1 && c == last2 + 2) { + } + else if (c == last + 1 && c == last2 + 2) { run = true; } last2 = last; @@ -58,7 +63,8 @@ bool valid_password(std::string const &s) { return double_count >= 2 && run; } -std::string next_password(std::string const &s) { +std::string next_password(std::string const& s) +{ std::string result = s; pre_advance_password(result); do { @@ -67,7 +73,8 @@ std::string next_password(std::string const &s) { return result; } -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ for (std::string line; std::getline(std::cin, line);) { std::string next = next_password(line); std::string next2 = next_password(next); diff --git a/2015/puzzle-12-01.cc b/2015/puzzle-12-01.cc index e22ec4a..81131d4 100644 --- a/2015/puzzle-12-01.cc +++ b/2015/puzzle-12-01.cc @@ -7,7 +7,8 @@ #include #include -int parse_numbers(std::string const &s) { +int parse_numbers(std::string const& s) +{ static const std::regex re("-?\\d+"); std::string left = s; std::smatch m; @@ -20,7 +21,8 @@ int parse_numbers(std::string const &s) { return acc; } -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ int acc = 0; for (std::string line; std::getline(std::cin, line);) { acc += parse_numbers(line); diff --git a/2015/puzzle-12-02.cc b/2015/puzzle-12-02.cc index 3691ff1..660c18d 100644 --- a/2015/puzzle-12-02.cc +++ b/2015/puzzle-12-02.cc @@ -8,7 +8,8 @@ #include #include -int do_parse(std::string const &s, std::string::size_type &pos) { +int do_parse(std::string const& s, std::string::size_type& pos) +{ int result = 0; bool ignore = false; @@ -16,16 +17,20 @@ int do_parse(std::string const &s, std::string::size_type &pos) { if (s[pos] == '{') { ++pos; result += do_parse(s, pos); - } else if (s[pos] == '[') { + } + else if (s[pos] == '[') { ++pos; result += do_parse(s, pos); - } else if (s[pos] == '}') { + } + else if (s[pos] == '}') { ++pos; return ignore ? 0 : result; - } else if (s[pos] == ']') { + } + else if (s[pos] == ']') { ++pos; return result; - } else if (s[pos] == '"') { + } + else if (s[pos] == '"') { ++pos; auto e = s.find('"', pos); assert(e != std::string::npos); @@ -34,11 +39,13 @@ int do_parse(std::string const &s, std::string::size_type &pos) { ignore = true; } pos = e + 1; - } else if (std::isdigit(s[pos]) || s[pos] == '-') { + } + else if (std::isdigit(s[pos]) || s[pos] == '-') { std::size_t len = 0; result += std::stoi(s.substr(pos), &len); pos += len; - } else { + } + else { assert(s[pos] == ',' || s[pos] == ':'); ++pos; } @@ -47,14 +54,16 @@ int do_parse(std::string const &s, std::string::size_type &pos) { return result; } -int parse_numbers(std::string const &s) { +int parse_numbers(std::string const& s) +{ std::string::size_type pos = 0; int result = do_parse(s, pos); assert(pos == s.size()); return result; } -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ int acc = 0; for (std::string line; std::getline(std::cin, line);) { acc += parse_numbers(line); diff --git a/2015/puzzle-13-01.cc b/2015/puzzle-13-01.cc index a7a3e35..ba7e5c4 100644 --- a/2015/puzzle-13-01.cc +++ b/2015/puzzle-13-01.cc @@ -9,7 +9,8 @@ #include #include -struct Graph { +struct Graph +{ using Node = std::string; using Weight = int; using Nodes = std::set; @@ -17,10 +18,10 @@ struct Graph { using Edge = std::pair; using EdgeWeights = std::map; - void add_edge(std::string const &s) { - static const std::regex re( - "(\\w+) would (gain|lose) (\\d+) happiness units? " - "by sitting next to (\\w+)."); + void add_edge(std::string const& s) + { + static const std::regex re("(\\w+) would (gain|lose) (\\d+) happiness units? " + "by sitting next to (\\w+)."); std::smatch m; if (std::regex_search(s, m, re)) { nodes_.insert(m.str(1)); @@ -30,18 +31,20 @@ struct Graph { delta = -delta; } weights_.insert({{m.str(1), m.str(4)}, delta}); - } else { + } + else { assert(false); } } - Weight max_happiness() const { + Weight max_happiness() const + { int max_happiness = INT_MIN; NodeList nl(nodes_.begin(), nodes_.end()); std::sort(nl.begin(), nl.end()); do { std::cout << "\r"; - for (auto const &s : nl) { + for (auto const& s : nl) { std::cout << s << " "; } int h = happiness(nl); @@ -56,7 +59,8 @@ struct Graph { return max_happiness; } - int happiness(NodeList const &nl) const { + int happiness(NodeList const& nl) const + { int h = 0; h += weights_.find(std::make_pair(nl[nl.size() - 1], nl[0]))->second; h += weights_.find(std::make_pair(nl[0], nl[nl.size() - 1]))->second; @@ -71,7 +75,8 @@ struct Graph { EdgeWeights weights_; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ Graph g; for (std::string line; std::getline(std::cin, line);) { g.add_edge(line); diff --git a/2015/puzzle-13-02.cc b/2015/puzzle-13-02.cc index 16fc1e9..38017c8 100644 --- a/2015/puzzle-13-02.cc +++ b/2015/puzzle-13-02.cc @@ -9,7 +9,8 @@ #include #include -struct Graph { +struct Graph +{ Graph() { nodes_.insert("self"); } using Node = std::string; using Weight = int; @@ -18,10 +19,10 @@ struct Graph { using Edge = std::pair; using EdgeWeights = std::map; - void add_edge(std::string const &s) { - static const std::regex re( - "(\\w+) would (gain|lose) (\\d+) happiness units? " - "by sitting next to (\\w+)."); + void add_edge(std::string const& s) + { + static const std::regex re("(\\w+) would (gain|lose) (\\d+) happiness units? " + "by sitting next to (\\w+)."); std::smatch m; if (std::regex_search(s, m, re)) { nodes_.insert(m.str(1)); @@ -33,18 +34,20 @@ struct Graph { delta = -delta; } weights_.insert({{m.str(1), m.str(4)}, delta}); - } else { + } + else { assert(false); } } - Weight max_happiness() const { + Weight max_happiness() const + { int max_happiness = INT_MIN; NodeList nl(nodes_.begin(), nodes_.end()); std::sort(nl.begin(), nl.end()); do { std::cout << "\r"; - for (auto const &s : nl) { + for (auto const& s : nl) { std::cout << s << " "; } int h = happiness(nl); @@ -59,7 +62,8 @@ struct Graph { return max_happiness; } - int happiness(NodeList const &nl) const { + int happiness(NodeList const& nl) const + { int h = 0; h += weights_.find(std::make_pair(nl[nl.size() - 1], nl[0]))->second; h += weights_.find(std::make_pair(nl[0], nl[nl.size() - 1]))->second; @@ -74,7 +78,8 @@ struct Graph { EdgeWeights weights_; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ Graph g; for (std::string line; std::getline(std::cin, line);) { g.add_edge(line); diff --git a/2015/puzzle-14-01.cc b/2015/puzzle-14-01.cc index d2f10a3..1b738bf 100644 --- a/2015/puzzle-14-01.cc +++ b/2015/puzzle-14-01.cc @@ -11,7 +11,8 @@ using Distance = unsigned long; -Distance distance(std::string const &s, unsigned t) { +Distance distance(std::string const& s, unsigned t) +{ static const std::regex re("(\\w+) can fly (\\d+) km/s for (\\d+) seconds?, " "but then must rest for (\\d+) seconds?."); std::smatch m; @@ -32,15 +33,17 @@ Distance distance(std::string const &s, unsigned t) { t = std::min(t, fly_time); result += t * fly_speed; - std::cout << m.str(1) << "(" << fly_speed << ", " << fly_time << ", " - << rest_time << ") = " << result << "\n"; + std::cout << m.str(1) << "(" << fly_speed << ", " << fly_time << ", " << rest_time + << ") = " << result << "\n"; return result; - } else { + } + else { assert(false); } } -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ Distance max_d = 0; for (std::string line; std::getline(std::cin, line);) { max_d = std::max(max_d, distance(line, 2503)); diff --git a/2015/puzzle-14-02.cc b/2015/puzzle-14-02.cc index f1c31d8..f2f47f2 100644 --- a/2015/puzzle-14-02.cc +++ b/2015/puzzle-14-02.cc @@ -16,29 +16,30 @@ using Speed = unsigned long; struct Reindeer; using ReindeerSet = std::set; -struct Reindeer { - Reindeer(std::string const &s) { - static const std::regex re( - "(\\w+) can fly (\\d+) km/s for (\\d+) seconds?, " - "but then must rest for (\\d+) seconds?."); +struct Reindeer +{ + Reindeer(std::string const& s) + { + static const std::regex re("(\\w+) can fly (\\d+) km/s for (\\d+) seconds?, " + "but then must rest for (\\d+) seconds?."); std::smatch m; if (std::regex_search(s, m, re)) { name_ = m.str(1); speed_ = std::stoul(m.str(2)); fly_time_ = std::stoul(m.str(3)); rest_time_ = std::stoul(m.str(4)); - } else { + } + else { assert(false); } } - bool operator<(Reindeer const &rhs) const noexcept { - return name_ < rhs.name_; - } + bool operator<(Reindeer const& rhs) const noexcept { return name_ < rhs.name_; } - std::string const &name() const { return name_; } + std::string const& name() const { return name_; } - Distance distance(Time t) const { // Period and number of them + Distance distance(Time t) const + { // Period and number of them Time period = fly_time_ + rest_time_; unsigned periods = t / period; @@ -59,7 +60,8 @@ struct Reindeer { Time rest_time_; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ ReindeerSet reindeer; for (std::string line; std::getline(std::cin, line);) { @@ -68,21 +70,20 @@ int main(int argc, char **argv) { std::map score; for (unsigned t = 1; t < 2504; ++t) { - auto it = - std::max_element(reindeer.begin(), reindeer.end(), - [t](Reindeer const &lhs, Reindeer const &rhs) -> bool { - return lhs.distance(t) < rhs.distance(t); - }); + auto it = std::max_element(reindeer.begin(), reindeer.end(), + [t](Reindeer const& lhs, Reindeer const& rhs) -> bool { + return lhs.distance(t) < rhs.distance(t); + }); auto [iit, success] = score.insert({it->name(), 1}); if (!success) { iit->second++; } } - auto it = std::max_element(score.begin(), score.end(), - [](auto const &lhs, auto const &rhs) -> bool { - return lhs.second < rhs.second; - }); + auto it = + std::max_element(score.begin(), score.end(), [](auto const& lhs, auto const& rhs) -> bool { + return lhs.second < rhs.second; + }); std::cout << it->first << " wins with a score of " << it->second << "\n"; return 0; diff --git a/2015/puzzle-15-01.cc b/2015/puzzle-15-01.cc index 73cd569..2533ea8 100644 --- a/2015/puzzle-15-01.cc +++ b/2015/puzzle-15-01.cc @@ -12,8 +12,10 @@ using Score = long; using PropertyMap = std::map; -struct Ingredient { - explicit Ingredient(std::string const &s) { +struct Ingredient +{ + explicit Ingredient(std::string const& s) + { auto colon = s.find(':'); name_ = s.substr(0, colon); auto suffix = s.substr(colon + 1); @@ -22,25 +24,24 @@ struct Ingredient { static const std::regex re(",? (\\w+) (-?\\d+)"); std::smatch m; if (std::regex_search(suffix, m, re)) { - auto [it, success] = - properties_.insert({m.str(1), std::stol(m.str(2))}); + auto [it, success] = properties_.insert({m.str(1), std::stol(m.str(2))}); assert(success); std::cout << " " << it->first << ": " << it->second << "\n"; suffix = m.suffix(); - } else { + } + else { assert(false); } } } - bool operator<(Ingredient const &rhs) const noexcept { - return name_ < rhs.name_; - } + bool operator<(Ingredient const& rhs) const noexcept { return name_ < rhs.name_; } - std::string const &name() const noexcept { return name_; } + std::string const& name() const noexcept { return name_; } - void update_score(Score amount, PropertyMap &totals) const { - for (auto const &kv : properties_) { + void update_score(Score amount, PropertyMap& totals) const + { + for (auto const& kv : properties_) { if (kv.first == "calories") { continue; } @@ -56,20 +57,20 @@ private: PropertyMap properties_; }; -struct Ingredients { - void add_ingredient(std::string const &s) { - ingredients_.push_back(Ingredient(s)); - } +struct Ingredients +{ + void add_ingredient(std::string const& s) { ingredients_.push_back(Ingredient(s)); } - Score best_combination(Score amount) const { + Score best_combination(Score amount) const + { PropertyMap totals; return best_combination(amount, ingredients_.begin(), 0UL, totals); } private: - Score best_combination(Score amount, - std::vector::const_iterator it, - Score best_score, PropertyMap &totals) const { + Score best_combination(Score amount, std::vector::const_iterator it, Score best_score, + PropertyMap& totals) const + { it->update_score(amount, totals); auto it2 = it; ++it2; @@ -83,17 +84,17 @@ private: for (auto allocation = amount - 1; allocation > 0; --allocation) { it->update_score(-1, totals); best_score = - std::max(best_score, best_combination(amount - allocation, it2, - best_score, totals)); + std::max(best_score, best_combination(amount - allocation, it2, best_score, totals)); } it->update_score(-1, totals); return best_score; } - Score calculate_score(PropertyMap const &totals) const { + Score calculate_score(PropertyMap const& totals) const + { Score r = 1; - for (auto const &kv : totals) { + for (auto const& kv : totals) { if (kv.first == "calories") { continue; } @@ -109,7 +110,8 @@ private: std::vector ingredients_; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ Ingredients ingredients; std::string line; diff --git a/2015/puzzle-15-02.cc b/2015/puzzle-15-02.cc index 2ba1a5f..c52d90b 100644 --- a/2015/puzzle-15-02.cc +++ b/2015/puzzle-15-02.cc @@ -12,8 +12,10 @@ using Score = long; using PropertyMap = std::map; -struct Ingredient { - explicit Ingredient(std::string const &s) { +struct Ingredient +{ + explicit Ingredient(std::string const& s) + { auto colon = s.find(':'); name_ = s.substr(0, colon); auto suffix = s.substr(colon + 1); @@ -22,25 +24,24 @@ struct Ingredient { static const std::regex re(",? (\\w+) (-?\\d+)"); std::smatch m; if (std::regex_search(suffix, m, re)) { - auto [it, success] = - properties_.insert({m.str(1), std::stol(m.str(2))}); + auto [it, success] = properties_.insert({m.str(1), std::stol(m.str(2))}); assert(success); std::cout << " " << it->first << ": " << it->second << "\n"; suffix = m.suffix(); - } else { + } + else { assert(false); } } } - bool operator<(Ingredient const &rhs) const noexcept { - return name_ < rhs.name_; - } + bool operator<(Ingredient const& rhs) const noexcept { return name_ < rhs.name_; } - std::string const &name() const noexcept { return name_; } + std::string const& name() const noexcept { return name_; } - void update_score(Score amount, PropertyMap &totals) const { - for (auto const &kv : properties_) { + void update_score(Score amount, PropertyMap& totals) const + { + for (auto const& kv : properties_) { auto [it, success] = totals.insert({kv.first, amount * kv.second}); if (!success) { it->second += amount * kv.second; @@ -53,20 +54,20 @@ private: PropertyMap properties_; }; -struct Ingredients { - void add_ingredient(std::string const &s) { - ingredients_.push_back(Ingredient(s)); - } +struct Ingredients +{ + void add_ingredient(std::string const& s) { ingredients_.push_back(Ingredient(s)); } - Score best_combination(Score amount) const { + Score best_combination(Score amount) const + { PropertyMap totals; return best_combination(amount, ingredients_.begin(), 0UL, totals); } private: - Score best_combination(Score amount, - std::vector::const_iterator it, - Score best_score, PropertyMap &totals) const { + Score best_combination(Score amount, std::vector::const_iterator it, Score best_score, + PropertyMap& totals) const + { it->update_score(amount, totals); auto it2 = it; ++it2; @@ -80,18 +81,18 @@ private: for (auto allocation = amount - 1; allocation > 0; --allocation) { it->update_score(-1, totals); best_score = - std::max(best_score, best_combination(amount - allocation, it2, - best_score, totals)); + std::max(best_score, best_combination(amount - allocation, it2, best_score, totals)); } it->update_score(-1, totals); return best_score; } - Score calculate_score(PropertyMap const &totals) const { + Score calculate_score(PropertyMap const& totals) const + { Score r = 1; Score calories = 0; - for (auto const &kv : totals) { + for (auto const& kv : totals) { if (kv.first == "calories") { calories += kv.second; continue; @@ -110,7 +111,8 @@ private: std::vector ingredients_; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ Ingredients ingredients; std::string line; diff --git a/2015/puzzle-16-01.cc b/2015/puzzle-16-01.cc index 6025793..891e776 100644 --- a/2015/puzzle-16-01.cc +++ b/2015/puzzle-16-01.cc @@ -22,7 +22,8 @@ static const InfoMap the_real_aunt_sue({{"children", 3}, {"cars", 2}, {"perfumes", 1}}); -bool matches_sue(std::string const &s) { +bool matches_sue(std::string const& s) +{ assert(s.substr(0, 4) == "Sue "); std::size_t pos = 4; std::size_t len = 0; @@ -34,7 +35,8 @@ bool matches_sue(std::string const &s) { while (pos < s.size()) { if (s[pos] == ' ' || s[pos] == ',') { ++pos; - } else { + } + else { auto colon = s.find(':', pos); assert(colon != std::string::npos); std::string name = s.substr(pos, colon - pos); @@ -57,7 +59,8 @@ bool matches_sue(std::string const &s) { return true; } -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ std::string line; while (std::getline(std::cin, line)) { if (matches_sue(line)) { diff --git a/2015/puzzle-16-02.cc b/2015/puzzle-16-02.cc index e01aa19..629203b 100644 --- a/2015/puzzle-16-02.cc +++ b/2015/puzzle-16-02.cc @@ -28,7 +28,8 @@ static const InfoMap the_real_aunt_sue({{"children", {3, Equality}}, {"cars", {2, Equality}}, {"perfumes", {1, Equality}}}); -bool matches_sue(std::string const &s) { +bool matches_sue(std::string const& s) +{ assert(s.substr(0, 4) == "Sue "); std::size_t pos = 4; std::size_t len = 0; @@ -40,7 +41,8 @@ bool matches_sue(std::string const &s) { while (pos < s.size()) { if (s[pos] == ' ' || s[pos] == ',') { ++pos; - } else { + } + else { auto colon = s.find(':', pos); assert(colon != std::string::npos); std::string name = s.substr(pos, colon - pos); @@ -63,7 +65,8 @@ bool matches_sue(std::string const &s) { return true; } -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ std::string line; while (std::getline(std::cin, line)) { if (matches_sue(line)) { diff --git a/2015/puzzle-17-01.cc b/2015/puzzle-17-01.cc index d448616..b2ad037 100644 --- a/2015/puzzle-17-01.cc +++ b/2015/puzzle-17-01.cc @@ -14,9 +14,9 @@ constexpr Quantity total = 150; using Quantities = std::vector; -unsigned count_combinations(Quantities::const_iterator it, - Quantities::const_iterator end, Quantity amount, - unsigned depth = 0) { +unsigned count_combinations(Quantities::const_iterator it, Quantities::const_iterator end, + Quantity amount, unsigned depth = 0) +{ // We have no room for this container std::cout << std::string(depth, ' ') << *it << ": " << amount << "\n"; if (amount < *it) { @@ -36,7 +36,8 @@ unsigned count_combinations(Quantities::const_iterator it, return result; } -unsigned count_combinations(Quantities const &containers) { +unsigned count_combinations(Quantities const& containers) +{ unsigned result = 0; for (auto it = containers.begin(); it != containers.end(); ++it) { result += count_combinations(it, containers.end(), total); @@ -44,7 +45,8 @@ unsigned count_combinations(Quantities const &containers) { return result; } -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ std::string line; Quantities containers; while (std::getline(std::cin, line)) { diff --git a/2015/puzzle-17-02.cc b/2015/puzzle-17-02.cc index 08b295a..9ed8380 100644 --- a/2015/puzzle-17-02.cc +++ b/2015/puzzle-17-02.cc @@ -14,11 +14,11 @@ constexpr Quantity total = 150; using Quantities = std::vector; -template -unsigned count_combinations(Quantities::const_iterator it, - Quantities::const_iterator end, Quantity amount, - unsigned depth, Fn1 base_result, - unsigned init_addend, Fn2 adder) { +template +unsigned count_combinations(Quantities::const_iterator it, Quantities::const_iterator end, + Quantity amount, unsigned depth, Fn1 base_result, unsigned init_addend, + Fn2 adder) +{ // We have no room for this container std::cout << std::string(depth, ' ') << *it << ": " << amount; if (amount < *it) { @@ -36,45 +36,42 @@ unsigned count_combinations(Quantities::const_iterator it, amount -= *it; auto result = init_addend; while (++it != end) { - auto child_score = count_combinations(it, end, amount, depth + 1, - base_result, init_addend, adder); + auto child_score = + count_combinations(it, end, amount, depth + 1, base_result, init_addend, adder); result = adder(result, child_score); } - std::cout << std::string(depth, ' ') << "Recursion result: " << result - << "\n"; + std::cout << std::string(depth, ' ') << "Recursion result: " << result << "\n"; return result; } -template -unsigned count_combinations(Quantities const &containers, Quantity amount, - Fn1 base_result, unsigned init_addend, Fn2 adder) { +template +unsigned count_combinations(Quantities const& containers, Quantity amount, Fn1 base_result, + unsigned init_addend, Fn2 adder) +{ unsigned result = init_addend; for (auto it = containers.begin(); it != containers.end(); ++it) { - result = adder(result, count_combinations(it, containers.end(), total, 0, - base_result, init_addend, adder)); + result = adder( + result, count_combinations(it, containers.end(), total, 0, base_result, init_addend, adder)); } return result; } -unsigned find_shortest_combination(Quantities const &containers) { +unsigned find_shortest_combination(Quantities const& containers) +{ return count_combinations( - containers, total, [](unsigned depth) { return depth; }, UINT_MAX, - [](unsigned current, unsigned child_score) { - return std::min(current, child_score); - }); + containers, total, [](unsigned depth) { return depth; }, UINT_MAX, + [](unsigned current, unsigned child_score) { return std::min(current, child_score); }); } -unsigned count_min_length_combinations(Quantities const &containers, - unsigned expected_depth) { +unsigned count_min_length_combinations(Quantities const& containers, unsigned expected_depth) +{ return count_combinations( - containers, total, - [expected_depth](unsigned depth) { return depth == expected_depth; }, 0, - [](unsigned current, unsigned child_score) { - return current + child_score; - }); + containers, total, [expected_depth](unsigned depth) { return depth == expected_depth; }, 0, + [](unsigned current, unsigned child_score) { return current + child_score; }); } -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ std::string line; Quantities containers; while (std::getline(std::cin, line)) { diff --git a/2015/puzzle-18-01.cc b/2015/puzzle-18-01.cc index 9d3f2e3..6ba9736 100644 --- a/2015/puzzle-18-01.cc +++ b/2015/puzzle-18-01.cc @@ -10,9 +10,10 @@ #include #include -struct ConwayState { - ConwayState(std::size_t width, std::string const &s) - : width_(width), state_(width_ * width_, 0) { +struct ConwayState +{ + ConwayState(std::size_t width, std::string const& s) : width_(width), state_(width_ * width_, 0) + { assert(s.size() == width_ * width_); for (std::size_t i = 0; i < state_.size(); ++i) { @@ -22,7 +23,8 @@ struct ConwayState { } } - ConwayState next_state() const { + ConwayState next_state() const + { ConwayState next(*this); for (std::size_t i = 0; i < state_.size(); ++i) { if (state_[i] == (2 | active_) || state_[i] == (3 | active_)) { @@ -36,23 +38,22 @@ struct ConwayState { return next; } - std::size_t num_active() const { - return std::accumulate(state_.begin(), state_.end(), std::size_t(0), - [](std::size_t current, unsigned char info) { - return current + ((info & active_) != 0); - }); + std::size_t num_active() const + { + return std::accumulate( + state_.begin(), state_.end(), std::size_t(0), + [](std::size_t current, unsigned char info) { return current + ((info & active_) != 0); }); } private: - void flip(std::size_t idx) { + void flip(std::size_t idx) + { state_[idx] = state_[idx] ^ active_; int delta = ((state_[idx] & active_) == active_) ? 1 : -1; std::size_t row = idx / width_; std::size_t col = idx % width_; - for (std::size_t r = std::max(std::size_t(1), row) - 1; - r < std::min(width_, row + 2); ++r) { - for (std::size_t c = std::max(std::size_t(1), col) - 1; - c < std::min(width_, col + 2); ++c) { + for (std::size_t r = std::max(std::size_t(1), row) - 1; r < std::min(width_, row + 2); ++r) { + for (std::size_t c = std::max(std::size_t(1), col) - 1; c < std::min(width_, col + 2); ++c) { if (r == row && c == col) { continue; } @@ -65,10 +66,11 @@ private: std::vector state_; static constexpr unsigned char active_ = 0x80; - friend std::ostream &operator<<(std::ostream &os, ConwayState const &state); + friend std::ostream& operator<<(std::ostream& os, ConwayState const& state); }; -std::ostream &operator<<(std::ostream &os, ConwayState const &state) { +std::ostream& operator<<(std::ostream& os, ConwayState const& state) +{ std::size_t c = 0; for (auto s : state.state_) { os << (s & ConwayState::active_ ? '#' : '.'); @@ -82,7 +84,8 @@ std::ostream &operator<<(std::ostream &os, ConwayState const &state) { return os; } -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ std::string line; std::size_t width = 0; std::string init; diff --git a/2015/puzzle-18-02.cc b/2015/puzzle-18-02.cc index 4ef8def..cef3d7b 100644 --- a/2015/puzzle-18-02.cc +++ b/2015/puzzle-18-02.cc @@ -10,9 +10,10 @@ #include #include -struct ConwayState { - ConwayState(std::size_t width, std::string const &s) - : width_(width), state_(width_ * width_, 0) { +struct ConwayState +{ + ConwayState(std::size_t width, std::string const& s) : width_(width), state_(width_ * width_, 0) + { assert(s.size() == width_ * width_); flip(0); @@ -27,11 +28,11 @@ struct ConwayState { } } - ConwayState next_state() const { + ConwayState next_state() const + { ConwayState next(*this); for (std::size_t i = 0; i < state_.size(); ++i) { - if (i == 0 || i == width_ - 1 || i == width_ * (width_ - 1) || - i == width_ * width_ - 1) { + if (i == 0 || i == width_ - 1 || i == width_ * (width_ - 1) || i == width_ * width_ - 1) { continue; } if (state_[i] == (2 | active_) || state_[i] == (3 | active_)) { @@ -45,23 +46,22 @@ struct ConwayState { return next; } - std::size_t num_active() const { - return std::accumulate(state_.begin(), state_.end(), std::size_t(0), - [](std::size_t current, unsigned char info) { - return current + ((info & active_) != 0); - }); + std::size_t num_active() const + { + return std::accumulate( + state_.begin(), state_.end(), std::size_t(0), + [](std::size_t current, unsigned char info) { return current + ((info & active_) != 0); }); } private: - void flip(std::size_t idx) { + void flip(std::size_t idx) + { state_[idx] = state_[idx] ^ active_; int delta = ((state_[idx] & active_) == active_) ? 1 : -1; std::size_t row = idx / width_; std::size_t col = idx % width_; - for (std::size_t r = std::max(std::size_t(1), row) - 1; - r < std::min(width_, row + 2); ++r) { - for (std::size_t c = std::max(std::size_t(1), col) - 1; - c < std::min(width_, col + 2); ++c) { + for (std::size_t r = std::max(std::size_t(1), row) - 1; r < std::min(width_, row + 2); ++r) { + for (std::size_t c = std::max(std::size_t(1), col) - 1; c < std::min(width_, col + 2); ++c) { if (r == row && c == col) { continue; } @@ -74,10 +74,11 @@ private: std::vector state_; static constexpr unsigned char active_ = 0x80; - friend std::ostream &operator<<(std::ostream &os, ConwayState const &state); + friend std::ostream& operator<<(std::ostream& os, ConwayState const& state); }; -std::ostream &operator<<(std::ostream &os, ConwayState const &state) { +std::ostream& operator<<(std::ostream& os, ConwayState const& state) +{ std::size_t c = 0; for (auto s : state.state_) { os << (s & ConwayState::active_ ? '#' : '.'); @@ -91,7 +92,8 @@ std::ostream &operator<<(std::ostream &os, ConwayState const &state) { return os; } -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ std::string line; std::size_t width = 0; std::string init; diff --git a/2015/puzzle-19-01.cc b/2015/puzzle-19-01.cc index d5e783b..59c010e 100644 --- a/2015/puzzle-19-01.cc +++ b/2015/puzzle-19-01.cc @@ -10,30 +10,31 @@ #include #include -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ std::multimap replacements; bool collecting_replacements = true; std::string line; while (std::getline(std::cin, line)) { if (line.empty()) { collecting_replacements = false; - } else if (collecting_replacements) { + } + else if (collecting_replacements) { auto sep = line.find(" => "); replacements.insert({line.substr(0, sep), line.substr(sep + 4)}); - } else { + } + else { std::set new_molecules; for (unsigned pos = 0; pos < line.size(); ++pos) { auto [it, ite] = replacements.equal_range(line.substr(pos, 1)); while (it != ite) { - new_molecules.insert(line.substr(0, pos) + it->second + - line.substr(pos + 1)); + new_molecules.insert(line.substr(0, pos) + it->second + line.substr(pos + 1)); ++it; } if (pos < line.size() - 1) { auto [it, ite] = replacements.equal_range(line.substr(pos, 2)); while (it != ite) { - new_molecules.insert(line.substr(0, pos) + it->second + - line.substr(pos + 2)); + new_molecules.insert(line.substr(0, pos) + it->second + line.substr(pos + 2)); ++it; } } diff --git a/2015/puzzle-19-02.cc b/2015/puzzle-19-02.cc index d38c739..95d99fd 100644 --- a/2015/puzzle-19-02.cc +++ b/2015/puzzle-19-02.cc @@ -13,10 +13,11 @@ #include #include -template -std::size_t dedup(Map const &replacements, std::string &molecule) { +template +std::size_t dedup(Map const& replacements, std::string& molecule) +{ std::size_t steps = 0; - for (auto const &kv : replacements) { + for (auto const& kv : replacements) { if (kv.first != kv.second + kv.second) { continue; } @@ -24,8 +25,7 @@ std::size_t dedup(Map const &replacements, std::string &molecule) { do { pos = molecule.find(kv.first); if (pos != std::string::npos) { - molecule = molecule.substr(0, pos) + kv.second + - molecule.substr(pos + kv.first.size()); + molecule = molecule.substr(0, pos) + kv.second + molecule.substr(pos + kv.first.size()); ++steps; } } while (pos != std::string::npos); @@ -143,21 +143,22 @@ find_molecule(std::unordered_map const &replacements, } #endif -template -bool is_good_replacement(Map const &replacements, std::string const &nm, - std::size_t pos, std::size_t replace_len) { - for (auto const &kv : replacements) { +template +bool is_good_replacement(Map const& replacements, std::string const& nm, std::size_t pos, + std::size_t replace_len) +{ + for (auto const& kv : replacements) { auto left = std::max(kv.first.size(), pos) - kv.first.size(); - if (nm.substr(left, kv.first.size() * 2 + replace_len).find(kv.first) != - std::string::npos) { + if (nm.substr(left, kv.first.size() * 2 + replace_len).find(kv.first) != std::string::npos) { return true; } } return false; } -template -std::size_t find_molecule(Map &replacements, std::string const &molecule) { +template +std::size_t find_molecule(Map& replacements, std::string const& molecule) +{ std::random_device rd; std::mt19937 g(rd()); while (true) { @@ -167,7 +168,7 @@ std::size_t find_molecule(Map &replacements, std::string const &molecule) { std::size_t steps = 0; do { changed = false; - for (auto const &kv : replacements) { + for (auto const& kv : replacements) { auto pos = m.find(kv.first); if (pos != std::string::npos) { m = m.substr(0, pos) + kv.second + m.substr(pos + kv.first.length()); @@ -185,17 +186,20 @@ std::size_t find_molecule(Map &replacements, std::string const &molecule) { } } -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ std::vector> replacements; bool collecting_replacements = true; std::string line; while (std::getline(std::cin, line)) { if (line.empty()) { collecting_replacements = false; - } else if (collecting_replacements) { + } + else if (collecting_replacements) { auto sep = line.find(" => "); replacements.push_back({line.substr(sep + 4), line.substr(0, sep)}); - } else { + } + else { std::cout << line << "\n"; auto time = find_molecule(replacements, line); std::cout << "Solution: " << time << "\n"; diff --git a/2015/puzzle-20-01.cc b/2015/puzzle-20-01.cc index f25e167..55f13bb 100644 --- a/2015/puzzle-20-01.cc +++ b/2015/puzzle-20-01.cc @@ -1,37 +1,39 @@ -#include -#include #include +#include +#include -int main() { - std::string line; - std::getline(std::cin, line); +int main() +{ + std::string line; + std::getline(std::cin, line); - auto target{std::stoul(line)}; + auto target{std::stoul(line)}; - /* This is a really stupid way to do this in terms of effort, but it produces the right answer and doesn't involve - * thinking too much: - * - * n is the current house we're at. + /* This is a really stupid way to do this in terms of effort, but it produces the right answer and + * doesn't involve thinking too much: + * + * n is the current house we're at. + */ + for (auto n{2UL}; true; ++n) { + auto amt{0UL}; + /* Find the number of presents delivered to house `n`. We do this by walking through all + * numbers <= sqrt(n) and seeing if they are a factor. If so we add presents for that number + * (i) and also (n/i), being careful not to double count for square roots. This reduces the + * amount of work we have to do significantly. */ - for (auto n{2UL}; true; ++n) { - auto amt{0UL}; - /* Find the number of presents delivered to house `n`. We do this by walking through all numbers <= sqrt(n) and - * seeing if they are a factor. If so we add presents for that number (i) and also (n/i), being careful not to - * double count for square roots. This reduces the amount of work we have to do significantly. - */ - for (auto i{1UL}; i <= (n / i); ++i) { - if (n % i == 0) { - amt += i * 10; - if (i != n / i) { - amt += (n / i) * 10; - } - } - } - if (amt >= target) { - std::cout << "Target: " << target << " met at: " << n << " with amount: " << amt << '\n'; - return EXIT_SUCCESS; + for (auto i{1UL}; i <= (n / i); ++i) { + if (n % i == 0) { + amt += i * 10; + if (i != n / i) { + amt += (n / i) * 10; } + } } + if (amt >= target) { + std::cout << "Target: " << target << " met at: " << n << " with amount: " << amt << '\n'; + return EXIT_SUCCESS; + } + } - return EXIT_FAILURE; + return EXIT_FAILURE; } \ No newline at end of file diff --git a/2015/puzzle-20-02.cc b/2015/puzzle-20-02.cc index e7a37ac..4851f5c 100644 --- a/2015/puzzle-20-02.cc +++ b/2015/puzzle-20-02.cc @@ -1,42 +1,45 @@ -#include -#include #include +#include +#include -int main() { - std::string line; - std::getline(std::cin, line); +int main() +{ + std::string line; + std::getline(std::cin, line); - auto target{std::stoul(line)}; + auto target{std::stoul(line)}; - /* This is a really stupid way to do this in terms of effort, but it produces the right answer and doesn't involve - * thinking too much: + /* This is a really stupid way to do this in terms of effort, but it produces the right answer and + * doesn't involve thinking too much: + * + * n is the current house we're at. + */ + for (auto n{2UL}; true; ++n) { + auto amt{0UL}; + /* Find the number of presents delivered to house `n`. We do this by walking through all + * numbers <= sqrt(n) and seeing if they are a factor. If so we add presents for that number + * (i) and also (n/i), being careful not to double count for square roots. This reduces the + * amount of work we have to do significantly. * - * n is the current house we're at. + * For the second part we also check to ensure we've not at the 51st delivery or greater for + * this elf. */ - for (auto n{2UL}; true; ++n) { - auto amt{0UL}; - /* Find the number of presents delivered to house `n`. We do this by walking through all numbers <= sqrt(n) and - * seeing if they are a factor. If so we add presents for that number (i) and also (n/i), being careful not to - * double count for square roots. This reduces the amount of work we have to do significantly. - * - * For the second part we also check to ensure we've not at the 51st delivery or greater for this elf. - */ - for (auto i{1UL}; i <= (n / i); ++i) { - if (n % i == 0) { - auto i2{n / i}; - if (i2 <= 50) { - amt += i * 11; - } - if (i <= 50 && i != i2) { - amt += i2 * 11; - } - } + for (auto i{1UL}; i <= (n / i); ++i) { + if (n % i == 0) { + auto i2{n / i}; + if (i2 <= 50) { + amt += i * 11; } - if (amt >= target) { - std::cout << "Target: " << target << " met at: " << n << " with amount: " << amt << '\n'; - return EXIT_SUCCESS; + if (i <= 50 && i != i2) { + amt += i2 * 11; } + } } + if (amt >= target) { + std::cout << "Target: " << target << " met at: " << n << " with amount: " << amt << '\n'; + return EXIT_SUCCESS; + } + } - return EXIT_FAILURE; + return EXIT_FAILURE; } \ No newline at end of file diff --git a/2015/puzzle-21-01.cc b/2015/puzzle-21-01.cc index 92f495c..4ddbfc1 100644 --- a/2015/puzzle-21-01.cc +++ b/2015/puzzle-21-01.cc @@ -1,127 +1,136 @@ +#include +#include +#include #include #include -#include -#include -#include using namespace std::string_literals; -struct Item { - std::string name_; - unsigned long cost_; - unsigned long damage_; - unsigned long armour_; +struct Item +{ + std::string name_; + unsigned long cost_; + unsigned long damage_; + unsigned long armour_; - bool operator==(Item const& rhs) const { return name_ == rhs.name_; } + bool operator==(Item const& rhs) const { return name_ == rhs.name_; } }; -struct Person { - Person(unsigned long hp, unsigned long damage, unsigned long armour) : name_("Enemy"), hp_(hp), - cost_(std::numeric_limits::max()), - damage_(damage), - armour_(armour) {} +struct Person +{ + Person(unsigned long hp, unsigned long damage, unsigned long armour) + : name_("Enemy"), hp_(hp), cost_(std::numeric_limits::max()), damage_(damage), + armour_(armour) + { + } - Person(Item const &weapon, Item const &armour, Item const &left_ring, Item const &right_ring) : - name_("Player: "s + weapon.name_ + ", "s + armour.name_ + ", "s + left_ring.name_ + ", " + - right_ring.name_), - hp_(100), - cost_(weapon.cost_ + armour.cost_ + left_ring.cost_ + right_ring.cost_), - damage_(weapon.damage_ + armour.damage_ + left_ring.damage_ + right_ring.damage_), - armour_(weapon.armour_ + armour.armour_ + left_ring.armour_ + right_ring.armour_) {} + Person(Item const& weapon, Item const& armour, Item const& left_ring, Item const& right_ring) + : name_("Player: "s + weapon.name_ + ", "s + armour.name_ + ", "s + left_ring.name_ + ", " + + right_ring.name_), + hp_(100), cost_(weapon.cost_ + armour.cost_ + left_ring.cost_ + right_ring.cost_), + damage_(weapon.damage_ + armour.damage_ + left_ring.damage_ + right_ring.damage_), + armour_(weapon.armour_ + armour.armour_ + left_ring.armour_ + right_ring.armour_) + { + } - unsigned get_attacked(Person const &attacker) { - auto damage{(attacker.damage_ > armour_) ? attacker.damage_ - armour_ : 1}; - hp_ = (hp_ > damage) ? hp_ - damage : 0; - return hp_; - } + unsigned get_attacked(Person const& attacker) + { + auto damage{(attacker.damage_ > armour_) ? attacker.damage_ - armour_ : 1}; + hp_ = (hp_ > damage) ? hp_ - damage : 0; + return hp_; + } - std::string name_; - unsigned long hp_; - unsigned long cost_; - unsigned long damage_; - unsigned long armour_; + std::string name_; + unsigned long hp_; + unsigned long cost_; + unsigned long damage_; + unsigned long armour_; }; -unsigned long get_value(std::string const &begin) { - std::string line; - if (!std::getline(std::cin, line)) { - std::cerr << "Missing line in input\n"; - std::exit(1); - } - if (line.substr(0, begin.length()) != begin) { - std::cerr << "Line doesn't begin with: " << begin << '\n'; - std::exit(1); - } - return std::stoul(line.substr(begin.length())); +unsigned long get_value(std::string const& begin) +{ + std::string line; + if (!std::getline(std::cin, line)) { + std::cerr << "Missing line in input\n"; + std::exit(1); + } + if (line.substr(0, begin.length()) != begin) { + std::cerr << "Line doesn't begin with: " << begin << '\n'; + std::exit(1); + } + return std::stoul(line.substr(begin.length())); } -bool me_beats_enemy(Person me, Person enemy) { - while (true) { - enemy.get_attacked(me); - if (enemy.hp_ == 0) { return true; } - me.get_attacked(enemy); - if (me.hp_ == 0) { return false; } +bool me_beats_enemy(Person me, Person enemy) +{ + while (true) { + enemy.get_attacked(me); + if (enemy.hp_ == 0) { + return true; } + me.get_attacked(enemy); + if (me.hp_ == 0) { + return false; + } + } } -int main() { - const std::string hp_begin{"Hit Points: "}; - const std::string d_begin{"Damage: "}; - const std::string a_begin{"Armor: "}; +int main() +{ + const std::string hp_begin{"Hit Points: "}; + const std::string d_begin{"Damage: "}; + const std::string a_begin{"Armor: "}; - const std::vector weapons{ - {"Dagger", 8, 4, 0}, - {"Shortsword", 10, 5, 0}, - {"Warhammer", 25, 6, 0}, - {"Longsword", 40, 7, 0}, - {"Greataxe", 74, 8, 0}, - }; + const std::vector weapons{ + {"Dagger", 8, 4, 0}, {"Shortsword", 10, 5, 0}, {"Warhammer", 25, 6, 0}, + {"Longsword", 40, 7, 0}, {"Greataxe", 74, 8, 0}, + }; - const std::vector armours{ - {"Nothing", 0, 0, 0}, - {"Leather", 13, 0, 1}, - {"Chainmail", 31, 0, 2}, - {"Splintmail", 53, 0, 3}, - {"Bandedmail", 75, 0, 4}, - {"Platedmail", 102, 0, 5}, - }; + const std::vector armours{ + {"Nothing", 0, 0, 0}, {"Leather", 13, 0, 1}, {"Chainmail", 31, 0, 2}, + {"Splintmail", 53, 0, 3}, {"Bandedmail", 75, 0, 4}, {"Platedmail", 102, 0, 5}, + }; - const std::vector rings{ - {"Empty", 0, 0, 0}, - {"Damage +1", 25, 1, 0}, - {"Damage +2", 50, 2, 0}, - {"Damage +3", 100, 3, 0}, - {"Defense +1", 20, 0, 1}, - {"Defense +2", 40, 0, 2}, - {"Defense +3", 80, 0, 3}, - }; + const std::vector rings{ + {"Empty", 0, 0, 0}, {"Damage +1", 25, 1, 0}, {"Damage +2", 50, 2, 0}, + {"Damage +3", 100, 3, 0}, {"Defense +1", 20, 0, 1}, {"Defense +2", 40, 0, 2}, + {"Defense +3", 80, 0, 3}, + }; + auto enemy_hp{get_value(hp_begin)}; + auto enemy_damage{get_value(d_begin)}; + auto enemy_armour{get_value(a_begin)}; - auto enemy_hp{get_value(hp_begin)}; - auto enemy_damage{get_value(d_begin)}; - auto enemy_armour{get_value(a_begin)}; + Person best_result{0, 0, 0}; + Person enemy{enemy_hp, enemy_damage, enemy_armour}; - Person best_result{0, 0, 0}; - Person enemy{enemy_hp, enemy_damage, enemy_armour}; - - for (auto const &weapon: weapons) { - if (weapon.cost_ > best_result.cost_) { continue; } - for (auto const &armour: armours) { - if (weapon.cost_ + armour.cost_ > best_result.cost_) { continue; } - for (auto const &left_ring: rings) { - if (weapon.cost_ + armour.cost_ + left_ring.cost_ > best_result.cost_) { continue; } - for (auto const &right_ring: rings) { - if (weapon.cost_ + armour.cost_ + left_ring.cost_ + right_ring.cost_ > - best_result.cost_) { continue; } - if (left_ring == right_ring) continue; - Person me{weapon, armour, left_ring, right_ring}; - if (me_beats_enemy(me, enemy) && me.cost_ < best_result.cost_) { - best_result = me; - } - } - } + for (auto const& weapon : weapons) { + if (weapon.cost_ > best_result.cost_) { + continue; + } + for (auto const& armour : armours) { + if (weapon.cost_ + armour.cost_ > best_result.cost_) { + continue; + } + for (auto const& left_ring : rings) { + if (weapon.cost_ + armour.cost_ + left_ring.cost_ > best_result.cost_) { + continue; } + for (auto const& right_ring : rings) { + if (weapon.cost_ + armour.cost_ + left_ring.cost_ + right_ring.cost_ > + best_result.cost_) { + continue; + } + if (left_ring == right_ring) + continue; + Person me{weapon, armour, left_ring, right_ring}; + if (me_beats_enemy(me, enemy) && me.cost_ < best_result.cost_) { + best_result = me; + } + } + } } + } - std::cout << "Best person: " << best_result.name_ << " at cost of " << best_result.cost_ << '\n'; + std::cout << "Best person: " << best_result.name_ << " at cost of " << best_result.cost_ << '\n'; } \ No newline at end of file diff --git a/2015/puzzle-21-02.cc b/2015/puzzle-21-02.cc index 55f0733..8873549 100644 --- a/2015/puzzle-21-02.cc +++ b/2015/puzzle-21-02.cc @@ -1,130 +1,132 @@ +#include +#include +#include #include #include -#include -#include -#include using namespace std::string_literals; -struct Item { - std::string name_; - unsigned long cost_; - unsigned long damage_; - unsigned long armour_; +struct Item +{ + std::string name_; + unsigned long cost_; + unsigned long damage_; + unsigned long armour_; - bool operator==(Item const& rhs) const { return name_ == rhs.name_; } + bool operator==(Item const& rhs) const { return name_ == rhs.name_; } }; -struct Person { - Person(unsigned long hp, unsigned long damage, unsigned long armour) : name_("Enemy"), hp_(hp), - cost_(std::numeric_limits::max()), - damage_(damage), - armour_(armour) {} +struct Person +{ + Person(unsigned long hp, unsigned long damage, unsigned long armour) + : name_("Enemy"), hp_(hp), cost_(std::numeric_limits::max()), damage_(damage), + armour_(armour) + { + } - Person(Item const &weapon, Item const &armour, Item const &left_ring, Item const &right_ring) : - name_("Player: "s + weapon.name_ + ", "s + armour.name_ + ", "s + left_ring.name_ + ", " + - right_ring.name_), - hp_(100), - cost_(weapon.cost_ + armour.cost_ + left_ring.cost_ + right_ring.cost_), - damage_(weapon.damage_ + armour.damage_ + left_ring.damage_ + right_ring.damage_), - armour_(weapon.armour_ + armour.armour_ + left_ring.armour_ + right_ring.armour_) {} + Person(Item const& weapon, Item const& armour, Item const& left_ring, Item const& right_ring) + : name_("Player: "s + weapon.name_ + ", "s + armour.name_ + ", "s + left_ring.name_ + ", " + + right_ring.name_), + hp_(100), cost_(weapon.cost_ + armour.cost_ + left_ring.cost_ + right_ring.cost_), + damage_(weapon.damage_ + armour.damage_ + left_ring.damage_ + right_ring.damage_), + armour_(weapon.armour_ + armour.armour_ + left_ring.armour_ + right_ring.armour_) + { + } - unsigned get_attacked(Person const &attacker) { - auto damage{(attacker.damage_ > armour_) ? attacker.damage_ - armour_ : 1}; - hp_ = (hp_ > damage) ? hp_ - damage : 0; - return hp_; - } + unsigned get_attacked(Person const& attacker) + { + auto damage{(attacker.damage_ > armour_) ? attacker.damage_ - armour_ : 1}; + hp_ = (hp_ > damage) ? hp_ - damage : 0; + return hp_; + } - std::string name_; - unsigned long hp_; - unsigned long cost_; - unsigned long damage_; - unsigned long armour_; + std::string name_; + unsigned long hp_; + unsigned long cost_; + unsigned long damage_; + unsigned long armour_; }; -unsigned long get_value(std::string const &begin) { - std::string line; - if (!std::getline(std::cin, line)) { - std::cerr << "Missing line in input\n"; - std::exit(1); - } - if (line.substr(0, begin.length()) != begin) { - std::cerr << "Line doesn't begin with: " << begin << '\n'; - std::exit(1); - } - return std::stoul(line.substr(begin.length())); +unsigned long get_value(std::string const& begin) +{ + std::string line; + if (!std::getline(std::cin, line)) { + std::cerr << "Missing line in input\n"; + std::exit(1); + } + if (line.substr(0, begin.length()) != begin) { + std::cerr << "Line doesn't begin with: " << begin << '\n'; + std::exit(1); + } + return std::stoul(line.substr(begin.length())); } -bool me_beats_enemy(Person me, Person enemy) { - while (true) { - enemy.get_attacked(me); - if (enemy.hp_ == 0) { return true; } - me.get_attacked(enemy); - if (me.hp_ == 0) { return false; } +bool me_beats_enemy(Person me, Person enemy) +{ + while (true) { + enemy.get_attacked(me); + if (enemy.hp_ == 0) { + return true; } + me.get_attacked(enemy); + if (me.hp_ == 0) { + return false; + } + } } -int main() { - const std::string hp_begin{"Hit Points: "}; - const std::string d_begin{"Damage: "}; - const std::string a_begin{"Armor: "}; +int main() +{ + const std::string hp_begin{"Hit Points: "}; + const std::string d_begin{"Damage: "}; + const std::string a_begin{"Armor: "}; - const std::vector weapons{ - {"Dagger", 8, 4, 0}, - {"Shortsword", 10, 5, 0}, - {"Warhammer", 25, 6, 0}, - {"Longsword", 40, 7, 0}, - {"Greataxe", 74, 8, 0}, - }; + const std::vector weapons{ + {"Dagger", 8, 4, 0}, {"Shortsword", 10, 5, 0}, {"Warhammer", 25, 6, 0}, + {"Longsword", 40, 7, 0}, {"Greataxe", 74, 8, 0}, + }; - const std::vector armours{ - {"Nothing", 0, 0, 0}, - {"Leather", 13, 0, 1}, - {"Chainmail", 31, 0, 2}, - {"Splintmail", 53, 0, 3}, - {"Bandedmail", 75, 0, 4}, - {"Platedmail", 102, 0, 5}, - }; + const std::vector armours{ + {"Nothing", 0, 0, 0}, {"Leather", 13, 0, 1}, {"Chainmail", 31, 0, 2}, + {"Splintmail", 53, 0, 3}, {"Bandedmail", 75, 0, 4}, {"Platedmail", 102, 0, 5}, + }; - const std::vector rings{ - {"Empty", 0, 0, 0}, - {"Damage +1", 25, 1, 0}, - {"Damage +2", 50, 2, 0}, - {"Damage +3", 100, 3, 0}, - {"Defense +1", 20, 0, 1}, - {"Defense +2", 40, 0, 2}, - {"Defense +3", 80, 0, 3}, - }; + const std::vector rings{ + {"Empty", 0, 0, 0}, {"Damage +1", 25, 1, 0}, {"Damage +2", 50, 2, 0}, + {"Damage +3", 100, 3, 0}, {"Defense +1", 20, 0, 1}, {"Defense +2", 40, 0, 2}, + {"Defense +3", 80, 0, 3}, + }; + auto enemy_hp{get_value(hp_begin)}; + auto enemy_damage{get_value(d_begin)}; + auto enemy_armour{get_value(a_begin)}; - auto enemy_hp{get_value(hp_begin)}; - auto enemy_damage{get_value(d_begin)}; - auto enemy_armour{get_value(a_begin)}; + Person best_result{0, 0, 0}; + Person worst_result{0, 0, 0}; + worst_result.cost_ = 0; + Person enemy{enemy_hp, enemy_damage, enemy_armour}; - Person best_result{0, 0, 0}; - Person worst_result{0, 0, 0}; - worst_result.cost_ = 0; - Person enemy{enemy_hp, enemy_damage, enemy_armour}; - - for (auto const &weapon: weapons) { - for (auto const &armour: armours) { - for (auto const &left_ring: rings) { - for (auto const &right_ring: rings) { - if (left_ring == right_ring) continue; - Person me{weapon, armour, left_ring, right_ring}; - if (me_beats_enemy(me, enemy)) { - if (me.cost_ < best_result.cost_) { - best_result = me; - } - } - else if (me.cost_ > worst_result.cost_) { - worst_result = me; - } - } + for (auto const& weapon : weapons) { + for (auto const& armour : armours) { + for (auto const& left_ring : rings) { + for (auto const& right_ring : rings) { + if (left_ring == right_ring) + continue; + Person me{weapon, armour, left_ring, right_ring}; + if (me_beats_enemy(me, enemy)) { + if (me.cost_ < best_result.cost_) { + best_result = me; } + } + else if (me.cost_ > worst_result.cost_) { + worst_result = me; + } } + } } + } - std::cout << "Best person: " << best_result.name_ << " at cost of " << best_result.cost_ << '\n'; - std::cout << "Worst person: " << worst_result.name_ << " at cost of " << worst_result.cost_ << '\n'; + std::cout << "Best person: " << best_result.name_ << " at cost of " << best_result.cost_ << '\n'; + std::cout << "Worst person: " << worst_result.name_ << " at cost of " << worst_result.cost_ + << '\n'; } \ No newline at end of file diff --git a/2015/puzzle-22-01.cc b/2015/puzzle-22-01.cc index 27cb609..6de3a80 100644 --- a/2015/puzzle-22-01.cc +++ b/2015/puzzle-22-01.cc @@ -1,127 +1,143 @@ +#include +#include +#include +#include #include #include -#include -#include -#include -#include using namespace std::string_literals; -struct State { - State(unsigned long enemy_hp, unsigned long enemy_damage) : enemy_hp_(enemy_hp), enemy_damage_(enemy_damage) {} +struct State +{ + State(unsigned long enemy_hp, unsigned long enemy_damage) + : enemy_hp_(enemy_hp), enemy_damage_(enemy_damage) + { + } - unsigned long my_hp_{50}; - unsigned long my_mana_{500}; - unsigned long my_armour_{0}; - unsigned long enemy_hp_; - unsigned long enemy_damage_; - unsigned long shield_counter_{0}; - unsigned long poison_counter_{0}; - unsigned long recharge_counter_{0}; - unsigned long cost_{0}; + unsigned long my_hp_{50}; + unsigned long my_mana_{500}; + unsigned long my_armour_{0}; + unsigned long enemy_hp_; + unsigned long enemy_damage_; + unsigned long shield_counter_{0}; + unsigned long poison_counter_{0}; + unsigned long recharge_counter_{0}; + unsigned long cost_{0}; - bool operator==(State const &rhs) const { - return my_hp_ == rhs.my_hp_ && my_mana_ == rhs.my_mana_ && my_armour_ == rhs.my_armour_ && - enemy_hp_ == rhs.enemy_hp_ && enemy_damage_ == rhs.enemy_damage_ && - shield_counter_ == rhs.shield_counter_ && poison_counter_ == rhs.poison_counter_ && - recharge_counter_ == rhs.recharge_counter_; + bool operator==(State const& rhs) const + { + return my_hp_ == rhs.my_hp_ && my_mana_ == rhs.my_mana_ && my_armour_ == rhs.my_armour_ && + enemy_hp_ == rhs.enemy_hp_ && enemy_damage_ == rhs.enemy_damage_ && + shield_counter_ == rhs.shield_counter_ && poison_counter_ == rhs.poison_counter_ && + recharge_counter_ == rhs.recharge_counter_; + } + + bool apply_effects() + { + if (shield_counter_ > 0) { + if (--shield_counter_ == 0) { + my_armour_ -= 7; + } } - - bool apply_effects() { - if (shield_counter_ > 0) { - if (--shield_counter_ == 0) { - my_armour_ -= 7; - } - } - if (poison_counter_ > 0) { - hit_enemy(3); - --poison_counter_; - } - if (recharge_counter_ > 0) { - my_mana_ += 101; - --recharge_counter_; - } - return enemy_hp_ == 0; + if (poison_counter_ > 0) { + hit_enemy(3); + --poison_counter_; } - - void hit_enemy(unsigned long damage) { - enemy_hp_ = (damage > enemy_hp_) ? 0 : enemy_hp_ - damage; + if (recharge_counter_ > 0) { + my_mana_ += 101; + --recharge_counter_; } + return enemy_hp_ == 0; + } - void hit_me() { - auto amt{my_armour_ >= enemy_damage_ ? 1 : enemy_damage_ - my_armour_}; - my_hp_ = (amt > my_hp_) ? 0 : my_hp_ - amt; - } + void hit_enemy(unsigned long damage) + { + enemy_hp_ = (damage > enemy_hp_) ? 0 : enemy_hp_ - damage; + } - void spend_mana(unsigned long amt) { - assert(my_mana_ >= amt); - my_mana_ -= amt; - cost_ += amt; - } + void hit_me() + { + auto amt{my_armour_ >= enemy_damage_ ? 1 : enemy_damage_ - my_armour_}; + my_hp_ = (amt > my_hp_) ? 0 : my_hp_ - amt; + } - void enemy_turn() { - if (!apply_effects()) { - hit_me(); - } - } + void spend_mana(unsigned long amt) + { + assert(my_mana_ >= amt); + my_mana_ -= amt; + cost_ += amt; + } - [[nodiscard]] State magic_missile() const { - State next{*this}; - next.spend_mana(53); - next.hit_enemy(4); - next.enemy_turn(); - return next; + void enemy_turn() + { + if (!apply_effects()) { + hit_me(); } + } - [[nodiscard]] State drain() const { - State next{*this}; - next.spend_mana(73); - next.hit_enemy(2); - next.my_hp_ += 2; - next.enemy_turn(); - return next; - } + [[nodiscard]] State magic_missile() const + { + State next{*this}; + next.spend_mana(53); + next.hit_enemy(4); + next.enemy_turn(); + return next; + } - [[nodiscard]] State shield() const { - State next{*this}; - assert(shield_counter_ == 0); - next.spend_mana(113); - next.my_armour_ += 7; - next.shield_counter_ = 6; - next.enemy_turn(); - return next; - } + [[nodiscard]] State drain() const + { + State next{*this}; + next.spend_mana(73); + next.hit_enemy(2); + next.my_hp_ += 2; + next.enemy_turn(); + return next; + } - [[nodiscard]] State poison() const { - State next{*this}; - assert(poison_counter_ == 0); - next.spend_mana(173); - next.poison_counter_ = 6; - next.enemy_turn(); - return next; - } + [[nodiscard]] State shield() const + { + State next{*this}; + assert(shield_counter_ == 0); + next.spend_mana(113); + next.my_armour_ += 7; + next.shield_counter_ = 6; + next.enemy_turn(); + return next; + } - [[nodiscard]] State recharge() const { - State next{*this}; - assert(recharge_counter_ == 0); - next.spend_mana(229); - next.recharge_counter_ = 5; - next.enemy_turn(); - return next; - } + [[nodiscard]] State poison() const + { + State next{*this}; + assert(poison_counter_ == 0); + next.spend_mana(173); + next.poison_counter_ = 6; + next.enemy_turn(); + return next; + } + + [[nodiscard]] State recharge() const + { + State next{*this}; + assert(recharge_counter_ == 0); + next.spend_mana(229); + next.recharge_counter_ = 5; + next.enemy_turn(); + return next; + } }; -unsigned long get_value(std::string const &begin) { - std::string line; - if (!std::getline(std::cin, line)) { - std::cerr << "Missing line in input\n"; - std::exit(1); - } - if (line.substr(0, begin.length()) != begin) { - std::cerr << "Line doesn't begin with: " << begin << '\n'; - std::exit(1); - } - return std::stoul(line.substr(begin.length())); +unsigned long get_value(std::string const& begin) +{ + std::string line; + if (!std::getline(std::cin, line)) { + std::cerr << "Missing line in input\n"; + std::exit(1); + } + if (line.substr(0, begin.length()) != begin) { + std::cerr << "Line doesn't begin with: " << begin << '\n'; + std::exit(1); + } + return std::stoul(line.substr(begin.length())); } /** \brief Add \a state to \a states. @@ -129,51 +145,55 @@ unsigned long get_value(std::string const &begin) { * @param states Vector of states * @param state State to add. * - * If there is already an element in \a states that is the same as \a state we don't add a new state, instead we just - * update the cost to the minimum. + * If there is already an element in \a states that is the same as \a state we don't add a new + * state, instead we just update the cost to the minimum. */ -void add_state(std::vector &states, State const &state) { - auto it = std::find(states.begin(), states.end(), state); - if (it == states.end()) { - states.push_back(state); - } else if (it->cost_ > state.cost_) { - it->cost_ = state.cost_; - } +void add_state(std::vector& states, State const& state) +{ + auto it = std::find(states.begin(), states.end(), state); + if (it == states.end()) { + states.push_back(state); + } + else if (it->cost_ > state.cost_) { + it->cost_ = state.cost_; + } } -int main() { - const std::string hp_begin{"Hit Points: "}; - const std::string d_begin{"Damage: "}; +int main() +{ + const std::string hp_begin{"Hit Points: "}; + const std::string d_begin{"Damage: "}; - auto enemy_hp{get_value(hp_begin)}; - auto enemy_damage{get_value(d_begin)}; + auto enemy_hp{get_value(hp_begin)}; + auto enemy_damage{get_value(d_begin)}; - /* States - list of states we've generated. */ - std::vector states; - states.emplace_back(enemy_hp, enemy_damage); + /* States - list of states we've generated. */ + std::vector states; + states.emplace_back(enemy_hp, enemy_damage); - /* Cost of the cheapest winning state so far. */ - auto best_cost{std::numeric_limits::max()}; + /* Cost of the cheapest winning state so far. */ + auto best_cost{std::numeric_limits::max()}; - while (!states.empty()) { - /* Get the lowest cost element in the list of states and process that. */ - auto it = std::min_element(states.begin(), states.end(), - [](State const &lhs, State const &rhs) { return lhs.cost_ < rhs.cost_; }); - State candidate{*it}; - states.erase(it); - if (candidate.cost_ >= best_cost) { - /* Because we've searched for the minimum element above we know that all future candidates are going to - * cost more than the current best cost for winning - so just stop here. */ - break; - } + while (!states.empty()) { + /* Get the lowest cost element in the list of states and process that. */ + auto it = + std::min_element(states.begin(), states.end(), + [](State const& lhs, State const& rhs) { return lhs.cost_ < rhs.cost_; }); + State candidate{*it}; + states.erase(it); + if (candidate.cost_ >= best_cost) { + /* Because we've searched for the minimum element above we know that all future candidates are + * going to cost more than the current best cost for winning - so just stop here. */ + break; + } - /* Someone has died so this is a winning state - deal with it. */ - if (candidate.my_hp_ == 0 || candidate.enemy_hp_ == 0) { - if (candidate.enemy_hp_ == 0 && candidate.cost_ < best_cost) { - best_cost = candidate.cost_; - } - continue; - } + /* Someone has died so this is a winning state - deal with it. */ + if (candidate.my_hp_ == 0 || candidate.enemy_hp_ == 0) { + if (candidate.enemy_hp_ == 0 && candidate.cost_ < best_cost) { + best_cost = candidate.cost_; + } + continue; + } #if 0 /* Part two's addition. */ @@ -182,35 +202,35 @@ int main() { } #endif - /* Apply effects at start of our turn. If someone dies push this state back on the queue. */ - if (candidate.apply_effects()) { - states.push_back(candidate); - } - - /* Handle each of our options (followed by the enemy's turn). Making sure that when we add the state we - * don't duplicate ourselves. - */ - if (candidate.my_mana_ >= 53) { - add_state(states, candidate.magic_missile()); - } - - if (candidate.my_mana_ >= 73) { - add_state(states, candidate.drain()); - } - - if (candidate.my_mana_ >= 113 && candidate.shield_counter_ == 0) { - add_state(states, candidate.shield()); - } - - if (candidate.my_mana_ >= 173 && candidate.poison_counter_ == 0) { - add_state(states, candidate.poison()); - } - - if (candidate.my_mana_ >= 229 && candidate.recharge_counter_ == 0) { - add_state(states, candidate.recharge()); - } + /* Apply effects at start of our turn. If someone dies push this state back on the queue. */ + if (candidate.apply_effects()) { + states.push_back(candidate); } - std::cout << "Best cost " << best_cost << "\n"; - return 1; + /* Handle each of our options (followed by the enemy's turn). Making sure that when we add the + * state we don't duplicate ourselves. + */ + if (candidate.my_mana_ >= 53) { + add_state(states, candidate.magic_missile()); + } + + if (candidate.my_mana_ >= 73) { + add_state(states, candidate.drain()); + } + + if (candidate.my_mana_ >= 113 && candidate.shield_counter_ == 0) { + add_state(states, candidate.shield()); + } + + if (candidate.my_mana_ >= 173 && candidate.poison_counter_ == 0) { + add_state(states, candidate.poison()); + } + + if (candidate.my_mana_ >= 229 && candidate.recharge_counter_ == 0) { + add_state(states, candidate.recharge()); + } + } + + std::cout << "Best cost " << best_cost << "\n"; + return 1; } \ No newline at end of file diff --git a/2015/puzzle-22-02.cc b/2015/puzzle-22-02.cc index 7ceb59f..814fcda 100644 --- a/2015/puzzle-22-02.cc +++ b/2015/puzzle-22-02.cc @@ -1,127 +1,143 @@ +#include +#include +#include +#include #include #include -#include -#include -#include -#include using namespace std::string_literals; -struct State { - State(unsigned long enemy_hp, unsigned long enemy_damage) : enemy_hp_(enemy_hp), enemy_damage_(enemy_damage) {} +struct State +{ + State(unsigned long enemy_hp, unsigned long enemy_damage) + : enemy_hp_(enemy_hp), enemy_damage_(enemy_damage) + { + } - unsigned long my_hp_{50}; - unsigned long my_mana_{500}; - unsigned long my_armour_{0}; - unsigned long enemy_hp_; - unsigned long enemy_damage_; - unsigned long shield_counter_{0}; - unsigned long poison_counter_{0}; - unsigned long recharge_counter_{0}; - unsigned long cost_{0}; + unsigned long my_hp_{50}; + unsigned long my_mana_{500}; + unsigned long my_armour_{0}; + unsigned long enemy_hp_; + unsigned long enemy_damage_; + unsigned long shield_counter_{0}; + unsigned long poison_counter_{0}; + unsigned long recharge_counter_{0}; + unsigned long cost_{0}; - bool operator==(State const &rhs) const { - return my_hp_ == rhs.my_hp_ && my_mana_ == rhs.my_mana_ && my_armour_ == rhs.my_armour_ && - enemy_hp_ == rhs.enemy_hp_ && enemy_damage_ == rhs.enemy_damage_ && - shield_counter_ == rhs.shield_counter_ && poison_counter_ == rhs.poison_counter_ && - recharge_counter_ == rhs.recharge_counter_; + bool operator==(State const& rhs) const + { + return my_hp_ == rhs.my_hp_ && my_mana_ == rhs.my_mana_ && my_armour_ == rhs.my_armour_ && + enemy_hp_ == rhs.enemy_hp_ && enemy_damage_ == rhs.enemy_damage_ && + shield_counter_ == rhs.shield_counter_ && poison_counter_ == rhs.poison_counter_ && + recharge_counter_ == rhs.recharge_counter_; + } + + bool apply_effects() + { + if (shield_counter_ > 0) { + if (--shield_counter_ == 0) { + my_armour_ -= 7; + } } - - bool apply_effects() { - if (shield_counter_ > 0) { - if (--shield_counter_ == 0) { - my_armour_ -= 7; - } - } - if (poison_counter_ > 0) { - hit_enemy(3); - --poison_counter_; - } - if (recharge_counter_ > 0) { - my_mana_ += 101; - --recharge_counter_; - } - return enemy_hp_ == 0; + if (poison_counter_ > 0) { + hit_enemy(3); + --poison_counter_; } - - void hit_enemy(unsigned long damage) { - enemy_hp_ = (damage > enemy_hp_) ? 0 : enemy_hp_ - damage; + if (recharge_counter_ > 0) { + my_mana_ += 101; + --recharge_counter_; } + return enemy_hp_ == 0; + } - void hit_me() { - auto amt{my_armour_ >= enemy_damage_ ? 1 : enemy_damage_ - my_armour_}; - my_hp_ = (amt > my_hp_) ? 0 : my_hp_ - amt; - } + void hit_enemy(unsigned long damage) + { + enemy_hp_ = (damage > enemy_hp_) ? 0 : enemy_hp_ - damage; + } - void spend_mana(unsigned long amt) { - assert(my_mana_ >= amt); - my_mana_ -= amt; - cost_ += amt; - } + void hit_me() + { + auto amt{my_armour_ >= enemy_damage_ ? 1 : enemy_damage_ - my_armour_}; + my_hp_ = (amt > my_hp_) ? 0 : my_hp_ - amt; + } - void enemy_turn() { - if (!apply_effects()) { - hit_me(); - } - } + void spend_mana(unsigned long amt) + { + assert(my_mana_ >= amt); + my_mana_ -= amt; + cost_ += amt; + } - [[nodiscard]] State magic_missile() const { - State next{*this}; - next.spend_mana(53); - next.hit_enemy(4); - next.enemy_turn(); - return next; + void enemy_turn() + { + if (!apply_effects()) { + hit_me(); } + } - [[nodiscard]] State drain() const { - State next{*this}; - next.spend_mana(73); - next.hit_enemy(2); - next.my_hp_ += 2; - next.enemy_turn(); - return next; - } + [[nodiscard]] State magic_missile() const + { + State next{*this}; + next.spend_mana(53); + next.hit_enemy(4); + next.enemy_turn(); + return next; + } - [[nodiscard]] State shield() const { - State next{*this}; - assert(shield_counter_ == 0); - next.spend_mana(113); - next.my_armour_ += 7; - next.shield_counter_ = 6; - next.enemy_turn(); - return next; - } + [[nodiscard]] State drain() const + { + State next{*this}; + next.spend_mana(73); + next.hit_enemy(2); + next.my_hp_ += 2; + next.enemy_turn(); + return next; + } - [[nodiscard]] State poison() const { - State next{*this}; - assert(poison_counter_ == 0); - next.spend_mana(173); - next.poison_counter_ = 6; - next.enemy_turn(); - return next; - } + [[nodiscard]] State shield() const + { + State next{*this}; + assert(shield_counter_ == 0); + next.spend_mana(113); + next.my_armour_ += 7; + next.shield_counter_ = 6; + next.enemy_turn(); + return next; + } - [[nodiscard]] State recharge() const { - State next{*this}; - assert(recharge_counter_ == 0); - next.spend_mana(229); - next.recharge_counter_ = 5; - next.enemy_turn(); - return next; - } + [[nodiscard]] State poison() const + { + State next{*this}; + assert(poison_counter_ == 0); + next.spend_mana(173); + next.poison_counter_ = 6; + next.enemy_turn(); + return next; + } + + [[nodiscard]] State recharge() const + { + State next{*this}; + assert(recharge_counter_ == 0); + next.spend_mana(229); + next.recharge_counter_ = 5; + next.enemy_turn(); + return next; + } }; -unsigned long get_value(std::string const &begin) { - std::string line; - if (!std::getline(std::cin, line)) { - std::cerr << "Missing line in input\n"; - std::exit(1); - } - if (line.substr(0, begin.length()) != begin) { - std::cerr << "Line doesn't begin with: " << begin << '\n'; - std::exit(1); - } - return std::stoul(line.substr(begin.length())); +unsigned long get_value(std::string const& begin) +{ + std::string line; + if (!std::getline(std::cin, line)) { + std::cerr << "Missing line in input\n"; + std::exit(1); + } + if (line.substr(0, begin.length()) != begin) { + std::cerr << "Line doesn't begin with: " << begin << '\n'; + std::exit(1); + } + return std::stoul(line.substr(begin.length())); } /** \brief Add \a state to \a states. @@ -129,86 +145,90 @@ unsigned long get_value(std::string const &begin) { * @param states Vector of states * @param state State to add. * - * If there is already an element in \a states that is the same as \a state we don't add a new state, instead we just - * update the cost to the minimum. + * If there is already an element in \a states that is the same as \a state we don't add a new + * state, instead we just update the cost to the minimum. */ -void add_state(std::vector &states, State const &state) { - auto it = std::find(states.begin(), states.end(), state); - if (it == states.end()) { - states.push_back(state); - } else if (it->cost_ > state.cost_) { - it->cost_ = state.cost_; - } +void add_state(std::vector& states, State const& state) +{ + auto it = std::find(states.begin(), states.end(), state); + if (it == states.end()) { + states.push_back(state); + } + else if (it->cost_ > state.cost_) { + it->cost_ = state.cost_; + } } -int main() { - const std::string hp_begin{"Hit Points: "}; - const std::string d_begin{"Damage: "}; +int main() +{ + const std::string hp_begin{"Hit Points: "}; + const std::string d_begin{"Damage: "}; - auto enemy_hp{get_value(hp_begin)}; - auto enemy_damage{get_value(d_begin)}; + auto enemy_hp{get_value(hp_begin)}; + auto enemy_damage{get_value(d_begin)}; - /* States - list of states we've generated. */ - std::vector states; - states.emplace_back(enemy_hp, enemy_damage); + /* States - list of states we've generated. */ + std::vector states; + states.emplace_back(enemy_hp, enemy_damage); - /* Cost of the cheapest winning state so far. */ - auto best_cost{std::numeric_limits::max()}; + /* Cost of the cheapest winning state so far. */ + auto best_cost{std::numeric_limits::max()}; - while (!states.empty()) { - /* Get the lowest cost element in the list of states and process that. */ - auto it = std::min_element(states.begin(), states.end(), - [](State const &lhs, State const &rhs) { return lhs.cost_ < rhs.cost_; }); - State candidate{*it}; - states.erase(it); - if (candidate.cost_ >= best_cost) { - /* Because we've searched for the minimum element above we know that all future candidates are going to - * cost more than the current best cost for winning - so just stop here. */ - break; - } - - /* Someone has died so this is a winning state - deal with it. */ - if (candidate.my_hp_ == 0 || candidate.enemy_hp_ == 0) { - if (candidate.enemy_hp_ == 0 && candidate.cost_ < best_cost) { - best_cost = candidate.cost_; - } - continue; - } - - /* Part two's addition - we lose health at the start of our turn. */ - if (--candidate.my_hp_ == 0) { - continue; - } - - /* Apply effects at start of our turn. If someone dies push this state back on the queue. */ - if (candidate.apply_effects()) { - states.push_back(candidate); - } - - /* Handle each of our options (followed by the enemy's turn). Making sure that when we add the state we - * don't duplicate ourselves. - */ - if (candidate.my_mana_ >= 53) { - add_state(states, candidate.magic_missile()); - } - - if (candidate.my_mana_ >= 73) { - add_state(states, candidate.drain()); - } - - if (candidate.my_mana_ >= 113 && candidate.shield_counter_ == 0) { - add_state(states, candidate.shield()); - } - - if (candidate.my_mana_ >= 173 && candidate.poison_counter_ == 0) { - add_state(states, candidate.poison()); - } - - if (candidate.my_mana_ >= 229 && candidate.recharge_counter_ == 0) { - add_state(states, candidate.recharge()); - } + while (!states.empty()) { + /* Get the lowest cost element in the list of states and process that. */ + auto it = + std::min_element(states.begin(), states.end(), + [](State const& lhs, State const& rhs) { return lhs.cost_ < rhs.cost_; }); + State candidate{*it}; + states.erase(it); + if (candidate.cost_ >= best_cost) { + /* Because we've searched for the minimum element above we know that all future candidates are + * going to cost more than the current best cost for winning - so just stop here. */ + break; } - std::cout << "Best cost " << best_cost << "\n"; - return 1; + /* Someone has died so this is a winning state - deal with it. */ + if (candidate.my_hp_ == 0 || candidate.enemy_hp_ == 0) { + if (candidate.enemy_hp_ == 0 && candidate.cost_ < best_cost) { + best_cost = candidate.cost_; + } + continue; + } + + /* Part two's addition - we lose health at the start of our turn. */ + if (--candidate.my_hp_ == 0) { + continue; + } + + /* Apply effects at start of our turn. If someone dies push this state back on the queue. */ + if (candidate.apply_effects()) { + states.push_back(candidate); + } + + /* Handle each of our options (followed by the enemy's turn). Making sure that when we add the + * state we don't duplicate ourselves. + */ + if (candidate.my_mana_ >= 53) { + add_state(states, candidate.magic_missile()); + } + + if (candidate.my_mana_ >= 73) { + add_state(states, candidate.drain()); + } + + if (candidate.my_mana_ >= 113 && candidate.shield_counter_ == 0) { + add_state(states, candidate.shield()); + } + + if (candidate.my_mana_ >= 173 && candidate.poison_counter_ == 0) { + add_state(states, candidate.poison()); + } + + if (candidate.my_mana_ >= 229 && candidate.recharge_counter_ == 0) { + add_state(states, candidate.recharge()); + } + } + + std::cout << "Best cost " << best_cost << "\n"; + return 1; } \ No newline at end of file diff --git a/2015/puzzle-23-01.cc b/2015/puzzle-23-01.cc index 21a1d04..85de73b 100644 --- a/2015/puzzle-23-01.cc +++ b/2015/puzzle-23-01.cc @@ -1,129 +1,138 @@ +#include +#include +#include +#include +#include #include #include -#include -#include -#include -#include -#include -enum class Op { - hlf, tpl, inc, jmp, jie, jio -}; -enum class Reg { - a, b +enum class Op { hlf, tpl, inc, jmp, jie, jio }; +enum class Reg { a, b }; + +struct Instr +{ + static Op get_opcode(std::string const& str) + { + static std::map ops{{"hlf ", Op::hlf}, {"tpl ", Op::tpl}, {"inc ", Op::inc}, + {"jmp ", Op::jmp}, {"jie ", Op::jie}, {"jio ", Op::jio}}; + auto it = ops.find(str.substr(0, 4)); + assert(it != ops.end()); + return it->second; + } + + static Reg get_register(std::string const& str) + { + assert(str.size() >= 1); + if (str[0] == 'a') { + return Reg::a; + } + else if (str[0] == 'b') { + return Reg::b; + } + else { + abort(); + } + } + + static long get_offset(std::string const& str) { return std::stol(str); } + + explicit Instr(std::string const& str) + { + op_ = get_opcode(str); + switch (op_) { + case Op::hlf: + case Op::tpl: + case Op::inc: + reg_ = get_register(str.substr(4)); + pc_add_ = 1; + assert(str.length() == 5); + break; + case Op::jmp: + reg_ = Reg::a; + pc_add_ = get_offset(str.substr(4)); + break; + case Op::jie: + case Op::jio: + reg_ = get_register(str.substr(4)); + assert(str.at(5) == ','); + assert(str.at(6) == ' '); + pc_add_ = get_offset(str.substr(7)); + break; + default: + abort(); + } + } + + Op op_; + Reg reg_; + long pc_add_; }; -struct Instr { - static Op get_opcode(std::string const &str) { - static std::map ops{ - {"hlf ", Op::hlf}, - {"tpl ", Op::tpl}, - {"inc ", Op::inc}, - {"jmp ", Op::jmp}, - {"jie ", Op::jie}, - {"jio ", Op::jio} - }; - auto it = ops.find(str.substr(0, 4)); - assert(it != ops.end()); - return it->second; - } +struct State +{ + unsigned long a_{0}; + unsigned long b_{0}; + unsigned long pc_{0}; - static Reg get_register(std::string const &str) { - assert(str.size() >= 1); - if (str[0] == 'a') { return Reg::a; } - else if (str[0] == 'b') { return Reg::b; } - else { abort(); } + unsigned long& reg(Reg r) + { + switch (r) { + case Reg::a: + return a_; + case Reg::b: + return b_; + default: + abort(); } + } - static long get_offset(std::string const &str) { - return std::stol(str); + void execute(Instr const& instruction) + { + switch (instruction.op_) { + case Op::hlf: + reg(instruction.reg_) /= 2; + pc_ += instruction.pc_add_; + break; + case Op::tpl: + reg(instruction.reg_) *= 3; + pc_ += instruction.pc_add_; + break; + case Op::inc: + reg(instruction.reg_) += 1; + pc_ += instruction.pc_add_; + break; + case Op::jmp: + pc_ += instruction.pc_add_; + break; + case Op::jie: + if (reg(instruction.reg_) % 2 == 0) + pc_ += instruction.pc_add_; + else + pc_ += 1; + break; + case Op::jio: + if (reg(instruction.reg_) == 1) + pc_ += instruction.pc_add_; + else + pc_ += 1; + break; + default: + abort(); } - - explicit Instr(std::string const &str) { - op_ = get_opcode(str); - switch (op_) { - case Op::hlf: - case Op::tpl: - case Op::inc: - reg_ = get_register(str.substr(4)); - pc_add_ = 1; - assert(str.length() == 5); - break; - case Op::jmp: - reg_ = Reg::a; - pc_add_ = get_offset(str.substr(4)); - break; - case Op::jie: - case Op::jio: - reg_ = get_register(str.substr(4)); - assert(str.at(5) == ','); - assert(str.at(6) == ' '); - pc_add_ = get_offset(str.substr(7)); - break; - default: - abort(); - } - } - - Op op_; - Reg reg_; - long pc_add_; + } }; -struct State { - unsigned long a_{0}; - unsigned long b_{0}; - unsigned long pc_{0}; +int main() +{ + std::vector instructions; + std::string line; + while (std::getline(std::cin, line)) { + instructions.emplace_back(line); + } - unsigned long& reg(Reg r) { - switch (r) { - case Reg::a: return a_; - case Reg::b: return b_; - default: abort(); - } - } - - void execute(Instr const &instruction) { - switch (instruction.op_) { - case Op::hlf: - reg(instruction.reg_) /= 2; - pc_ += instruction.pc_add_; - break; - case Op::tpl: - reg(instruction.reg_) *= 3; - pc_ += instruction.pc_add_; - break; - case Op::inc: - reg(instruction.reg_) += 1; - pc_ += instruction.pc_add_; - break; - case Op::jmp: - pc_ += instruction.pc_add_; - break; - case Op::jie: - if (reg(instruction.reg_) % 2 == 0) pc_ += instruction.pc_add_; - else pc_ += 1; - break; - case Op::jio: - if (reg(instruction.reg_) == 1) pc_ += instruction.pc_add_; - else pc_ += 1; - break; - default: - abort(); - } - } -}; - -int main() { - std::vector instructions; - std::string line; - while (std::getline(std::cin, line)) { - instructions.emplace_back(line); - } - - State state; - while (state.pc_ < instructions.size()) { - state.execute(instructions[state.pc_]); - } - std::cout << "a = " << state.a_ << "\nb = " << state.b_ << '\n'; + State state; + while (state.pc_ < instructions.size()) { + state.execute(instructions[state.pc_]); + } + std::cout << "a = " << state.a_ << "\nb = " << state.b_ << '\n'; } \ No newline at end of file diff --git a/2015/puzzle-23-02.cc b/2015/puzzle-23-02.cc index cc821fe..62c56a2 100644 --- a/2015/puzzle-23-02.cc +++ b/2015/puzzle-23-02.cc @@ -1,129 +1,138 @@ +#include +#include +#include +#include +#include #include #include -#include -#include -#include -#include -#include -enum class Op { - hlf, tpl, inc, jmp, jie, jio -}; -enum class Reg { - a, b +enum class Op { hlf, tpl, inc, jmp, jie, jio }; +enum class Reg { a, b }; + +struct Instr +{ + static Op get_opcode(std::string const& str) + { + static std::map ops{{"hlf ", Op::hlf}, {"tpl ", Op::tpl}, {"inc ", Op::inc}, + {"jmp ", Op::jmp}, {"jie ", Op::jie}, {"jio ", Op::jio}}; + auto it = ops.find(str.substr(0, 4)); + assert(it != ops.end()); + return it->second; + } + + static Reg get_register(std::string const& str) + { + assert(str.size() >= 1); + if (str[0] == 'a') { + return Reg::a; + } + else if (str[0] == 'b') { + return Reg::b; + } + else { + abort(); + } + } + + static long get_offset(std::string const& str) { return std::stol(str); } + + explicit Instr(std::string const& str) + { + op_ = get_opcode(str); + switch (op_) { + case Op::hlf: + case Op::tpl: + case Op::inc: + reg_ = get_register(str.substr(4)); + pc_add_ = 1; + assert(str.length() == 5); + break; + case Op::jmp: + reg_ = Reg::a; + pc_add_ = get_offset(str.substr(4)); + break; + case Op::jie: + case Op::jio: + reg_ = get_register(str.substr(4)); + assert(str.at(5) == ','); + assert(str.at(6) == ' '); + pc_add_ = get_offset(str.substr(7)); + break; + default: + abort(); + } + } + + Op op_; + Reg reg_; + long pc_add_; }; -struct Instr { - static Op get_opcode(std::string const &str) { - static std::map ops{ - {"hlf ", Op::hlf}, - {"tpl ", Op::tpl}, - {"inc ", Op::inc}, - {"jmp ", Op::jmp}, - {"jie ", Op::jie}, - {"jio ", Op::jio} - }; - auto it = ops.find(str.substr(0, 4)); - assert(it != ops.end()); - return it->second; - } +struct State +{ + unsigned long a_{1}; + unsigned long b_{0}; + unsigned long pc_{0}; - static Reg get_register(std::string const &str) { - assert(str.size() >= 1); - if (str[0] == 'a') { return Reg::a; } - else if (str[0] == 'b') { return Reg::b; } - else { abort(); } + unsigned long& reg(Reg r) + { + switch (r) { + case Reg::a: + return a_; + case Reg::b: + return b_; + default: + abort(); } + } - static long get_offset(std::string const &str) { - return std::stol(str); + void execute(Instr const& instruction) + { + switch (instruction.op_) { + case Op::hlf: + reg(instruction.reg_) /= 2; + pc_ += instruction.pc_add_; + break; + case Op::tpl: + reg(instruction.reg_) *= 3; + pc_ += instruction.pc_add_; + break; + case Op::inc: + reg(instruction.reg_) += 1; + pc_ += instruction.pc_add_; + break; + case Op::jmp: + pc_ += instruction.pc_add_; + break; + case Op::jie: + if (reg(instruction.reg_) % 2 == 0) + pc_ += instruction.pc_add_; + else + pc_ += 1; + break; + case Op::jio: + if (reg(instruction.reg_) == 1) + pc_ += instruction.pc_add_; + else + pc_ += 1; + break; + default: + abort(); } - - explicit Instr(std::string const &str) { - op_ = get_opcode(str); - switch (op_) { - case Op::hlf: - case Op::tpl: - case Op::inc: - reg_ = get_register(str.substr(4)); - pc_add_ = 1; - assert(str.length() == 5); - break; - case Op::jmp: - reg_ = Reg::a; - pc_add_ = get_offset(str.substr(4)); - break; - case Op::jie: - case Op::jio: - reg_ = get_register(str.substr(4)); - assert(str.at(5) == ','); - assert(str.at(6) == ' '); - pc_add_ = get_offset(str.substr(7)); - break; - default: - abort(); - } - } - - Op op_; - Reg reg_; - long pc_add_; + } }; -struct State { - unsigned long a_{1}; - unsigned long b_{0}; - unsigned long pc_{0}; +int main() +{ + std::vector instructions; + std::string line; + while (std::getline(std::cin, line)) { + instructions.emplace_back(line); + } - unsigned long& reg(Reg r) { - switch (r) { - case Reg::a: return a_; - case Reg::b: return b_; - default: abort(); - } - } - - void execute(Instr const &instruction) { - switch (instruction.op_) { - case Op::hlf: - reg(instruction.reg_) /= 2; - pc_ += instruction.pc_add_; - break; - case Op::tpl: - reg(instruction.reg_) *= 3; - pc_ += instruction.pc_add_; - break; - case Op::inc: - reg(instruction.reg_) += 1; - pc_ += instruction.pc_add_; - break; - case Op::jmp: - pc_ += instruction.pc_add_; - break; - case Op::jie: - if (reg(instruction.reg_) % 2 == 0) pc_ += instruction.pc_add_; - else pc_ += 1; - break; - case Op::jio: - if (reg(instruction.reg_) == 1) pc_ += instruction.pc_add_; - else pc_ += 1; - break; - default: - abort(); - } - } -}; - -int main() { - std::vector instructions; - std::string line; - while (std::getline(std::cin, line)) { - instructions.emplace_back(line); - } - - State state; - while (state.pc_ < instructions.size()) { - state.execute(instructions[state.pc_]); - } - std::cout << "a = " << state.a_ << "\nb = " << state.b_ << '\n'; + State state; + while (state.pc_ < instructions.size()) { + state.execute(instructions[state.pc_]); + } + std::cout << "a = " << state.a_ << "\nb = " << state.b_ << '\n'; } \ No newline at end of file diff --git a/2015/puzzle-24-01.cc b/2015/puzzle-24-01.cc index ad6fbb6..cc64b1f 100644 --- a/2015/puzzle-24-01.cc +++ b/2015/puzzle-24-01.cc @@ -1,78 +1,87 @@ -#include -#include -#include -#include -#include #include +#include +#include +#include #include #include #include +#include +#include -struct State { - std::set weights_; +struct State +{ + std::set weights_; - explicit State(unsigned long w) { weights_.insert(w); } + explicit State(unsigned long w) { weights_.insert(w); } - std::size_t cost() const { return weights_.size(); } + std::size_t cost() const { return weights_.size(); } - unsigned long qe() const { - return std::accumulate(weights_.begin(), weights_.end(), 1UL, - [](unsigned long l, unsigned long w) { return l * w; }); + unsigned long qe() const + { + return std::accumulate(weights_.begin(), weights_.end(), 1UL, + [](unsigned long l, unsigned long w) { return l * w; }); + } + + unsigned long weight() const + { + return std::accumulate(weights_.begin(), weights_.end(), 0UL, + [](unsigned long l, unsigned long w) { return l + w; }); + } + + bool operator==(State const& rhs) const { return weights_ == rhs.weights_; } + + bool operator<(State const& rhs) const + { + if (cost() < rhs.cost()) { + return true; } - - unsigned long weight() const { - return std::accumulate(weights_.begin(), weights_.end(), 0UL, - [](unsigned long l, unsigned long w) { return l + w; }); - } - - bool operator==(State const &rhs) const { return weights_ == rhs.weights_; } - - bool operator<(State const &rhs) const { - if (cost() < rhs.cost()) { return true; } - if (cost() == rhs.cost()) { return qe() < rhs.qe(); } - return false; + if (cost() == rhs.cost()) { + return qe() < rhs.qe(); } + return false; + } }; -int main() { - std::vector weights; - std::string line; - while (std::getline(std::cin, line)) { - weights.emplace_back(std::stoul(line)); +int main() +{ + std::vector weights; + std::string line; + while (std::getline(std::cin, line)) { + weights.emplace_back(std::stoul(line)); + } + + std::set states; + unsigned long target_weight; + for (auto w : weights) { + states.insert(State{w}); + target_weight += w; + } + target_weight /= 3; + + while (!states.empty()) { + auto it = states.begin(); + State current(*it); + states.erase(it); + + if (current.weight() == target_weight) { + std::cout << "Cost " << current.cost() << ", qe " << current.qe() << '\n'; + return 0; + } + if (current.weight() > target_weight) { + continue; } - std::set states; - unsigned long target_weight; - for (auto w: weights) { - states.insert(State{w}); - target_weight += w; + for (auto w : weights) { + if (current.weights_.find(w) == current.weights_.end()) { + auto next{current}; + next.weights_.insert(w); + if (states.find(next) == states.end()) { + states.insert(next); + } + } } - target_weight /= 3; + } - while (!states.empty()) { - auto it = states.begin(); - State current(*it); - states.erase(it); - - if (current.weight() == target_weight) { - std::cout << "Cost " << current.cost() << ", qe " << current.qe() << '\n'; - return 0; - } - if (current.weight() > target_weight) { - continue; - } - - for (auto w: weights) { - if (current.weights_.find(w) == current.weights_.end()) { - auto next{current}; - next.weights_.insert(w); - if (states.find(next) == states.end()) { - states.insert(next); - } - } - } - } - - std::cout << "It went wrong somewhere!\n"; - return 1; + std::cout << "It went wrong somewhere!\n"; + return 1; } \ No newline at end of file diff --git a/2015/puzzle-24-02.cc b/2015/puzzle-24-02.cc index 86248ff..3750d78 100644 --- a/2015/puzzle-24-02.cc +++ b/2015/puzzle-24-02.cc @@ -1,78 +1,87 @@ -#include -#include -#include -#include -#include #include +#include +#include +#include #include #include #include +#include +#include -struct State { - std::set weights_; +struct State +{ + std::set weights_; - explicit State(unsigned long w) { weights_.insert(w); } + explicit State(unsigned long w) { weights_.insert(w); } - std::size_t cost() const { return weights_.size(); } + std::size_t cost() const { return weights_.size(); } - unsigned long qe() const { - return std::accumulate(weights_.begin(), weights_.end(), 1UL, - [](unsigned long l, unsigned long w) { return l * w; }); + unsigned long qe() const + { + return std::accumulate(weights_.begin(), weights_.end(), 1UL, + [](unsigned long l, unsigned long w) { return l * w; }); + } + + unsigned long weight() const + { + return std::accumulate(weights_.begin(), weights_.end(), 0UL, + [](unsigned long l, unsigned long w) { return l + w; }); + } + + bool operator==(State const& rhs) const { return weights_ == rhs.weights_; } + + bool operator<(State const& rhs) const + { + if (cost() < rhs.cost()) { + return true; } - - unsigned long weight() const { - return std::accumulate(weights_.begin(), weights_.end(), 0UL, - [](unsigned long l, unsigned long w) { return l + w; }); - } - - bool operator==(State const &rhs) const { return weights_ == rhs.weights_; } - - bool operator<(State const &rhs) const { - if (cost() < rhs.cost()) { return true; } - if (cost() == rhs.cost()) { return qe() < rhs.qe(); } - return false; + if (cost() == rhs.cost()) { + return qe() < rhs.qe(); } + return false; + } }; -int main() { - std::vector weights; - std::string line; - while (std::getline(std::cin, line)) { - weights.emplace_back(std::stoul(line)); +int main() +{ + std::vector weights; + std::string line; + while (std::getline(std::cin, line)) { + weights.emplace_back(std::stoul(line)); + } + + std::set states; + unsigned long target_weight; + for (auto w : weights) { + states.insert(State{w}); + target_weight += w; + } + target_weight /= 4; + + while (!states.empty()) { + auto it = states.begin(); + State current(*it); + states.erase(it); + + if (current.weight() == target_weight) { + std::cout << "Cost " << current.cost() << ", qe " << current.qe() << '\n'; + return 0; + } + if (current.weight() > target_weight) { + continue; } - std::set states; - unsigned long target_weight; - for (auto w: weights) { - states.insert(State{w}); - target_weight += w; + for (auto w : weights) { + if (current.weights_.find(w) == current.weights_.end()) { + auto next{current}; + next.weights_.insert(w); + if (states.find(next) == states.end()) { + states.insert(next); + } + } } - target_weight /= 4; + } - while (!states.empty()) { - auto it = states.begin(); - State current(*it); - states.erase(it); - - if (current.weight() == target_weight) { - std::cout << "Cost " << current.cost() << ", qe " << current.qe() << '\n'; - return 0; - } - if (current.weight() > target_weight) { - continue; - } - - for (auto w: weights) { - if (current.weights_.find(w) == current.weights_.end()) { - auto next{current}; - next.weights_.insert(w); - if (states.find(next) == states.end()) { - states.insert(next); - } - } - } - } - - std::cout << "It went wrong somewhere!\n"; - return 1; + std::cout << "It went wrong somewhere!\n"; + return 1; } \ No newline at end of file diff --git a/2015/puzzle-25-01.cc b/2015/puzzle-25-01.cc index 08d980a..8fdde5f 100644 --- a/2015/puzzle-25-01.cc +++ b/2015/puzzle-25-01.cc @@ -1,49 +1,47 @@ #include -#include #include +#include -std::uint64_t lcg(std::uint64_t val) -{ - return (val * 252533) % 33554393; -} +std::uint64_t lcg(std::uint64_t val) { return (val * 252533) % 33554393; } int main() { - std::string line; - std::getline(std::cin, line); + std::string line; + std::getline(std::cin, line); - using namespace std::string_literals; - auto line_begin{"To continue, please consult the code grid in the manual. Enter the code at row "s}; - auto line_middle{", column "s}; - auto line_end{"."s}; + using namespace std::string_literals; + auto line_begin{ + "To continue, please consult the code grid in the manual. Enter the code at row "s}; + auto line_middle{", column "s}; + auto line_end{"."s}; - assert(line.substr(0, line_begin.size()) == line_begin); - line = line.substr(line_begin.size()); - std::size_t idx = 0; - unsigned long target_row = std::stoul(line, &idx); - line = line.substr(idx); - assert(line.substr(0, line_middle.size()) == line_middle); - line = line.substr(line_middle.size()); - unsigned long target_column = std::stoul(line, &idx); - line = line.substr(idx); - assert(line == line_end); + assert(line.substr(0, line_begin.size()) == line_begin); + line = line.substr(line_begin.size()); + std::size_t idx = 0; + unsigned long target_row = std::stoul(line, &idx); + line = line.substr(idx); + assert(line.substr(0, line_middle.size()) == line_middle); + line = line.substr(line_middle.size()); + unsigned long target_column = std::stoul(line, &idx); + line = line.substr(idx); + assert(line == line_end); - unsigned long row{1}; - unsigned long column{1}; - std::uint64_t num{20151125}; - while (row != target_row || column != target_column) { - if (row == 1) { - row = column + 1; - column = 1; - } - else { - --row; - ++column; - } - num = lcg(num); + unsigned long row{1}; + unsigned long column{1}; + std::uint64_t num{20151125}; + while (row != target_row || column != target_column) { + if (row == 1) { + row = column + 1; + column = 1; } + else { + --row; + ++column; + } + num = lcg(num); + } - std::cout << "Target row " << target_row << " column " << target_column << '\n'; - std::cout << "Row " << row << " column " << column << " num " << num << '\n'; - return 0; + std::cout << "Target row " << target_row << " column " << target_column << '\n'; + std::cout << "Row " << row << " column " << column << " num " << num << '\n'; + return 0; } \ No newline at end of file diff --git a/2020/puzzle-01-01.cc b/2020/puzzle-01-01.cc index 0fbba19..84e13f9 100644 --- a/2020/puzzle-01-01.cc +++ b/2020/puzzle-01-01.cc @@ -2,7 +2,8 @@ #include #include -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ std::vector vals; for (std::string line; std::getline(std::cin, line);) { int val = std::stoi(line, nullptr, 10); diff --git a/2020/puzzle-01-02.cc b/2020/puzzle-01-02.cc index 2698640..6ac4ccd 100644 --- a/2020/puzzle-01-02.cc +++ b/2020/puzzle-01-02.cc @@ -2,7 +2,8 @@ #include #include -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ std::vector vals; for (std::string line; std::getline(std::cin, line);) { int val = std::stoi(line, nullptr, 10); @@ -12,8 +13,8 @@ int main(int argc, char **argv) { for (std::size_t j = 0; j < i; ++j) { int64_t v2 = vals[j]; if (v1 + v2 + val == 2020) { - std::cout << v1 << " * " << v2 << " * " << val << " = " - << v1 * v2 * std::int64_t(val) << "\n"; + std::cout << v1 << " * " << v2 << " * " << val << " = " << v1 * v2 * std::int64_t(val) + << "\n"; } } } diff --git a/2020/puzzle-02-01.cc b/2020/puzzle-02-01.cc index 4a15b28..e12137e 100644 --- a/2020/puzzle-02-01.cc +++ b/2020/puzzle-02-01.cc @@ -3,8 +3,10 @@ #include #include -struct PasswordChecker { - PasswordChecker(std::string const &s) { +struct PasswordChecker +{ + PasswordChecker(std::string const& s) + { std::size_t pos = 0; min_ = std::stoul(s, &pos, 10); assert(s[pos] == '-'); @@ -17,7 +19,8 @@ struct PasswordChecker { password_ = s2.substr(pos + 4); } - bool is_valid() const { + bool is_valid() const + { std::string::size_type count = 0; for (auto c : password_) { if (c == c_) { @@ -34,13 +37,15 @@ struct PasswordChecker { std::string password_; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ unsigned count = 0; for (std::string line; std::getline(std::cin, line);) { PasswordChecker check(line); if (check.is_valid()) { ++count; - } else { + } + else { std::cout << "IN"; } std::cout << "VALID: " << line << "\n"; diff --git a/2020/puzzle-02-02.cc b/2020/puzzle-02-02.cc index 79d8416..1ac700a 100644 --- a/2020/puzzle-02-02.cc +++ b/2020/puzzle-02-02.cc @@ -3,8 +3,10 @@ #include #include -struct PasswordChecker { - PasswordChecker(std::string const &s) { +struct PasswordChecker +{ + PasswordChecker(std::string const& s) + { std::size_t pos = 0; pos1_ = std::stoul(s, &pos, 10); assert(s[pos] == '-'); @@ -17,7 +19,8 @@ struct PasswordChecker { password_ = s2.substr(pos + 4); } - bool is_valid() const { + bool is_valid() const + { std::string::size_type count = 0; if (password_.at(pos1_ - 1) == c_) { ++count; @@ -28,19 +31,21 @@ struct PasswordChecker { return count == 1; } - std::string::size_type pos1_; // 1 based - std::string::size_type pos2_; // 1 based + std::string::size_type pos1_; // 1 based + std::string::size_type pos2_; // 1 based std::string::value_type c_; std::string password_; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ unsigned count = 0; for (std::string line; std::getline(std::cin, line);) { PasswordChecker check(line); if (check.is_valid()) { ++count; - } else { + } + else { std::cout << "IN"; } std::cout << "VALID: " << line << "\n"; diff --git a/2020/puzzle-03-01.cc b/2020/puzzle-03-01.cc index 36b02cd..d6c808a 100644 --- a/2020/puzzle-03-01.cc +++ b/2020/puzzle-03-01.cc @@ -3,12 +3,14 @@ #include #include -std::string rotate(std::string const &s, std::string::size_type amount) { +std::string rotate(std::string const& s, std::string::size_type amount) +{ auto length = s.length(); return (s + s).substr(amount % length, length); } -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ unsigned count = 0; std::string::size_type rotation = 0; for (std::string line; std::getline(std::cin, line);) { diff --git a/2020/puzzle-03-02.cc b/2020/puzzle-03-02.cc index a3e2148..76ca09c 100644 --- a/2020/puzzle-03-02.cc +++ b/2020/puzzle-03-02.cc @@ -3,12 +3,14 @@ #include #include -std::string rotate(std::string const &s, std::string::size_type amount) { +std::string rotate(std::string const& s, std::string::size_type amount) +{ auto length = s.length(); return (s + s).substr(amount % length, length); } -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ if (argc != 3) { std::cerr << "Usage: " << argv[0] << " \n"; return 1; diff --git a/2020/puzzle-04-01.cc b/2020/puzzle-04-01.cc index 3ac1688..910518e 100644 --- a/2020/puzzle-04-01.cc +++ b/2020/puzzle-04-01.cc @@ -6,7 +6,8 @@ using StringSet = std::set; namespace { -StringSet parse_line(std::string const &str) { +StringSet parse_line(std::string const& str) +{ StringSet result; std::string::size_type pos = 0; @@ -28,20 +29,21 @@ StringSet parse_line(std::string const &str) { const StringSet mandatory{"byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"}; const StringSet optional{"cid"}; -bool is_valid_passport(StringSet const &mandatory_found) { +bool is_valid_passport(StringSet const& mandatory_found) +{ return mandatory_found.size() == mandatory.size(); } -} // namespace - -int main(int argc, char **argv) { +} // namespace +int main(int argc, char** argv) +{ StringSet mandatory_found; StringSet optional_found; unsigned valid = 0; for (std::string line; std::getline(std::cin, line);) { if (!line.empty()) { auto keys = parse_line(line); - for (auto const &key : keys) { + for (auto const& key : keys) { std::cout << "Key " << key << " is "; if (mandatory.find(key) != mandatory.end()) { auto [it, success] = mandatory_found.insert(key); @@ -49,18 +51,21 @@ int main(int argc, char **argv) { if (!success) { std::cout << " (duplicate)"; } - } else if (optional.find(key) != optional.end()) { + } + else if (optional.find(key) != optional.end()) { auto [it, success] = optional_found.insert(key); std::cout << "optional"; if (!success) { std::cout << " (duplicate)"; } - } else { + } + else { std::cout << "unexpected"; } std::cout << "\n"; } - } else { + } + else { valid += is_valid_passport(mandatory_found); mandatory_found.clear(); optional_found.clear(); diff --git a/2020/puzzle-04-02.cc b/2020/puzzle-04-02.cc index 08cad89..6ffc46b 100644 --- a/2020/puzzle-04-02.cc +++ b/2020/puzzle-04-02.cc @@ -8,7 +8,7 @@ // Various types that are useful using StringSet = std::set; using StringMap = std::map; -using ValidateFn = std::function; +using ValidateFn = std::function; using ValidateMap = std::map; namespace { @@ -19,7 +19,8 @@ namespace { * \a str is expected to look like a space separated list of pairs where each * pair is of the form 'key:value'. */ -StringMap parse_line(std::string const &str) { +StringMap parse_line(std::string const& str) +{ StringMap result; std::string::size_type pos = 0; @@ -32,9 +33,8 @@ StringMap parse_line(std::string const &str) { assert(colon_pos != std::string::npos); assert(colon_pos < end_pos); auto key_len = colon_pos - pos; - auto value_len = (end_pos == std::string::npos) - ? std::string::npos - : (end_pos - (colon_pos + 1)); + auto value_len = + (end_pos == std::string::npos) ? std::string::npos : (end_pos - (colon_pos + 1)); auto key = str.substr(pos, key_len); auto value = str.substr(colon_pos + 1, value_len); result.insert({key, value}); @@ -50,7 +50,8 @@ StringMap parse_line(std::string const &str) { * \param max Maximum year to accept * \return True if \a s is in range [min, max] */ -bool validate_year(std::string const &s, unsigned min, unsigned max) { +bool validate_year(std::string const& s, unsigned min, unsigned max) +{ if (s.length() != 4) { return false; } @@ -63,16 +64,17 @@ bool validate_year(std::string const &s, unsigned min, unsigned max) { } /// Validate byr field -bool validate_byr(std::string const &s) { return validate_year(s, 1920, 2002); } +bool validate_byr(std::string const& s) { return validate_year(s, 1920, 2002); } /// Validate iyr field -bool validate_iyr(std::string const &s) { return validate_year(s, 2010, 2020); } +bool validate_iyr(std::string const& s) { return validate_year(s, 2010, 2020); } /// Validate eyr field -bool validate_eyr(std::string const &s) { return validate_year(s, 2020, 2030); } +bool validate_eyr(std::string const& s) { return validate_year(s, 2020, 2030); } /// Validate hgt field -bool validate_hgt(std::string const &s) { +bool validate_hgt(std::string const& s) +{ std::size_t pos = 0; auto hgt = std::stoul(s, &pos, 10); if (pos != s.length() - 2) { @@ -80,7 +82,8 @@ bool validate_hgt(std::string const &s) { } if (s[pos] == 'c' && s[pos + 1] == 'm') { return hgt >= 150 && hgt <= 193; - } else if (s[pos] == 'i' && s[pos + 1] == 'n') { + } + else if (s[pos] == 'i' && s[pos + 1] == 'n') { return hgt >= 59 && hgt <= 76; } return false; @@ -92,8 +95,8 @@ bool validate_hgt(std::string const &s) { * \param cs Valid characters * \return True iff we pass validation */ -bool validate_chars(std::string const &s, std::string::size_type len, - std::string const &cs) { +bool validate_chars(std::string const& s, std::string::size_type len, std::string const& cs) +{ if (s.length() != len) { return false; } @@ -107,7 +110,8 @@ bool validate_chars(std::string const &s, std::string::size_type len, } /// Validate hcl field -bool validate_hcl(std::string const &s) { +bool validate_hcl(std::string const& s) +{ if (s.length() != 7) { return false; } @@ -118,46 +122,43 @@ bool validate_hcl(std::string const &s) { } /// Validate ecl field -bool validate_ecl(std::string const &s) { - static const StringSet valid = {"amb", "blu", "brn", "gry", - "grn", "hzl", "oth"}; +bool validate_ecl(std::string const& s) +{ + static const StringSet valid = {"amb", "blu", "brn", "gry", "grn", "hzl", "oth"}; return valid.find(s) != valid.end(); } /// Validate pid field -bool validate_pid(std::string const &s) { - return validate_chars(s, 9, "0123456789"); -} +bool validate_pid(std::string const& s) { return validate_chars(s, 9, "0123456789"); } /// Validate cid field -bool validate_cid(std::string const &s) { return true; } +bool validate_cid(std::string const& s) { return true; } /// Check if a passport is valid /// /// A passport is valid if it contains all mandatory fields, and passes /// validation on all fields. -bool is_valid_passport(StringMap const &found) { - static const StringSet mandatory{"byr", "iyr", "eyr", "hgt", - "hcl", "ecl", "pid"}; +bool is_valid_passport(StringMap const& found) +{ + static const StringSet mandatory{"byr", "iyr", "eyr", "hgt", "hcl", "ecl", "pid"}; static const ValidateMap validators{ - {"byr", validate_byr}, {"iyr", validate_iyr}, {"eyr", validate_eyr}, - {"hgt", validate_hgt}, {"hcl", validate_hcl}, {"ecl", validate_ecl}, - {"pid", validate_pid}, {"cid", validate_cid}, + {"byr", validate_byr}, {"iyr", validate_iyr}, {"eyr", validate_eyr}, {"hgt", validate_hgt}, + {"hcl", validate_hcl}, {"ecl", validate_ecl}, {"pid", validate_pid}, {"cid", validate_cid}, }; unsigned mandatory_found = 0; - for (auto const &kv : found) { + for (auto const& kv : found) { if (mandatory.find(kv.first) != mandatory.end()) { ++mandatory_found; } auto validator = validators.find(kv.first); if (validator == validators.end()) { std::cout << "Found invalid key: " << kv.first << "\n"; - } else { + } + else { auto valid = validator->second(kv.second); if (!valid) { - std::cout << "Invalid value for key: " << kv.first << ": " << kv.second - << "\n"; + std::cout << "Invalid value for key: " << kv.first << ": " << kv.second << "\n"; return false; } } @@ -165,17 +166,18 @@ bool is_valid_passport(StringMap const &found) { return mandatory_found == mandatory.size(); } -} // namespace - -int main(int argc, char **argv) { +} // namespace +int main(int argc, char** argv) +{ StringMap found; unsigned valid = 0; for (std::string line; std::getline(std::cin, line);) { if (!line.empty()) { auto keys = parse_line(line); found.insert(keys.begin(), keys.end()); - } else { + } + else { valid += is_valid_passport(found); found.clear(); } diff --git a/2020/puzzle-05-01.cc b/2020/puzzle-05-01.cc index 14a32fb..d93c693 100644 --- a/2020/puzzle-05-01.cc +++ b/2020/puzzle-05-01.cc @@ -9,8 +9,9 @@ * \param s String to parse, must be L2 characters long * \return Position described by s. */ -template -unsigned find_pos(std::string const &s) { +template +unsigned find_pos(std::string const& s) +{ assert(s.length() == L2); unsigned low = 0; unsigned high = 1 << L2; @@ -20,9 +21,11 @@ unsigned find_pos(std::string const &s) { assert(width != 0); if (c == LOWER) { high = low + width; - } else if (c == UPPER) { + } + else if (c == UPPER) { low = low + width; - } else { + } + else { assert(false); } width >>= 1; @@ -32,13 +35,15 @@ unsigned find_pos(std::string const &s) { return low; } -template struct Seat { - Seat(std::string const &s) { +template +struct Seat +{ + Seat(std::string const& s) + { assert(s.length() == L2R + L2C); row_ = find_pos(s.substr(0, L2R)); col_ = find_pos(s.substr(L2R, L2C)); - std::cout << s << ": row " << row_ << ", column " << col_ << ", seat ID " - << id() << ".\n"; + std::cout << s << ": row " << row_ << ", column " << col_ << ", seat ID " << id() << ".\n"; } unsigned id() const noexcept { return row_ * (1 << L2C) + col_; } @@ -47,7 +52,8 @@ template struct Seat { unsigned col_; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ unsigned max_id = 0; for (std::string line; std::getline(std::cin, line);) { diff --git a/2020/puzzle-05-02.cc b/2020/puzzle-05-02.cc index 5f12954..0d62550 100644 --- a/2020/puzzle-05-02.cc +++ b/2020/puzzle-05-02.cc @@ -15,8 +15,9 @@ * We don't need to keep a record of high at all so the original solution * was over enthusiastic. */ -template -unsigned find_pos(std::string const &s) { +template +unsigned find_pos(std::string const& s) +{ assert(s.length() == L2); unsigned low = 0; unsigned width = 1 << (L2 - 1); @@ -25,7 +26,8 @@ unsigned find_pos(std::string const &s) { assert(width != 0); if (c == UPPER) { low = low + width; - } else { + } + else { assert(c == LOWER); } @@ -40,8 +42,9 @@ unsigned find_pos(std::string const &s) { * \tparam L2R log 2 number of rows * \tparam L2C log 2 number of columns */ -template struct Seat { - +template +struct Seat +{ /** \brief Construct the seat. * \param s String representing seat location, must be L2R + L2C chars. * @@ -52,19 +55,19 @@ template struct Seat { * * Should have used initialiser lists. */ - Seat(std::string const &s) + Seat(std::string const& s) : row_(find_pos(s.substr(0, L2R))), - col_(find_pos(s.substr(L2R, L2C))) { + col_(find_pos(s.substr(L2R, L2C))) + { assert(s.length() == L2R + L2C); - std::cout << s << ": row " << row_ << ", column " << col_ << ", seat ID " - << id() << ".\n"; + std::cout << s << ": row " << row_ << ", column " << col_ << ", seat ID " << id() << ".\n"; } /** Get seat ID. */ unsigned id() const noexcept { return row_ * (1 << L2C) + col_; } - unsigned row_; ///< Seat row - unsigned col_; ///< Seat column + unsigned row_; ///< Seat row + unsigned col_; ///< Seat column }; /** \brief main @@ -73,11 +76,12 @@ template struct Seat { * * Could have initialised the array better. */ -int main(int argc, char **argv) { - constexpr unsigned l2r = 7; ///< Log 2 number of rows - constexpr unsigned l2c = 3; ///< Log 2 number of columns - constexpr unsigned max_id = 1 << (l2r + l2c); ///< Max ID. - std::array id_present = {}; ///< Is the ID present? +int main(int argc, char** argv) +{ + constexpr unsigned l2r = 7; ///< Log 2 number of rows + constexpr unsigned l2c = 3; ///< Log 2 number of columns + constexpr unsigned max_id = 1 << (l2r + l2c); ///< Max ID. + std::array id_present = {}; ///< Is the ID present? // Read the lines in marking IDs that are present for (std::string line; std::getline(std::cin, line);) { diff --git a/2020/puzzle-06-01.cc b/2020/puzzle-06-01.cc index d4c4ee5..ac09699 100644 --- a/2020/puzzle-06-01.cc +++ b/2020/puzzle-06-01.cc @@ -5,7 +5,8 @@ // Merge two strings of answers, preserving order and ensuring uniqueness of // each answer. -std::string merge_answers(std::string const &a, std::string const &b) { +std::string merge_answers(std::string const& a, std::string const& b) +{ std::string result; std::string::size_type pos_a = 0; std::string::size_type pos_b = 0; @@ -13,10 +14,12 @@ std::string merge_answers(std::string const &a, std::string const &b) { if (a[pos_a] < b[pos_b]) { result += a[pos_a]; ++pos_a; - } else if (b[pos_b] < a[pos_a]) { + } + else if (b[pos_b] < a[pos_a]) { result += b[pos_b]; ++pos_b; - } else { + } + else { assert(a[pos_a] == b[pos_b]); result += a[pos_a]; ++pos_a; @@ -29,23 +32,22 @@ std::string merge_answers(std::string const &a, std::string const &b) { return result; } -int main(int argc, char **argv) { - +int main(int argc, char** argv) +{ std::string current_answers; unsigned count = 0; for (std::string line; std::getline(std::cin, line);) { if (!line.empty()) { std::sort(line.begin(), line.end()); current_answers = merge_answers(current_answers, line); - } else { - std::cout << "Length of " << current_answers << " = " - << current_answers.length() << "\n"; + } + else { + std::cout << "Length of " << current_answers << " = " << current_answers.length() << "\n"; count += current_answers.length(); current_answers.clear(); } } - std::cout << "Length of " << current_answers << " = " - << current_answers.length() << "\n"; + std::cout << "Length of " << current_answers << " = " << current_answers.length() << "\n"; count += current_answers.length(); std::cout << "Total length = " << count << "\n"; diff --git a/2020/puzzle-06-02.cc b/2020/puzzle-06-02.cc index 0c279be..7016e26 100644 --- a/2020/puzzle-06-02.cc +++ b/2020/puzzle-06-02.cc @@ -6,8 +6,10 @@ using OccuranceMap = std::map; -struct GroupAnswers { - void add_answers(std::string const &a) { +struct GroupAnswers +{ + void add_answers(std::string const& a) + { // Increase group size ++group_size_; @@ -16,14 +18,16 @@ struct GroupAnswers { auto it = map_.find(c); if (it == map_.end()) { map_.insert({c, 1}); - } else { + } + else { ++(it->second); } } } // Count the number of answered questions answered by everyone. - unsigned all_answer_count() const noexcept { + unsigned all_answer_count() const noexcept + { unsigned count = 0; for (auto kv : map_) { if (kv.second == group_size_) { @@ -34,7 +38,8 @@ struct GroupAnswers { return count; } - void clear() { + void clear() + { map_.clear(); group_size_ = 0; } @@ -43,13 +48,15 @@ struct GroupAnswers { unsigned group_size_ = 0; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ GroupAnswers answers; unsigned count = 0; for (std::string line; std::getline(std::cin, line);) { if (!line.empty()) { answers.add_answers(line); - } else { + } + else { count += answers.all_answer_count(); answers.clear(); } diff --git a/2020/puzzle-07-01.cc b/2020/puzzle-07-01.cc index f01aae6..5168862 100644 --- a/2020/puzzle-07-01.cc +++ b/2020/puzzle-07-01.cc @@ -15,10 +15,11 @@ using Weight = unsigned; using EdgeWeights = std::map; using NodeQueue = std::list; -std::ostream &operator<<(std::ostream &os, NodeQueue const &q) { +std::ostream& operator<<(std::ostream& os, NodeQueue const& q) +{ std::string p = ""; os << "["; - for (auto const &n : q) { + for (auto const& n : q) { os << p << n; p = ", "; } @@ -26,10 +27,12 @@ std::ostream &operator<<(std::ostream &os, NodeQueue const &q) { return os; } -struct BagGraph { +struct BagGraph +{ BagGraph() { nodes_.insert({"", false}); } - void add_edges(std::string const &s) { + void add_edges(std::string const& s) + { static const auto prefix_re = std::regex("([a-z ]+) bags? contain "); static const auto suffix_re = std::regex("(\\d)+ ([a-z ]+) bags?[,.]\\s*"); static const auto empty_str = std::string("no other bags."); @@ -49,7 +52,8 @@ struct BagGraph { Edge e = std::make_pair("", n2); edges_.insert({e, 0}); std::cout << " -> " << n2 << ": 0\n"; - } else { + } + else { std::smatch suffix_m; if (!std::regex_search(suffix, suffix_m, suffix_re)) { std::cout << "Failed to match: " << suffix << "\n"; @@ -67,7 +71,8 @@ struct BagGraph { } } - unsigned num_containers(Node const &n) { + unsigned num_containers(Node const& n) + { NodeQueue queue; // Initial population of queue: populate_queue(queue, n); @@ -84,7 +89,8 @@ struct BagGraph { populate_queue(queue, n1); ++visited; std::cout << "Updated queue: " << queue << "\n"; - } else { + } + else { std::cout << "Already visited\n"; } } @@ -93,8 +99,9 @@ struct BagGraph { } private: - void populate_queue(NodeQueue &queue, Node const &n) { - for (auto const &kv : edges_) { + void populate_queue(NodeQueue& queue, Node const& n) + { + for (auto const& kv : edges_) { if (kv.first.first == n && !nodes_[kv.first.second]) { queue.push_back(kv.first.second); } @@ -105,7 +112,8 @@ private: Nodes nodes_; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ BagGraph bg; for (std::string line; std::getline(std::cin, line);) { bg.add_edges(line); diff --git a/2020/puzzle-07-02.cc b/2020/puzzle-07-02.cc index f105b06..065446b 100644 --- a/2020/puzzle-07-02.cc +++ b/2020/puzzle-07-02.cc @@ -13,8 +13,10 @@ using Edge = std::pair; using Weight = unsigned; using EdgeWeights = std::map; -struct BagGraph { - void add_edges(std::string const &s) { +struct BagGraph +{ + void add_edges(std::string const& s) + { static const auto prefix_re = std::regex("([a-z ]+) bags? contain "); static const auto suffix_re = std::regex("(\\d)+ ([a-z ]+) bags?[,.]\\s*"); static const auto empty_str = std::string("no other bags."); @@ -31,7 +33,8 @@ struct BagGraph { if (suffix == empty_str) { std::cout << n1 << " -> none: \n"; suffix.clear(); - } else { + } + else { std::smatch suffix_m; if (!std::regex_search(suffix, suffix_m, suffix_re)) { std::cout << "Failed to match: " << suffix << "\n"; @@ -48,17 +51,19 @@ struct BagGraph { } } - Weight num_contained(Node const &n) { + Weight num_contained(Node const& n) + { // Get the number of bags including the node we've been asked for so the // result is 1 less than the internal result return num_contained1(n) - 1; } private: - Weight num_contained1(Node const &n) { + Weight num_contained1(Node const& n) + { Weight w = 0; bool contains_something = false; - for (auto const &kv : edges_) { + for (auto const& kv : edges_) { if (kv.first.first == n) { w += kv.second * num_contained1(kv.first.second); contains_something = true; @@ -70,7 +75,8 @@ private: EdgeWeights edges_; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ BagGraph bg; for (std::string line; std::getline(std::cin, line);) { bg.add_edges(line); diff --git a/2020/puzzle-08-01.cc b/2020/puzzle-08-01.cc index 7325511..a33077d 100644 --- a/2020/puzzle-08-01.cc +++ b/2020/puzzle-08-01.cc @@ -9,36 +9,43 @@ enum class Opcode { Acc, Jmp, Nop }; using Value = int; -struct Instruction { - Instruction(std::string const &s) - : op_(to_opcode(s.substr(0, 3))), v_(to_value(s.substr(4))) {} +struct Instruction +{ + Instruction(std::string const& s) : op_(to_opcode(s.substr(0, 3))), v_(to_value(s.substr(4))) {} Opcode opcode() const noexcept { return op_; } Value value() const noexcept { return v_; } private: - Opcode to_opcode(std::string const &s) { + Opcode to_opcode(std::string const& s) + { if (s == "acc") { return Opcode::Acc; - } else if (s == "jmp") { + } + else if (s == "jmp") { return Opcode::Jmp; - } else if (s == "nop") { + } + else if (s == "nop") { return Opcode::Nop; - } else { + } + else { assert(false); return Opcode::Nop; } } - Value to_value(std::string const &s) { + Value to_value(std::string const& s) + { int sign = 0; int v = 0; std::size_t pos = 0; if (s[0] == '+') { sign = 1; - } else if (s[0] == '-') { + } + else if (s[0] == '-') { sign = -1; - } else { + } + else { assert(false); } v = std::stoi(s.substr(1), &pos); @@ -50,7 +57,8 @@ private: Value v_; }; -std::ostream &operator<<(std::ostream &os, Instruction const &i) { +std::ostream& operator<<(std::ostream& os, Instruction const& i) +{ switch (i.opcode()) { case Opcode::Acc: os << "acc"; @@ -74,15 +82,18 @@ std::ostream &operator<<(std::ostream &os, Instruction const &i) { } using Instructions = std::vector; -struct VM { +struct VM +{ VM() : pc_(0), acc_(0) {} - void add_instruction(Instruction const &i) { + void add_instruction(Instruction const& i) + { std::cout << i << "\n"; instrs_.push_back(i); } - void execute() { + void execute() + { std::vector seen(instrs_.size(), false); while (!seen[pc_]) { assert(pc_ < instrs_.size()); @@ -96,7 +107,8 @@ struct VM { Value acc() const noexcept { return acc_; } private: - void execute(Instruction const &i) { + void execute(Instruction const& i) + { std::cout << pc_ << ": " << i; switch (i.opcode()) { case Opcode::Acc: @@ -120,7 +132,8 @@ private: Value acc_; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ VM vm; for (std::string line; std::getline(std::cin, line);) { vm.add_instruction(Instruction(line)); diff --git a/2020/puzzle-08-02.cc b/2020/puzzle-08-02.cc index 86aa921..101e088 100644 --- a/2020/puzzle-08-02.cc +++ b/2020/puzzle-08-02.cc @@ -9,13 +9,14 @@ enum class Opcode { Acc, Jmp, Nop }; using Value = int; -struct Instruction { - Instruction(std::string const &s) - : op_(to_opcode(s.substr(0, 3))), v_(to_value(s.substr(4))) {} +struct Instruction +{ + Instruction(std::string const& s) : op_(to_opcode(s.substr(0, 3))), v_(to_value(s.substr(4))) {} Opcode opcode() const noexcept { return op_; } Value value() const noexcept { return v_; } - bool flip_jmp() { + bool flip_jmp() + { switch (op_) { case Opcode::Acc: return false; @@ -31,28 +32,35 @@ struct Instruction { } private: - Opcode to_opcode(std::string const &s) { + Opcode to_opcode(std::string const& s) + { if (s == "acc") { return Opcode::Acc; - } else if (s == "jmp") { + } + else if (s == "jmp") { return Opcode::Jmp; - } else if (s == "nop") { + } + else if (s == "nop") { return Opcode::Nop; - } else { + } + else { assert(false); return Opcode::Nop; } } - Value to_value(std::string const &s) { + Value to_value(std::string const& s) + { int sign = 0; int v = 0; std::size_t pos = 0; if (s[0] == '+') { sign = 1; - } else if (s[0] == '-') { + } + else if (s[0] == '-') { sign = -1; - } else { + } + else { assert(false); } v = std::stoi(s.substr(1), &pos); @@ -64,7 +72,8 @@ private: Value v_; }; -std::ostream &operator<<(std::ostream &os, Instruction const &i) { +std::ostream& operator<<(std::ostream& os, Instruction const& i) +{ switch (i.opcode()) { case Opcode::Acc: os << "acc"; @@ -88,15 +97,18 @@ std::ostream &operator<<(std::ostream &os, Instruction const &i) { } using Instructions = std::vector; -struct VM { +struct VM +{ VM() : pc_(0), acc_(0) {} - void add_instruction(Instruction const &i) { + void add_instruction(Instruction const& i) + { std::cout << i << "\n"; instrs_.push_back(i); } - bool execute() { + bool execute() + { std::vector seen(instrs_.size(), false); acc_ = 0; pc_ = 0; @@ -114,12 +126,12 @@ struct VM { return false; } - void find_fix() { + void find_fix() + { for (std::size_t pos = 0; pos < instrs_.size(); ++pos) { if (instrs_[pos].flip_jmp()) { if (execute()) { - std::cout << "Success at instruction " << pos << ": " << instrs_[pos] - << "\n"; + std::cout << "Success at instruction " << pos << ": " << instrs_[pos] << "\n"; return; } instrs_[pos].flip_jmp(); @@ -130,7 +142,8 @@ struct VM { Value acc() const noexcept { return acc_; } private: - void execute(Instruction const &i) { + void execute(Instruction const& i) + { std::cout << pc_ << ": " << i; switch (i.opcode()) { case Opcode::Acc: @@ -154,7 +167,8 @@ private: Value acc_; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ VM vm; for (std::string line; std::getline(std::cin, line);) { vm.add_instruction(Instruction(line)); diff --git a/2020/puzzle-09-01.cc b/2020/puzzle-09-01.cc index 371dedf..71c0286 100644 --- a/2020/puzzle-09-01.cc +++ b/2020/puzzle-09-01.cc @@ -6,10 +6,13 @@ #include #include -template struct Buffer { +template +struct Buffer +{ Buffer() {} - bool add_value(std::string const &s) { + bool add_value(std::string const& s) + { unsigned long i = std::stoul(s); auto size = buf_.size(); if (size == L && !valid(i)) { @@ -23,7 +26,8 @@ template struct Buffer { } private: - bool valid(unsigned long v) const { + bool valid(unsigned long v) const + { assert(buf_.size() == L); for (auto i = buf_.begin(); i != buf_.end(); ++i) { auto j = i; @@ -41,7 +45,8 @@ private: std::list buf_; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ Buffer<25> buf; for (std::string line; std::getline(std::cin, line);) { if (!buf.add_value(line)) { diff --git a/2020/puzzle-09-02.cc b/2020/puzzle-09-02.cc index c00302a..c7bc75c 100644 --- a/2020/puzzle-09-02.cc +++ b/2020/puzzle-09-02.cc @@ -6,10 +6,13 @@ #include #include -template struct Buffer { +template +struct Buffer +{ Buffer() {} - bool add_value(std::string const &s) { + bool add_value(std::string const& s) + { unsigned long i = std::stoul(s); bool result = true; auto size = buf_.size(); @@ -20,7 +23,8 @@ template struct Buffer { return result; } - unsigned long get_weakness(unsigned long v) { + unsigned long get_weakness(unsigned long v) + { std::size_t sum_begin = 0; std::size_t sum_end = 0; unsigned long sum = 0; @@ -36,16 +40,15 @@ template struct Buffer { ++sum_begin; } } - std::cout << "sum_begin = " << sum_begin << " sum_end = " << sum_end - << "\n"; - std::vector vec(buf_.begin() + sum_begin, - buf_.begin() + sum_end); + std::cout << "sum_begin = " << sum_begin << " sum_end = " << sum_end << "\n"; + std::vector vec(buf_.begin() + sum_begin, buf_.begin() + sum_end); std::sort(vec.begin(), vec.end()); return vec[0] + vec[vec.size() - 1]; } private: - bool valid(unsigned long v) const { + bool valid(unsigned long v) const + { assert(buf_.size() >= L); for (auto i = buf_.size() - 25; i != buf_.size(); ++i) { for (auto j = i + 1; j != buf_.size(); ++j) { @@ -60,7 +63,8 @@ private: std::vector buf_; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ Buffer<25> buf; unsigned long invalid = 0; for (std::string line; std::getline(std::cin, line);) { diff --git a/2020/puzzle-10-01.cc b/2020/puzzle-10-01.cc index d9681f5..8eab519 100644 --- a/2020/puzzle-10-01.cc +++ b/2020/puzzle-10-01.cc @@ -9,7 +9,8 @@ using Jolt = unsigned long; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ std::vector jolts; for (std::string line; std::getline(std::cin, line);) { jolts.push_back(std::stoul(line)); @@ -26,7 +27,6 @@ int main(int argc, char **argv) { // and the gap to our voltage ++gaps[3]; - std::cout << gaps[1] << " * " << gaps[3] << " = " << gaps[1] * gaps[3] - << "\n"; + std::cout << gaps[1] << " * " << gaps[3] << " = " << gaps[1] * gaps[3] << "\n"; return 0; } \ No newline at end of file diff --git a/2020/puzzle-10-02.cc b/2020/puzzle-10-02.cc index 7a1db8b..0d59452 100644 --- a/2020/puzzle-10-02.cc +++ b/2020/puzzle-10-02.cc @@ -9,7 +9,8 @@ using Jolt = unsigned long; -Jolt count_valid_combinations(std::vector const &jolts) { +Jolt count_valid_combinations(std::vector const& jolts) +{ std::vector valid(jolts.size(), 0); valid[jolts.size() - 1] = 1; std::size_t i = jolts.size() - 1; @@ -27,7 +28,8 @@ Jolt count_valid_combinations(std::vector const &jolts) { return valid[0]; } -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ std::vector jolts; for (std::string line; std::getline(std::cin, line);) { jolts.push_back(std::stoul(line)); diff --git a/2020/puzzle-11-01.cc b/2020/puzzle-11-01.cc index be29d4c..41c7875 100644 --- a/2020/puzzle-11-01.cc +++ b/2020/puzzle-11-01.cc @@ -9,7 +9,8 @@ using Array = std::vector; -char next_state(Array const &a, std::size_t row, std::size_t col) { +char next_state(Array const& a, std::size_t row, std::size_t col) +{ unsigned occupied = 0; if (row > 0) { if (col > 0) { @@ -38,16 +39,19 @@ char next_state(Array const &a, std::size_t row, std::size_t col) { if (a[row][col] == 'L' && occupied == 0) { return '#'; - } else if (a[row][col] == '#' && occupied >= 4) { + } + else if (a[row][col] == '#' && occupied >= 4) { return 'L'; - } else { + } + else { return a[row][col]; } } -unsigned long count_occupied(Array const &a) { +unsigned long count_occupied(Array const& a) +{ unsigned long count = 0; - for (auto const &row : a) { + for (auto const& row : a) { for (auto col : row) { count += (col == '#'); } @@ -56,7 +60,8 @@ unsigned long count_occupied(Array const &a) { return count; } -unsigned long run_to_steady_state(Array const &a) { +unsigned long run_to_steady_state(Array const& a) +{ Array current = a; bool changed = true; unsigned i = 0; @@ -81,7 +86,8 @@ unsigned long run_to_steady_state(Array const &a) { return count_occupied(current); } -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ Array array; for (std::string line; std::getline(std::cin, line);) { diff --git a/2020/puzzle-11-02.cc b/2020/puzzle-11-02.cc index a60a795..1aaaaf3 100644 --- a/2020/puzzle-11-02.cc +++ b/2020/puzzle-11-02.cc @@ -9,8 +9,8 @@ using Array = std::vector; -unsigned search(Array const &a, std::size_t row, std::size_t col, int rd, - int cd) { +unsigned search(Array const& a, std::size_t row, std::size_t col, int rd, int cd) +{ int cr = (int)row; int cc = (int)col; while (true) { @@ -36,7 +36,8 @@ unsigned search(Array const &a, std::size_t row, std::size_t col, int rd, assert(false); } -char next_state(Array const &a, std::size_t row, std::size_t col) { +char next_state(Array const& a, std::size_t row, std::size_t col) +{ unsigned occupied = 0; for (int row_delta = -1; row_delta < 2; ++row_delta) { for (int col_delta = -1; col_delta < 2; ++col_delta) { @@ -46,16 +47,19 @@ char next_state(Array const &a, std::size_t row, std::size_t col) { if (a[row][col] == 'L' && occupied == 0) { return '#'; - } else if (a[row][col] == '#' && occupied >= 5) { + } + else if (a[row][col] == '#' && occupied >= 5) { return 'L'; - } else { + } + else { return a[row][col]; } } -unsigned long count_occupied(Array const &a) { +unsigned long count_occupied(Array const& a) +{ unsigned long count = 0; - for (auto const &row : a) { + for (auto const& row : a) { for (auto col : row) { count += (col == '#'); } @@ -64,7 +68,8 @@ unsigned long count_occupied(Array const &a) { return count; } -unsigned long run_to_steady_state(Array const &a) { +unsigned long run_to_steady_state(Array const& a) +{ Array current = a; bool changed = true; unsigned i = 0; @@ -89,7 +94,8 @@ unsigned long run_to_steady_state(Array const &a) { return count_occupied(current); } -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ Array array; for (std::string line; std::getline(std::cin, line);) { diff --git a/2020/puzzle-12-01.cc b/2020/puzzle-12-01.cc index 68cac96..5df9741 100644 --- a/2020/puzzle-12-01.cc +++ b/2020/puzzle-12-01.cc @@ -9,10 +9,12 @@ enum class Heading { North = 'N', East = 'E', South = 'S', West = 'W' }; -struct Position { +struct Position +{ Position() : x_(0), y_(0), head_(Heading::East) {} - void move(std::string const &s) { + void move(std::string const& s) + { char act = s[0]; int dist = std::stoi(s.substr(1)); @@ -39,7 +41,8 @@ struct Position { std::cout << s << ": (" << x_ << ", " << y_ << ")\n"; } - void move(int dist, Heading head) { + void move(int dist, Heading head) + { switch (head) { case Heading::North: y_ += dist; @@ -59,7 +62,8 @@ struct Position { } } - void rotate_left(int deg) { + void rotate_left(int deg) + { /* We want the rang [-180, 180]. */ while (deg < -180) { deg += 360; @@ -85,7 +89,8 @@ struct Position { default: assert(false); } - } else if (deg == 90) { + } + else if (deg == 90) { switch (head_) { case Heading::North: head_ = Heading::West; @@ -102,7 +107,8 @@ struct Position { default: assert(false); } - } else if (deg == 180 || deg == -180) { + } + else if (deg == 180 || deg == -180) { switch (head_) { case Heading::North: head_ = Heading::South; @@ -119,7 +125,8 @@ struct Position { default: assert(false); } - } else if (deg != 0) { + } + else if (deg != 0) { assert(false); } } @@ -131,7 +138,8 @@ struct Position { Heading head_; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ Position pos; for (std::string line; std::getline(std::cin, line);) { diff --git a/2020/puzzle-12-02.cc b/2020/puzzle-12-02.cc index db88d44..b81b9a5 100644 --- a/2020/puzzle-12-02.cc +++ b/2020/puzzle-12-02.cc @@ -9,10 +9,12 @@ enum class Heading { North = 'N', East = 'E', South = 'S', West = 'W' }; -struct Position { +struct Position +{ Position() : x_(0), y_(0), wx_(10), wy_(1), head_(Heading::East) {} - void move(std::string const &s) { + void move(std::string const& s) + { char act = s[0]; int dist = std::stoi(s.substr(1)); @@ -37,11 +39,12 @@ struct Position { assert(false); } - std::cout << s << ": Pos = (" << x_ << ", " << y_ << "), Way = (" << wx_ - << ", " << wy_ << ")\n"; + std::cout << s << ": Pos = (" << x_ << ", " << y_ << "), Way = (" << wx_ << ", " << wy_ + << ")\n"; } - void move_way(int dist, Heading head) { + void move_way(int dist, Heading head) + { switch (head) { case Heading::North: wy_ += dist; @@ -61,7 +64,8 @@ struct Position { } } - void rotate_left(int deg) { + void rotate_left(int deg) + { /* We want the rang [-180, 180]. */ while (deg < -180) { deg += 360; @@ -75,15 +79,18 @@ struct Position { int py = wy_; wx_ = py; wy_ = -px; - } else if (deg == 90) { + } + else if (deg == 90) { int px = wx_; int py = wy_; wx_ = -py; wy_ = px; - } else if (deg == 180 || deg == -180) { + } + else if (deg == 180 || deg == -180) { wx_ = -wx_; wy_ = -wy_; - } else if (deg != 0) { + } + else if (deg != 0) { assert(false); } } @@ -97,7 +104,8 @@ struct Position { Heading head_; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ Position pos; for (std::string line; std::getline(std::cin, line);) { diff --git a/2020/puzzle-13-01.cc b/2020/puzzle-13-01.cc index d5c5f01..9ed7369 100644 --- a/2020/puzzle-13-01.cc +++ b/2020/puzzle-13-01.cc @@ -10,7 +10,8 @@ using Time = unsigned long; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ Time arrival; Time closest = ULONG_MAX; Time closest_id = 0; @@ -36,8 +37,7 @@ int main(int argc, char **argv) { if (next_departure_in == depart) { next_departure_in = 0; } - std::cout << "Bus #" << depart - << " previous departure: " << (arrival / depart) * depart + std::cout << "Bus #" << depart << " previous departure: " << (arrival / depart) * depart << ", next departure in: " << next_departure_in << "\n"; if (next_departure_in < closest) { closest = next_departure_in; @@ -45,8 +45,7 @@ int main(int argc, char **argv) { } } - std::cout << "Next departure # " << closest_id << " in " << closest - << " minutes.\n"; + std::cout << "Next departure # " << closest_id << " in " << closest << " minutes.\n"; std::cout << "Result = " << closest * closest_id << "\n"; return 0; } \ No newline at end of file diff --git a/2020/puzzle-13-02.cc b/2020/puzzle-13-02.cc index 606dbde..916e702 100644 --- a/2020/puzzle-13-02.cc +++ b/2020/puzzle-13-02.cc @@ -12,9 +12,10 @@ using Time = unsigned long; using IDPair = std::pair; using IDVector = std::vector; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ std::string line; - std::getline(std::cin, line); // Ignore first line + std::getline(std::cin, line); // Ignore first line std::string buses; std::getline(std::cin, buses); @@ -26,7 +27,8 @@ int main(int argc, char **argv) { if (buses[pos] == ',') { ++pos; continue; - } else if (buses[pos] == 'x') { + } + else if (buses[pos] == 'x') { ++offset; ++pos; continue; @@ -43,12 +45,11 @@ int main(int argc, char **argv) { // Make assumption that all bus IDs are co-prime. Time t = 0; Time t_incr = 1; - for (auto const &bus : ids) { + for (auto const& bus : ids) { while ((t + bus.second) % bus.first != 0) { t += t_incr; } - std::cout << "Bus #" << bus.first << " at offset " << bus.second - << " t = " << t << "\n"; + std::cout << "Bus #" << bus.first << " at offset " << bus.second << " t = " << t << "\n"; t_incr *= bus.first; } return 0; diff --git a/2020/puzzle-14-01.cc b/2020/puzzle-14-01.cc index da83414..22d20f7 100644 --- a/2020/puzzle-14-01.cc +++ b/2020/puzzle-14-01.cc @@ -10,10 +10,12 @@ using Data = std::uint64_t; using Mem = std::vector; -struct Computer { +struct Computer +{ Computer() : set_(0), clear_(0), mem_() {} - void add_instruction(std::string const &s) { + void add_instruction(std::string const& s) + { static const std::regex re("mem\\[(\\d+)\\] = (\\d+)"); std::smatch m; @@ -28,11 +30,12 @@ struct Computer { } std::cout << "mask set: " << std::hex << set_ << "\n"; std::cout << "mask clear: " << std::hex << clear_ << "\n"; - } else if (std::regex_search(s, m, re)) { + } + else if (std::regex_search(s, m, re)) { Data loc = std::stoul(m.str(1)); Data value = std::stoul(m.str(2)); - std::cout << "mem[" << std::dec << loc << " = (" << value << " | " << set_ - << ") & " << clear_ << " = "; + std::cout << "mem[" << std::dec << loc << " = (" << value << " | " << set_ << ") & " << clear_ + << " = "; value = (value | set_) & clear_; std::cout << value << "\n"; if (mem_.size() <= loc) { @@ -42,7 +45,8 @@ struct Computer { } } - Data mem_sum() const noexcept { + Data mem_sum() const noexcept + { Data r = 0; for (auto v : mem_) { r += v; @@ -55,7 +59,8 @@ struct Computer { Mem mem_; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ Computer c; std::string line; diff --git a/2020/puzzle-14-02.cc b/2020/puzzle-14-02.cc index 5c2b646..9cbbc8e 100644 --- a/2020/puzzle-14-02.cc +++ b/2020/puzzle-14-02.cc @@ -10,9 +10,10 @@ using Data = std::uint64_t; using Mem = std::map; -struct Computer { - - void add_instruction(std::string const &s) { +struct Computer +{ + void add_instruction(std::string const& s) + { static const std::regex re("mem\\[(\\d+)\\] = (\\d+)"); std::smatch m; @@ -26,28 +27,29 @@ struct Computer { for (auto c : mask) { if (c == '1') { set_ |= Data(1) << bit; - - } else if (c == 'X') { + } + else if (c == 'X') { bits_.push_back(std::size_t(1) << bit); std::cout << bit << " (" << (std::size_t(1) << bit) << ") "; - - } else { + } + else { assert(c == '0'); } ++bit; } assert(bit == mask.size()); std::cout << " set: 0x" << std::hex << set_ << std::dec << "\n"; - } else if (std::regex_search(s, m, re)) { + } + else if (std::regex_search(s, m, re)) { Data loc = std::stoul(m.str(1)); Data value = std::stoul(m.str(2)); set_mem(loc, value); } } - void set_mem(Data loc, Data value) { - std::cout << "Writing: " << value << " to base loc " << std::hex << loc - << " which floats to:"; + void set_mem(Data loc, Data value) + { + std::cout << "Writing: " << value << " to base loc " << std::hex << loc << " which floats to:"; for (auto b : bits_) { loc &= ~b; } @@ -67,7 +69,8 @@ struct Computer { std::cout << std::dec << "\n"; } - Data mem_sum() const noexcept { + Data mem_sum() const noexcept + { Data r = 0; for (auto v : mem_) { r += v.second; @@ -80,7 +83,8 @@ struct Computer { Mem mem_; }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ Computer c; std::string line; diff --git a/2020/puzzle-15-01.cc b/2020/puzzle-15-01.cc index e7df5a6..5a18e8c 100644 --- a/2020/puzzle-15-01.cc +++ b/2020/puzzle-15-01.cc @@ -9,12 +9,14 @@ using NumMap = std::map; -int add_to_map(NumMap &nums, int num, int turn) { +int add_to_map(NumMap& nums, int num, int turn) +{ auto [it, success] = nums.insert({num, turn}); if (success) { // std::cout << "Turn " << turn << ": " << num << " (new)\n"; return 0; - } else { + } + else { int r = turn - it->second; // std::cout << "Turn " << turn << ": " << num << " (previous seen turn " // << it->second << " dist = " << r << ")\n"; @@ -23,7 +25,8 @@ int add_to_map(NumMap &nums, int num, int turn) { } } -int run(std::string const &s, int num_turns) { +int run(std::string const& s, int num_turns) +{ NumMap seen; std::size_t pos = 0; std::size_t len; @@ -47,7 +50,8 @@ int run(std::string const &s, int num_turns) { return next_num; } -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ std::string line; while (std::getline(std::cin, line)) { int r = run(line, 2020); diff --git a/2020/puzzle-15-02.cc b/2020/puzzle-15-02.cc index cf6d799..bb761c1 100644 --- a/2020/puzzle-15-02.cc +++ b/2020/puzzle-15-02.cc @@ -10,12 +10,14 @@ using NumMap = std::unordered_map; -int add_to_map(NumMap &nums, int num, int turn) { +int add_to_map(NumMap& nums, int num, int turn) +{ auto [it, success] = nums.insert({num, turn}); if (success) { // std::cout << "Turn " << turn << ": " << num << " (new)\n"; return 0; - } else { + } + else { int r = turn - it->second; // std::cout << "Turn " << turn << ": " << num << " (previous seen turn " // << it->second << " dist = " << r << ")\n"; @@ -24,7 +26,8 @@ int add_to_map(NumMap &nums, int num, int turn) { } } -int run(std::string const &s, int num_turns) { +int run(std::string const& s, int num_turns) +{ NumMap seen; std::size_t pos = 0; std::size_t len; @@ -48,7 +51,8 @@ int run(std::string const &s, int num_turns) { return next_num; } -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ std::string line; while (std::getline(std::cin, line)) { int r = run(line, 30000000); diff --git a/2020/puzzle-16-01.cc b/2020/puzzle-16-01.cc index cd4d74f..57f849b 100644 --- a/2020/puzzle-16-01.cc +++ b/2020/puzzle-16-01.cc @@ -9,8 +9,10 @@ using ValidVector = std::vector; -struct Classifier { - void add_ranges(std::string const &s) { +struct Classifier +{ + void add_ranges(std::string const& s) + { static const std::regex re("(\\w+): (\\d+)-(\\d+) or (\\d+)-(\\d+)"); std::smatch m; if (std::regex_search(s, m, re)) { @@ -22,7 +24,8 @@ struct Classifier { } } - void add_range(std::string const &lows, std::string const &highs) { + void add_range(std::string const& lows, std::string const& highs) + { int low = std::stoi(lows); int high = std::stoi(highs); ++high; @@ -36,7 +39,8 @@ struct Classifier { } } - int count_errors(std::string const &s) { + int count_errors(std::string const& s) + { std::string::size_type pos = 0; int result = 0; while (pos < s.size()) { @@ -59,7 +63,8 @@ struct Classifier { enum class State { Permitted, Your, Nearby }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ std::string line; State s = State::Permitted; @@ -68,13 +73,17 @@ int main(int argc, char **argv) { while (std::getline(std::cin, line)) { if (line == "your ticket:") { s = State::Your; - } else if (line == "nearby tickets:") { + } + else if (line == "nearby tickets:") { s = State::Nearby; - } else if (line == "") { + } + else if (line == "") { /* nothing */; - } else if (s == State::Permitted) { + } + else if (s == State::Permitted) { c.add_ranges(line); - } else if (s == State::Nearby) { + } + else if (s == State::Nearby) { e += c.count_errors(line); } } diff --git a/2020/puzzle-16-02.cc b/2020/puzzle-16-02.cc index 6de01d3..64e5384 100644 --- a/2020/puzzle-16-02.cc +++ b/2020/puzzle-16-02.cc @@ -7,8 +7,10 @@ #include #include -struct Classifier { - void add_ranges(std::string const &s) { +struct Classifier +{ + void add_ranges(std::string const& s) + { static const std::regex re("(.+): (\\d+)-(\\d+) or (\\d+)-(\\d+)"); std::smatch m; if (std::regex_search(s, m, re)) { @@ -22,8 +24,8 @@ struct Classifier { } } - void add_range(std::string const &lows, std::string const &highs, - std::size_t field) { + void add_range(std::string const& lows, std::string const& highs, std::size_t field) + { int low = std::stoi(lows); int high = std::stoi(highs); ++high; @@ -39,13 +41,14 @@ struct Classifier { } } - void close() { + void close() + { idx_to_valid_field_.resize(0); - idx_to_valid_field_.resize(field_names_.size(), - (1 << field_names_.size()) - 1); + idx_to_valid_field_.resize(field_names_.size(), (1 << field_names_.size()) - 1); } - int count_errors(std::string const &s) const { + int count_errors(std::string const& s) const + { std::string::size_type pos = 0; int result = 0; while (pos < s.size()) { @@ -64,14 +67,14 @@ struct Classifier { return result; } - void mark_valid(std::string const &s) { + void mark_valid(std::string const& s) + { std::string::size_type pos = 0; unsigned idx = 0; while (pos < s.size()) { std::size_t len = 0; int num = std::stoi(s.substr(pos), &len); - unsigned valid_fields = - num >= num_to_valid_field_.size() ? 0 : num_to_valid_field_[num]; + unsigned valid_fields = num >= num_to_valid_field_.size() ? 0 : num_to_valid_field_[num]; idx_to_valid_field_[idx] &= valid_fields; pos += len; while (pos < s.size() && s[pos] == ',') { @@ -82,12 +85,12 @@ struct Classifier { } } - void print_valid() const { + void print_valid() const + { for (std::size_t idx = 0; idx < idx_to_valid_field_.size(); ++idx) { std::cout << "Index " << idx << " valid for fields:"; unsigned field = 0; - for (auto valid_fields = idx_to_valid_field_[idx]; valid_fields != 0; - valid_fields >>= 1) { + for (auto valid_fields = idx_to_valid_field_[idx]; valid_fields != 0; valid_fields >>= 1) { if (valid_fields & 1) { std::cout << " " << field_names_[field]; } @@ -97,15 +100,15 @@ struct Classifier { } } - void reduce() { + void reduce() + { bool changed = true; while (changed) { changed = false; for (unsigned idx = 0; idx < idx_to_valid_field_.size(); ++idx) { auto valid_fields = idx_to_valid_field_[idx]; if ((valid_fields & (valid_fields - 1)) == 0) { - std::cout << "Index " << idx << " can only be field " << valid_fields - << "\n"; + std::cout << "Index " << idx << " can only be field " << valid_fields << "\n"; for (unsigned idx2 = 0; idx2 < idx_to_valid_field_.size(); ++idx2) { if (idx == idx2) { continue; @@ -118,14 +121,15 @@ struct Classifier { } } - unsigned long calculate_product(std::string const &s, - std::string const &prefix) { + unsigned long calculate_product(std::string const& s, std::string const& prefix) + { std::vector values; std::size_t pos = 0; while (pos < s.size()) { if (s[pos] == ',') { ++pos; - } else { + } + else { std::size_t len; values.push_back(std::stoi(s.substr(pos), &len)); pos += len; @@ -136,8 +140,7 @@ struct Classifier { for (unsigned field = 0; field < field_names_.size(); ++field) { if (field_names_[field].substr(0, prefix.size()) == prefix) { unsigned idx = 0; - while (idx < idx_to_valid_field_.size() && - idx_to_valid_field_[idx] != (1 << field)) { + while (idx < idx_to_valid_field_.size() && idx_to_valid_field_[idx] != (1 << field)) { ++idx; } assert(idx < idx_to_valid_field_.size()); @@ -155,7 +158,8 @@ struct Classifier { enum class State { Permitted, Your, Nearby }; -int main(int argc, char **argv) { +int main(int argc, char** argv) +{ std::string line; State s = State::Permitted; @@ -165,16 +169,21 @@ int main(int argc, char **argv) { while (std::getline(std::cin, line)) { if (line == "your ticket:") { s = State::Your; - } else if (line == "nearby tickets:") { + } + else if (line == "nearby tickets:") { s = State::Nearby; c.close(); - } else if (line == "") { + } + else if (line == "") { /* nothing */; - } else if (s == State::Permitted) { + } + else if (s == State::Permitted) { c.add_ranges(line); - } else if (s == State::Your) { + } + else if (s == State::Your) { our_ticket = line; - } else if (s == State::Nearby) { + } + else if (s == State::Nearby) { auto e2 = c.count_errors(line); e += e2; if (e2 == 0) { @@ -187,7 +196,6 @@ int main(int argc, char **argv) { c.reduce(); c.print_valid(); - std::cout << "Product: " << c.calculate_product(our_ticket, "departure ") - << "\n"; + std::cout << "Product: " << c.calculate_product(our_ticket, "departure ") << "\n"; return 0; } \ No newline at end of file diff --git a/2020/puzzle-17-01.cc b/2020/puzzle-17-01.cc index 9d37076..37a00ce 100644 --- a/2020/puzzle-17-01.cc +++ b/2020/puzzle-17-01.cc @@ -9,13 +9,15 @@ #include #include -struct ConwayCubeState { +struct ConwayCubeState +{ using Point = std::tuple; using PointSet = std::set; using NeighbourMap = std::map; ConwayCubeState() : next_row_(0) {} - void add_row(std::string const &s) { + void add_row(std::string const& s) + { for (int x = 0; x < s.size(); ++x) { if (s[x] == '#') { set_point(x, next_row_, 0); @@ -24,13 +26,15 @@ struct ConwayCubeState { ++next_row_; } - ConwayCubeState next_state() const { + ConwayCubeState next_state() const + { ConwayCubeState next; - for (auto const &n : neighbours_) { + for (auto const& n : neighbours_) { bool active = points_.find(n.first) != points_.end(); if (active && (n.second == 2 || n.second == 3)) { next.set_point(n.first); - } else if (!active && n.second == 3) { + } + else if (!active && n.second == 3) { next.set_point(n.first); } } @@ -41,11 +45,10 @@ struct ConwayCubeState { auto active() const { return points_.size(); } private: - void set_point(Point const &pt) { - set_point(std::get<0>(pt), std::get<1>(pt), std::get<2>(pt)); - } + void set_point(Point const& pt) { set_point(std::get<0>(pt), std::get<1>(pt), std::get<2>(pt)); } - void set_point(int px, int py, int pz) { + void set_point(int px, int py, int pz) + { points_.insert({px, py, pz}); for (int x = px - 1; x < px + 2; ++x) { @@ -69,7 +72,8 @@ private: NeighbourMap neighbours_; }; -int main(void) { +int main(void) +{ std::string line; ConwayCubeState state; while (std::getline(std::cin, line)) { diff --git a/2020/puzzle-17-02.cc b/2020/puzzle-17-02.cc index a35fa62..4cc06e0 100644 --- a/2020/puzzle-17-02.cc +++ b/2020/puzzle-17-02.cc @@ -9,13 +9,15 @@ #include #include -struct ConwayCubeState { +struct ConwayCubeState +{ using Point = std::tuple; using PointSet = std::set; using NeighbourMap = std::map; ConwayCubeState() : next_row_(0) {} - void add_row(std::string const &s) { + void add_row(std::string const& s) + { for (int x = 0; x < s.size(); ++x) { if (s[x] == '#') { set_point(x, next_row_, 0, 0); @@ -24,13 +26,15 @@ struct ConwayCubeState { ++next_row_; } - ConwayCubeState next_state() const { + ConwayCubeState next_state() const + { ConwayCubeState next; - for (auto const &n : neighbours_) { + for (auto const& n : neighbours_) { bool active = points_.find(n.first) != points_.end(); if (active && (n.second == 2 || n.second == 3)) { next.set_point(n.first); - } else if (!active && n.second == 3) { + } + else if (!active && n.second == 3) { next.set_point(n.first); } } @@ -41,12 +45,13 @@ struct ConwayCubeState { auto active() const { return points_.size(); } private: - void set_point(Point const &pt) { - set_point(std::get<0>(pt), std::get<1>(pt), std::get<2>(pt), - std::get<3>(pt)); + void set_point(Point const& pt) + { + set_point(std::get<0>(pt), std::get<1>(pt), std::get<2>(pt), std::get<3>(pt)); } - void set_point(int pw, int px, int py, int pz) { + void set_point(int pw, int px, int py, int pz) + { points_.insert({pw, px, py, pz}); for (int w = pw - 1; w < pw + 2; ++w) { @@ -72,7 +77,8 @@ private: NeighbourMap neighbours_; }; -int main(void) { +int main(void) +{ std::string line; ConwayCubeState state; while (std::getline(std::cin, line)) { diff --git a/2020/puzzle-18-01.cc b/2020/puzzle-18-01.cc index 9e0b37a..f281860 100644 --- a/2020/puzzle-18-01.cc +++ b/2020/puzzle-18-01.cc @@ -9,46 +9,46 @@ #include #include -enum class Token : char { - Eof, - Number, - LParens = '(', - RParens = ')', - Add = '+', - Multiply = '*' -}; +enum class Token : char { Eof, Number, LParens = '(', RParens = ')', Add = '+', Multiply = '*' }; using Value = unsigned long; -struct Parser { - Parser(std::string const &s) : expr_(s), pos_(0) { skip_whitespace(); } +struct Parser +{ + Parser(std::string const& s) : expr_(s), pos_(0) { skip_whitespace(); } Value evaluate() { return binop(); } private: - Value binop() { + Value binop() + { auto value = primary(); do { if (peek() == Token::Add) { chew(Token::Add); value += primary(); - } else if (peek() == Token::Multiply) { + } + else if (peek() == Token::Multiply) { chew(Token::Multiply); value *= primary(); - } else { + } + else { return value; } } while (true); } - Value primary() { + Value primary() + { if (peek() == Token::LParens) { chew(Token::LParens); Value value = binop(); chew(Token::RParens); return value; - } else if (peek() == Token::Number) { + } + else if (peek() == Token::Number) { return chew_number(); - } else { + } + else { std::cout << "expr_ = " << expr_ << "\n"; std::cout << "pos_ = " << pos_ << "\n"; std::cout << "End = " << expr_.substr(pos_) << "\n"; @@ -56,7 +56,8 @@ private: } } - Token peek() { + Token peek() + { if (pos_ == expr_.size()) { return Token::Eof; } @@ -89,7 +90,8 @@ private: } } - void chew(Token tok) { + void chew(Token tok) + { assert(peek() == tok); switch (tok) { case Token::LParens: @@ -104,13 +106,15 @@ private: } } - void skip_whitespace() { + void skip_whitespace() + { while (pos_ < expr_.size() && expr_[pos_] == ' ') { ++pos_; } } - Value chew_number() { + Value chew_number() + { assert(peek() == Token::Number); std::size_t len = 0; @@ -124,7 +128,8 @@ private: std::string::size_type pos_; }; -int main(void) { +int main(void) +{ std::string line; Value result = 0; while (std::getline(std::cin, line)) { diff --git a/2020/puzzle-18-02.cc b/2020/puzzle-18-02.cc index 9cce34c..d02f608 100644 --- a/2020/puzzle-18-02.cc +++ b/2020/puzzle-18-02.cc @@ -9,55 +9,56 @@ #include #include -enum class Token : char { - Eof, - Number, - LParens = '(', - RParens = ')', - Add = '+', - Multiply = '*' -}; +enum class Token : char { Eof, Number, LParens = '(', RParens = ')', Add = '+', Multiply = '*' }; using Value = unsigned long; -struct Parser { - Parser(std::string const &s) : expr_(s), pos_(0) { skip_whitespace(); } +struct Parser +{ + Parser(std::string const& s) : expr_(s), pos_(0) { skip_whitespace(); } Value evaluate() { return multop(); } private: - Value addop() { + Value addop() + { auto value = primary(); do { if (peek() == Token::Add) { chew(Token::Add); value += primary(); - } else { + } + else { return value; } } while (true); } - Value multop() { + Value multop() + { auto value = addop(); do { if (peek() == Token::Multiply) { chew(Token::Multiply); value *= addop(); - } else { + } + else { return value; } } while (true); } - Value primary() { + Value primary() + { if (peek() == Token::LParens) { chew(Token::LParens); Value value = evaluate(); chew(Token::RParens); return value; - } else if (peek() == Token::Number) { + } + else if (peek() == Token::Number) { return chew_number(); - } else { + } + else { std::cout << "expr_ = " << expr_ << "\n"; std::cout << "pos_ = " << pos_ << "\n"; std::cout << "End = " << expr_.substr(pos_) << "\n"; @@ -65,7 +66,8 @@ private: } } - Token peek() { + Token peek() + { if (pos_ == expr_.size()) { return Token::Eof; } @@ -98,7 +100,8 @@ private: } } - void chew(Token tok) { + void chew(Token tok) + { assert(peek() == tok); switch (tok) { case Token::LParens: @@ -113,13 +116,15 @@ private: } } - void skip_whitespace() { + void skip_whitespace() + { while (pos_ < expr_.size() && expr_[pos_] == ' ') { ++pos_; } } - Value chew_number() { + Value chew_number() + { assert(peek() == Token::Number); std::size_t len = 0; @@ -133,7 +138,8 @@ private: std::string::size_type pos_; }; -int main(void) { +int main(void) +{ std::string line; Value result = 0; while (std::getline(std::cin, line)) { diff --git a/2020/puzzle-19-01.cc b/2020/puzzle-19-01.cc index c16ec28..f1d6526 100644 --- a/2020/puzzle-19-01.cc +++ b/2020/puzzle-19-01.cc @@ -9,8 +9,10 @@ #include #include -struct Matcher { - void add_rule(std::string const &s) { +struct Matcher +{ + void add_rule(std::string const& s) + { std::size_t len = 0; unsigned long id = std::stoul(s, &len); assert(s[len] == ':'); @@ -26,24 +28,25 @@ struct Matcher { void calculate_regex() { re_.assign("^" + expand_rule(0) + "$"); } - bool does_match(std::string const &s) const { - return std::regex_match(s, re_); - } + bool does_match(std::string const& s) const { return std::regex_match(s, re_); } private: - std::string expand_rule(std::size_t id) { + std::string expand_rule(std::size_t id) + { std::string re; - std::string const &rule = rules_[id]; + std::string const& rule = rules_[id]; std::size_t pos = 0; bool needs_brackets = false; while (pos < rule.size()) { if (rule[pos] == ' ') { ++pos; - } else if (rule[pos] == '|') { + } + else if (rule[pos] == '|') { re += "|"; needs_brackets = true; ++pos; - } else if (rule[pos] == '"') { + } + else if (rule[pos] == '"') { ++pos; while (pos < rule.size() && rule[pos] != '"') { re += rule[pos]; @@ -52,12 +55,14 @@ private: assert(pos < rule.size()); assert(rule[pos] == '"'); ++pos; - } else if (std::isdigit(rule[pos])) { + } + else if (std::isdigit(rule[pos])) { std::size_t len = 0; std::size_t subid = std::stoul(rule.substr(pos), &len); pos += len; re += expand_rule(subid); - } else { + } + else { assert(false); } } @@ -73,7 +78,8 @@ private: std::regex re_; }; -int main(void) { +int main(void) +{ std::string line; Matcher matcher; bool adding_rules = true; @@ -82,9 +88,11 @@ int main(void) { if (line.empty()) { adding_rules = false; matcher.calculate_regex(); - } else if (adding_rules) { + } + else if (adding_rules) { matcher.add_rule(line); - } else { + } + else { bool m = matcher.does_match(line); std::cout << line << ": does " << (m ? "" : "not ") << "match\n"; matches += m; diff --git a/2020/puzzle-19-02.cc b/2020/puzzle-19-02.cc index 09a554d..4ab1509 100644 --- a/2020/puzzle-19-02.cc +++ b/2020/puzzle-19-02.cc @@ -9,8 +9,10 @@ #include #include -struct Matcher { - void add_rule(std::string const &s) { +struct Matcher +{ + void add_rule(std::string const& s) + { std::size_t len = 0; unsigned long id = std::stoul(s, &len); assert(s[len] == ':'); @@ -24,12 +26,14 @@ struct Matcher { rules_[id] = s.substr(len); } - void calculate_regex() { + void calculate_regex() + { re42_ = expand_rule(42); re31_ = expand_rule(31); } - bool does_match(std::string const &s) const { + bool does_match(std::string const& s) const + { std::smatch m; std::string begin = "^" + re42_ + re42_; unsigned repeats = 1; @@ -50,19 +54,22 @@ struct Matcher { } private: - std::string expand_rule(std::size_t id) { + std::string expand_rule(std::size_t id) + { std::string re; - std::string const &rule = rules_[id]; + std::string const& rule = rules_[id]; std::size_t pos = 0; bool needs_brackets = false; while (pos < rule.size()) { if (rule[pos] == ' ') { ++pos; - } else if (rule[pos] == '|') { + } + else if (rule[pos] == '|') { re += "|"; needs_brackets = true; ++pos; - } else if (rule[pos] == '"') { + } + else if (rule[pos] == '"') { ++pos; while (pos < rule.size() && rule[pos] != '"') { re += rule[pos]; @@ -71,12 +78,14 @@ private: assert(pos < rule.size()); assert(rule[pos] == '"'); ++pos; - } else if (std::isdigit(rule[pos])) { + } + else if (std::isdigit(rule[pos])) { std::size_t len = 0; std::size_t subid = std::stoul(rule.substr(pos), &len); pos += len; re += expand_rule(subid); - } else { + } + else { assert(false); } } @@ -93,7 +102,8 @@ private: std::string re31_; }; -int main(void) { +int main(void) +{ std::string line; Matcher matcher; bool adding_rules = true; @@ -102,9 +112,11 @@ int main(void) { if (line.empty()) { adding_rules = false; matcher.calculate_regex(); - } else if (adding_rules) { + } + else if (adding_rules) { matcher.add_rule(line); - } else { + } + else { bool m = matcher.does_match(line); std::cout << line << ": does " << (m ? "" : "not ") << "match\n"; matches += m; diff --git a/2020/puzzle-20-01.cc b/2020/puzzle-20-01.cc index 551f591..03ff01f 100644 --- a/2020/puzzle-20-01.cc +++ b/2020/puzzle-20-01.cc @@ -14,8 +14,10 @@ using Hash = unsigned long; enum Edge { Top, Left, Bottom, Right }; -struct Picture { - Picture(std::string id, std::istream &is) : in_use_(false) { +struct Picture +{ + Picture(std::string id, std::istream& is) : in_use_(false) + { assert(id.substr(0, 5) == "Tile "); id_ = std::stoul(id.substr(5)); std::string line; @@ -27,20 +29,22 @@ struct Picture { } } - Picture(Picture const &) = delete; - Picture &operator=(Picture const &) = delete; - Picture(Picture &&) = default; - Picture &operator=(Picture &&) = default; + Picture(Picture const&) = delete; + Picture& operator=(Picture const&) = delete; + Picture(Picture&&) = default; + Picture& operator=(Picture&&) = default; - void flip() { - for (auto &r : rows_) { + void flip() + { + for (auto& r : rows_) { std::reverse(r.begin(), r.end()); } } - void rotate() { + void rotate() + { std::vector copy(rows_.size()); - for (auto const &r : rows_) { + for (auto const& r : rows_) { std::size_t off = copy.size(); assert(r.size() == copy.size()); for (auto c : r) { @@ -51,7 +55,8 @@ struct Picture { rows_ = copy; } - Hash hash(Edge edge) const { + Hash hash(Edge edge) const + { unsigned x = (edge == Edge::Right) ? rows_[0].size() - 1 : 0; unsigned y = (edge == Edge::Bottom) ? rows_.size() - 1 : 0; unsigned dx = (edge == Edge::Top || edge == Edge::Bottom) ? 1 : 0; @@ -70,11 +75,9 @@ struct Picture { Id id() const noexcept { return id_; } - bool operator<(Picture const &pict) const noexcept { return id_ < pict.id_; } + bool operator<(Picture const& pict) const noexcept { return id_ < pict.id_; } - bool operator==(Picture const &pict) const noexcept { - return id_ == pict.id_; - } + bool operator==(Picture const& pict) const noexcept { return id_ == pict.id_; } bool in_use() const noexcept { return in_use_; } void use() noexcept { in_use_ = true; } @@ -90,14 +93,16 @@ using Pictures = std::map; using HashMap = std::multimap; using Array = std::map, Id>; -struct PictureArray { - void add(Picture &&pic) { +struct PictureArray +{ + void add(Picture&& pic) + { auto id = pic.id(); auto [it, success] = pictures_.insert(std::make_pair(id, std::move(pic))); assert(success); // Set up hash -> ID mapping - Picture &picture = it->second; + Picture& picture = it->second; for (unsigned r = 0; r < 4; ++r) { for (unsigned f = 0; f < 2; ++f) { hash_map_.insert({picture.hash(Edge::Top), picture.id()}); @@ -107,13 +112,13 @@ struct PictureArray { } } - Id solve() { + Id solve() + { assert(pictures_.size() == 9 || pictures_.size() == 144); - for (auto &kv : pictures_) { + for (auto& kv : pictures_) { if (try_position(0, 0, kv.second)) { print_ids(); - return piece(0, 0).id() * piece(width() - 1, 0).id() * - piece(0, height() - 1).id() * + return piece(0, 0).id() * piece(width() - 1, 0).id() * piece(0, height() - 1).id() * piece(width() - 1, height() - 1).id(); } } @@ -123,7 +128,8 @@ struct PictureArray { } private: - bool try_position(unsigned x, unsigned y, Picture &pict) { + bool try_position(unsigned x, unsigned y, Picture& pict) + { if (pict.in_use()) { return false; } @@ -180,7 +186,8 @@ private: return false; } - void print_ids() const { + void print_ids() const + { for (unsigned y = 0; y < height(); ++y) { for (unsigned x = 0; x < width(); ++x) { std::cout << " " << piece(x, y).id(); @@ -189,20 +196,24 @@ private: } } - Picture const &piece(unsigned x, unsigned y) const { - auto const &it = array_.find({x, y}); + Picture const& piece(unsigned x, unsigned y) const + { + auto const& it = array_.find({x, y}); assert(it != array_.end()); - auto const &itp = pictures_.find(it->second); + auto const& itp = pictures_.find(it->second); assert(itp != pictures_.end()); return itp->second; } - unsigned width() const noexcept { + unsigned width() const noexcept + { if (pictures_.size() == 9) { return 3; - } else if (pictures_.size() == 144) { + } + else if (pictures_.size() == 144) { return 12; - } else { + } + else { assert(false); } @@ -216,7 +227,8 @@ private: Array array_; }; -int main(void) { +int main(void) +{ PictureArray pictures_; std::string line; diff --git a/2020/puzzle-20-02.cc b/2020/puzzle-20-02.cc index 64feb27..d38c45b 100644 --- a/2020/puzzle-20-02.cc +++ b/2020/puzzle-20-02.cc @@ -16,8 +16,10 @@ enum Edge { Top, Left, Bottom, Right }; struct PictureArray; -struct Picture { - Picture(std::string id, std::istream &is) : in_use_(false) { +struct Picture +{ + Picture(std::string id, std::istream& is) : in_use_(false) + { assert(id.substr(0, 5) == "Tile "); id_ = std::stoul(id.substr(5)); std::string line; @@ -29,22 +31,24 @@ struct Picture { } } - Picture(PictureArray const &array); + Picture(PictureArray const& array); - Picture(Picture const &) = delete; - Picture &operator=(Picture const &) = delete; - Picture(Picture &&) = default; - Picture &operator=(Picture &&) = default; + Picture(Picture const&) = delete; + Picture& operator=(Picture const&) = delete; + Picture(Picture&&) = default; + Picture& operator=(Picture&&) = default; - void flip() { - for (auto &r : rows_) { + void flip() + { + for (auto& r : rows_) { std::reverse(r.begin(), r.end()); } } - void rotate() { + void rotate() + { std::vector copy(rows_.size()); - for (auto const &r : rows_) { + for (auto const& r : rows_) { std::size_t off = copy.size(); assert(r.size() == copy.size()); for (auto c : r) { @@ -55,7 +59,8 @@ struct Picture { rows_ = copy; } - Hash hash(Edge edge) const { + Hash hash(Edge edge) const + { unsigned x = (edge == Edge::Right) ? rows_[0].size() - 1 : 0; unsigned y = (edge == Edge::Bottom) ? rows_.size() - 1 : 0; unsigned dx = (edge == Edge::Top || edge == Edge::Bottom) ? 1 : 0; @@ -74,19 +79,18 @@ struct Picture { Id id() const noexcept { return id_; } - bool operator<(Picture const &pict) const noexcept { return id_ < pict.id_; } + bool operator<(Picture const& pict) const noexcept { return id_ < pict.id_; } - bool operator==(Picture const &pict) const noexcept { - return id_ == pict.id_; - } + bool operator==(Picture const& pict) const noexcept { return id_ == pict.id_; } bool in_use() const noexcept { return in_use_; } void use() noexcept { in_use_ = true; } void release() noexcept { in_use_ = false; } - friend std::ostream &operator<<(std::ostream &os, Picture const &pict); + friend std::ostream& operator<<(std::ostream& os, Picture const& pict); - unsigned find_monsters() { + unsigned find_monsters() + { for (unsigned r = 0; r < 4; ++r) { rotate(); for (unsigned f = 0; f < 2; ++f) { @@ -101,9 +105,10 @@ struct Picture { return 0; } - unsigned roughness() const { + unsigned roughness() const + { unsigned rough = 0; - for (auto const &r : rows_) { + for (auto const& r : rows_) { for (auto c : r) { if (c == '#') { ++rough; @@ -114,7 +119,8 @@ struct Picture { } private: - unsigned find_monsters1() { + unsigned find_monsters1() + { // 0 1 // 01234567890123456789 // # @@ -146,12 +152,13 @@ private: for (std::size_t y = 0; y <= rows_.size() - mheight; ++y) { for (std::string::size_type x = 0; x <= rows_[y].size() - mwidth; ++x) { std::size_t cy = 0; - std::string::size_type const *cx = locs; + std::string::size_type const* cx = locs; bool found = true; while (cy < mheight) { if (*cx == std::string::npos) { ++cy; - } else if (rows_[y + cy][x + *cx] != '#') { + } + else if (rows_[y + cy][x + *cx] != '#') { found = false; break; } @@ -160,11 +167,12 @@ private: if (found) { ++monster_count; std::size_t cy = 0; - std::string::size_type const *cx = locs; + std::string::size_type const* cx = locs; while (cy < mheight) { if (*cx == std::string::npos) { ++cy; - } else { + } + else { rows_[y + cy][x + *cx] = '*'; } ++cx; @@ -182,8 +190,9 @@ private: bool in_use_; }; -std::ostream &operator<<(std::ostream &os, Picture const &pict) { - for (auto const &r : pict.rows_) { +std::ostream& operator<<(std::ostream& os, Picture const& pict) +{ + for (auto const& r : pict.rows_) { os << r << '\n'; } return os; @@ -193,14 +202,16 @@ using Pictures = std::map; using HashMap = std::multimap; using Array = std::map, Id>; -struct PictureArray { - void add(Picture &&pic) { +struct PictureArray +{ + void add(Picture&& pic) + { auto id = pic.id(); auto [it, success] = pictures_.insert(std::make_pair(id, std::move(pic))); assert(success); // Set up hash -> ID mapping - Picture &picture = it->second; + Picture& picture = it->second; for (unsigned r = 0; r < 4; ++r) { for (unsigned f = 0; f < 2; ++f) { hash_map_.insert({picture.hash(Edge::Top), picture.id()}); @@ -210,13 +221,13 @@ struct PictureArray { } } - Id solve() { + Id solve() + { assert(pictures_.size() == 9 || pictures_.size() == 144); - for (auto &kv : pictures_) { + for (auto& kv : pictures_) { if (try_position(0, 0, kv.second)) { print_ids(); - return piece(0, 0).id() * piece(width() - 1, 0).id() * - piece(0, height() - 1).id() * + return piece(0, 0).id() * piece(width() - 1, 0).id() * piece(0, height() - 1).id() * piece(width() - 1, height() - 1).id(); } } @@ -224,12 +235,15 @@ struct PictureArray { assert(false); return 0; } - unsigned width() const noexcept { + unsigned width() const noexcept + { if (pictures_.size() == 9) { return 3; - } else if (pictures_.size() == 144) { + } + else if (pictures_.size() == 144) { return 12; - } else { + } + else { assert(false); } @@ -238,16 +252,18 @@ struct PictureArray { unsigned height() const noexcept { return width(); } - Picture const &piece(unsigned x, unsigned y) const { - auto const &it = array_.find({x, y}); + Picture const& piece(unsigned x, unsigned y) const + { + auto const& it = array_.find({x, y}); assert(it != array_.end()); - auto const &itp = pictures_.find(it->second); + auto const& itp = pictures_.find(it->second); assert(itp != pictures_.end()); return itp->second; } private: - bool try_position(unsigned x, unsigned y, Picture &pict) { + bool try_position(unsigned x, unsigned y, Picture& pict) + { if (pict.in_use()) { return false; } @@ -304,7 +320,8 @@ private: return false; } - void print_ids() const { + void print_ids() const + { for (unsigned y = 0; y < height(); ++y) { for (unsigned x = 0; x < width(); ++x) { std::cout << " " << piece(x, y).id(); @@ -318,11 +335,12 @@ private: Array array_; }; -Picture::Picture(PictureArray const &array) : id_(0), in_use_(false) { +Picture::Picture(PictureArray const& array) : id_(0), in_use_(false) +{ for (unsigned y = 0; y < array.height(); ++y) { auto ybase = rows_.size(); for (unsigned x = 0; x < array.width(); ++x) { - auto &pict = array.piece(x, y); + auto& pict = array.piece(x, y); for (auto py = 1; py < pict.rows_.size() - 1; ++py) { auto yidx = ybase + py - 1; if (rows_.size() <= yidx) { @@ -336,7 +354,8 @@ Picture::Picture(PictureArray const &array) : id_(0), in_use_(false) { assert(rows_[0].size() == array.width() * 8); } -int main(void) { +int main(void) +{ PictureArray pictures_; std::string line; diff --git a/2020/puzzle-21-01.cc b/2020/puzzle-21-01.cc index 81429e5..63b6a9a 100644 --- a/2020/puzzle-21-01.cc +++ b/2020/puzzle-21-01.cc @@ -19,9 +19,11 @@ using IngredientInfo = std::pair; using IngredientMap = std::map; using AllergenMap = std::map; -class IngredientParser { +class IngredientParser +{ public: - void add_recipe(std::string const &s) { + void add_recipe(std::string const& s) + { auto it = s.begin(); Ingredients i; while (it != s.end()) { @@ -33,10 +35,10 @@ public: it = ite; if (ingredient == "(contains") { break; - } else { + } + else { i.insert(ingredient); - auto [iit, success] = - ingredients_.insert({ingredient, {1, Allergens()}}); + auto [iit, success] = ingredients_.insert({ingredient, {1, Allergens()}}); if (!success) { iit->second.first++; } @@ -44,8 +46,7 @@ public: } while (it != s.end()) { - auto ite = std::find_if( - it, s.end(), [](char c) -> bool { return c == ',' || c == ')'; }); + auto ite = std::find_if(it, s.end(), [](char c) -> bool { return c == ',' || c == ')'; }); auto allergen = std::string(it, ite); ++ite; while (ite != s.end() && *ite == ' ') { @@ -56,18 +57,18 @@ public: if (!success) { Ingredients a; std::set_intersection(i.begin(), i.end(), insert_it->second.begin(), - insert_it->second.end(), - std::inserter(a, a.end())); + insert_it->second.end(), std::inserter(a, a.end())); insert_it->second = a; } } } - unsigned clean_ingredients() { - for (auto const &kv : allergens_) { + unsigned clean_ingredients() + { + for (auto const& kv : allergens_) { auto allergen = kv.first; std::cout << "Allergen " << allergen << ":"; - for (auto const &i : kv.second) { + for (auto const& i : kv.second) { std::cout << " " << i; auto it = ingredients_.find(i); assert(it != ingredients_.end()); @@ -77,10 +78,9 @@ public: } unsigned count = 0; - for (auto const &i : ingredients_) { + for (auto const& i : ingredients_) { if (i.second.second.size() == 0) { - std::cout << i.first << " is not an allergen, appears " - << i.second.first << ".\n"; + std::cout << i.first << " is not an allergen, appears " << i.second.first << ".\n"; count += i.second.first; } } @@ -93,7 +93,8 @@ private: AllergenMap allergens_; }; -int main(void) { +int main(void) +{ std::string line; IngredientParser parser; while (std::getline(std::cin, line)) { diff --git a/2020/puzzle-21-02.cc b/2020/puzzle-21-02.cc index e1b8cbd..de2bfe5 100644 --- a/2020/puzzle-21-02.cc +++ b/2020/puzzle-21-02.cc @@ -19,9 +19,11 @@ using IngredientInfo = std::pair; using IngredientMap = std::map; using AllergenMap = std::map; -class IngredientParser { +class IngredientParser +{ public: - void add_recipe(std::string const &s) { + void add_recipe(std::string const& s) + { auto it = s.begin(); Ingredients i; while (it != s.end()) { @@ -33,10 +35,10 @@ public: it = ite; if (ingredient == "(contains") { break; - } else { + } + else { i.insert(ingredient); - auto [iit, success] = - ingredients_.insert({ingredient, {1, Allergens()}}); + auto [iit, success] = ingredients_.insert({ingredient, {1, Allergens()}}); if (!success) { iit->second.first++; } @@ -44,8 +46,7 @@ public: } while (it != s.end()) { - auto ite = std::find_if( - it, s.end(), [](char c) -> bool { return c == ',' || c == ')'; }); + auto ite = std::find_if(it, s.end(), [](char c) -> bool { return c == ',' || c == ')'; }); auto allergen = std::string(it, ite); ++ite; while (ite != s.end() && *ite == ' ') { @@ -56,18 +57,18 @@ public: if (!success) { Ingredients a; std::set_intersection(i.begin(), i.end(), insert_it->second.begin(), - insert_it->second.end(), - std::inserter(a, a.end())); + insert_it->second.end(), std::inserter(a, a.end())); insert_it->second = a; } } } - unsigned clean_ingredients() { - for (auto const &kv : allergens_) { + unsigned clean_ingredients() + { + for (auto const& kv : allergens_) { auto allergen = kv.first; std::cout << "Allergen " << allergen << ":"; - for (auto const &i : kv.second) { + for (auto const& i : kv.second) { std::cout << " " << i; auto it = ingredients_.find(i); assert(it != ingredients_.end()); @@ -77,10 +78,9 @@ public: } unsigned count = 0; - for (auto const &i : ingredients_) { + for (auto const& i : ingredients_) { if (i.second.second.size() == 0) { - std::cout << i.first << " is not an allergen, appears " - << i.second.first << ".\n"; + std::cout << i.first << " is not an allergen, appears " << i.second.first << ".\n"; count += i.second.first; } } @@ -88,23 +88,23 @@ public: return count; } - std::string dangerous_ingredients() { + std::string dangerous_ingredients() + { bool changed = true; Ingredients i; while (changed) { changed = false; - for (auto const &kv : allergens_) { + for (auto const& kv : allergens_) { if (kv.second.size() == 1) { auto it = kv.second.begin(); - Ingredient const &ing = *it; + Ingredient const& ing = *it; auto [iit, success] = i.insert(ing); if (!success) { continue; } changed = true; - std::cout << "Allergen " << kv.first << " is in ingredient " << ing - << "\n"; - for (auto &kv2 : allergens_) { + std::cout << "Allergen " << kv.first << " is in ingredient " << ing << "\n"; + for (auto& kv2 : allergens_) { if (kv2.second.size() != 1) { kv2.second.erase(ing); } @@ -114,7 +114,7 @@ public: } std::string result; - for (auto const &kv : allergens_) { + for (auto const& kv : allergens_) { result += ","; result += *kv.second.begin(); } @@ -127,7 +127,8 @@ private: AllergenMap allergens_; }; -int main(void) { +int main(void) +{ std::string line; IngredientParser parser; while (std::getline(std::cin, line)) { diff --git a/2020/puzzle-22-01.cc b/2020/puzzle-22-01.cc index 4e313ad..1eea162 100644 --- a/2020/puzzle-22-01.cc +++ b/2020/puzzle-22-01.cc @@ -14,8 +14,10 @@ using Card = unsigned; using Cards = std::list; using Score = unsigned long; -struct Player { - Player(std::istream &is) { +struct Player +{ + Player(std::istream& is) + { std::string line; if (!std::getline(is, name_)) { assert(false); @@ -33,7 +35,8 @@ struct Player { void pop_front() { cards_.pop_front(); } void push_back(Card c) { cards_.push_back(c); } - Score score() const { + Score score() const + { Score r = 0; unsigned idx = 1; for (auto it = cards_.rbegin(); it != cards_.rend(); ++it) { @@ -48,7 +51,8 @@ private: Cards cards_; }; -Score play_game(Player &p1, Player &p2) { +Score play_game(Player& p1, Player& p2) +{ while (!p1.empty() && !p2.empty()) { Card c1 = p1.front(); Card c2 = p2.front(); @@ -57,7 +61,8 @@ Score play_game(Player &p1, Player &p2) { if (c1 > c2) { p1.push_back(c1); p1.push_back(c2); - } else if (c1 < c2) { + } + else if (c1 < c2) { p2.push_back(c2); p2.push_back(c1); } @@ -66,7 +71,8 @@ Score play_game(Player &p1, Player &p2) { return p1.score() + p2.score(); } -int main(void) { +int main(void) +{ Player player1(std::cin); Player player2(std::cin); diff --git a/2020/puzzle-22-02.cc b/2020/puzzle-22-02.cc index 135032c..509270f 100644 --- a/2020/puzzle-22-02.cc +++ b/2020/puzzle-22-02.cc @@ -15,8 +15,10 @@ using Cards = std::list; using Score = unsigned long; using Hash = std::string; -struct Player { - Player(std::istream &is) { +struct Player +{ + Player(std::istream& is) + { std::string line; if (!std::getline(is, name_)) { assert(false); @@ -35,7 +37,8 @@ struct Player { void push_back(Card c) { cards_.push_back(c); } std::size_t size() const { return cards_.size(); } - Score score() const { + Score score() const + { Score r = 0; unsigned idx = 1; for (auto it = cards_.rbegin(); it != cards_.rend(); ++it) { @@ -45,7 +48,8 @@ struct Player { return r; } - Hash hash() const { + Hash hash() const + { std::string r = name_; for (auto c : cards_) { assert(c > 0); @@ -56,21 +60,23 @@ struct Player { return r; } - Player(Player const &prev, Card size) : name_(prev.name_ + "-") { + Player(Player const& prev, Card size) : name_(prev.name_ + "-") + { auto it = prev.cards_.begin(); for (Card i = 0; i < size; ++i) { cards_.push_back(*it++); } } - friend std::ostream &operator<<(std::ostream &os, Player const &p); + friend std::ostream& operator<<(std::ostream& os, Player const& p); private: std::string name_; Cards cards_; }; -std::ostream &operator<<(std::ostream &os, Player const &p) { +std::ostream& operator<<(std::ostream& os, Player const& p) +{ os << p.name_; for (auto c : p.cards_) { os << " " << c; @@ -79,7 +85,8 @@ std::ostream &operator<<(std::ostream &os, Player const &p) { } // False = p1 wins, true = p2 wins. -bool play_game(Player &p1, Player &p2) { +bool play_game(Player& p1, Player& p2) +{ std::set hashes; while (!p1.empty() && !p2.empty()) { @@ -100,17 +107,21 @@ bool play_game(Player &p1, Player &p2) { if (!result) { p1.push_back(c1); p1.push_back(c2); - } else { + } + else { p2.push_back(c2); p2.push_back(c1); } - } else if (c1 > c2) { + } + else if (c1 > c2) { p1.push_back(c1); p1.push_back(c2); - } else if (c1 < c2) { + } + else if (c1 < c2) { p2.push_back(c2); p2.push_back(c1); - } else { + } + else { assert(false); } } @@ -118,7 +129,8 @@ bool play_game(Player &p1, Player &p2) { return p1.empty(); } -int main(void) { +int main(void) +{ Player player1(std::cin); Player player2(std::cin); diff --git a/2020/puzzle-23-01.cc b/2020/puzzle-23-01.cc index 1602d21..2f4b548 100644 --- a/2020/puzzle-23-01.cc +++ b/2020/puzzle-23-01.cc @@ -10,12 +10,12 @@ #include #include -struct Cups { - Cups(std::string const &s) : s_(s) { - std::cout << "Initial state: " << s_ << "\n"; - } +struct Cups +{ + Cups(std::string const& s) : s_(s) { std::cout << "Initial state: " << s_ << "\n"; } - void play(unsigned moves) { + void play(unsigned moves) + { // We assume the current value is always at index 0. // So the values to remove are in offsets [1, 4). while (moves-- > 0) { @@ -27,13 +27,13 @@ struct Cups { d = d == '1' ? '9' : d - 1; } - s_ = s_.substr(4, dest - 3) + s_.substr(1, 3) + s_.substr(dest + 1) + - s_[0]; + s_ = s_.substr(4, dest - 3) + s_.substr(1, 3) + s_.substr(dest + 1) + s_[0]; std::cout << moves << ": " << s_ << "\n"; } } - std::string result() const { + std::string result() const + { auto split = s_.find('1'); return s_.substr(split + 1) + s_.substr(0, split); } @@ -42,7 +42,8 @@ private: std::string s_; }; -int main(void) { +int main(void) +{ std::string line; std::getline(std::cin, line); Cups cups(line); diff --git a/2020/puzzle-23-02.cc b/2020/puzzle-23-02.cc index 0f2fb47..a75058a 100644 --- a/2020/puzzle-23-02.cc +++ b/2020/puzzle-23-02.cc @@ -11,24 +11,27 @@ #include using Value = unsigned long; -struct Cup { +struct Cup +{ Cup() : value_(0), next_(nullptr) {} Value value() const { return value_; } void value(Value v) { value_ = v; } - Cup *next() const { return next_; } - void next(Cup *n) { next_ = n; } + Cup* next() const { return next_; } + void next(Cup* n) { next_ = n; } private: Value value_; - Cup *next_; + Cup* next_; }; -struct Cups { +struct Cups +{ static constexpr std::size_t num_cups_ = 1000000; static constexpr std::size_t num_moves_ = 10000000; - Cups(std::string const &s) : cups_(new Cup[num_cups_]), current_(cups_) { + Cups(std::string const& s) : cups_(new Cup[num_cups_]), current_(cups_) + { std::size_t idx = 0; for (auto c : s) { cups_[idx].value(c - '0'); @@ -47,17 +50,18 @@ struct Cups { ~Cups() { delete[] cups_; } - void play() { + void play() + { for (std::size_t move = 0; move < num_moves_; ++move) { if (move % 1000 == 0) { - std::cout << "A" << move << " " << current_ - cups_ << " " - << current_->value() << " " << result() << "\n"; + std::cout << "A" << move << " " << current_ - cups_ << " " << current_->value() << " " + << result() << "\n"; // print(); } // Remove first three after current. - Cup *rb = current_->next(); - Cup *re = rb->next()->next(); + Cup* rb = current_->next(); + Cup* re = rb->next()->next(); current_->next(re->next()); re->next(nullptr); @@ -74,14 +78,15 @@ struct Cups { // Where do we insert? Note that we use the hack that all the values are // actually in an index and we know what the value is (roughly) based on // the index. - Cup *ins = nullptr; + Cup* ins = nullptr; if (vm1 < 10) { ins = cups_; while (ins->value() != vm1) { assert(ins != cups_ + 10); ++ins; } - } else { + } + else { ins = cups_ + vm1 - 1; } @@ -96,8 +101,9 @@ struct Cups { } } - Value result() const { - Cup *one = cups_; + Value result() const + { + Cup* one = cups_; while (one->value() != 1) { ++one; assert(one != cups_ + num_cups_); @@ -106,9 +112,10 @@ struct Cups { return one->next()->value() * one->next()->next()->value(); } - void print() const { + void print() const + { std::cout << "State:"; - Cup const *c = current_; + Cup const* c = current_; do { std::cout << " " << c->value(); c = c->next(); @@ -117,11 +124,12 @@ struct Cups { } private: - Cup *cups_; - Cup *current_; + Cup* cups_; + Cup* current_; }; -int main(void) { +int main(void) +{ std::string line; std::getline(std::cin, line); Cups cups(line); diff --git a/2020/puzzle-24-01.cc b/2020/puzzle-24-01.cc index 3a48f90..d5a0f22 100644 --- a/2020/puzzle-24-01.cc +++ b/2020/puzzle-24-01.cc @@ -14,8 +14,10 @@ using Coord = long; using Position = std::pair; using Tiles = std::set; -struct Tiler { - void flip_tile(std::string const &s) { +struct Tiler +{ + void flip_tile(std::string const& s) + { Coord x = 0; Coord y = 0; auto it = s.begin(); @@ -30,9 +32,11 @@ struct Tiler { if (*it == 'e') { x += dx; - } else if (*it == 'w') { + } + else if (*it == 'w') { x -= dx; - } else { + } + else { assert(false); } ++it; @@ -51,7 +55,8 @@ private: Tiles black_tiles_; }; -int main(void) { +int main(void) +{ Tiler tiler; std::string line; while (std::getline(std::cin, line)) { diff --git a/2020/puzzle-24-02.cc b/2020/puzzle-24-02.cc index fb3ce21..403362c 100644 --- a/2020/puzzle-24-02.cc +++ b/2020/puzzle-24-02.cc @@ -12,14 +12,17 @@ using Coord = long; using Position = std::pair; -struct State { +struct State +{ bool black_ = false; int neighbours_ = 0; }; using Tiles = std::map; -struct Tiler { - void flip_tile(std::string const &s) { +struct Tiler +{ + void flip_tile(std::string const& s) + { Coord x = 0; Coord y = 0; auto it = s.begin(); @@ -34,9 +37,11 @@ struct Tiler { if (*it == 'e') { x += dx; - } else if (*it == 'w') { + } + else if (*it == 'w') { x -= dx; - } else { + } + else { assert(false); } ++it; @@ -46,13 +51,14 @@ struct Tiler { flip_tile(x, y); } - Tiler next_state() const { + Tiler next_state() const + { Tiler next(*this); - for (auto const &kv : tiles_) { - if (kv.second.black_ && - (kv.second.neighbours_ == 0 || kv.second.neighbours_ > 2)) { + for (auto const& kv : tiles_) { + if (kv.second.black_ && (kv.second.neighbours_ == 0 || kv.second.neighbours_ > 2)) { next.flip_tile(kv.first.first, kv.first.second); - } else if (!kv.second.black_ && kv.second.neighbours_ == 2) { + } + else if (!kv.second.black_ && kv.second.neighbours_ == 2) { next.flip_tile(kv.first.first, kv.first.second); } } @@ -60,9 +66,10 @@ struct Tiler { return next; } - std::size_t black_count() const noexcept { + std::size_t black_count() const noexcept + { std::size_t count = 0; - for (auto const &kv : tiles_) { + for (auto const& kv : tiles_) { count += kv.second.black_; } @@ -70,7 +77,8 @@ struct Tiler { } private: - void set_neighbours(Coord x, Coord y, int delta) { + void set_neighbours(Coord x, Coord y, int delta) + { set_neighbour(x + 2, y, delta); set_neighbour(x + 1, y - 1, delta); set_neighbour(x - 1, y - 1, delta); @@ -79,7 +87,8 @@ private: set_neighbour(x + 1, y + 1, delta); } - void set_neighbour(Coord x, Coord y, int delta) { + void set_neighbour(Coord x, Coord y, int delta) + { auto [it, success] = tiles_.insert({{x, y}, {false, delta}}); if (!success) { it->second.neighbours_ += delta; @@ -87,14 +96,16 @@ private: assert(it->second.neighbours_ >= 0); } - void flip_tile(Coord x, Coord y) { + void flip_tile(Coord x, Coord y) + { auto [it, success] = tiles_.insert({{x, y}, {true, 0}}); if (!success) { it->second.black_ = !it->second.black_; } if (it->second.black_) { set_neighbours(x, y, 1); - } else { + } + else { set_neighbours(x, y, -1); } } @@ -103,7 +114,8 @@ private: Tiles tiles_; }; -int main(void) { +int main(void) +{ Tiler tiler; std::string line; while (std::getline(std::cin, line)) { diff --git a/2020/puzzle-25-01.cc b/2020/puzzle-25-01.cc index c89ed20..3867f1d 100644 --- a/2020/puzzle-25-01.cc +++ b/2020/puzzle-25-01.cc @@ -13,11 +13,13 @@ constexpr unsigned mod = 20201227; constexpr unsigned initial_subject = 7; -unsigned lcg(unsigned i, unsigned subject) { +unsigned lcg(unsigned i, unsigned subject) +{ return ((unsigned long)i * (unsigned long)subject) % mod; } -unsigned find_loop_length(unsigned pk) { +unsigned find_loop_length(unsigned pk) +{ unsigned v = 1; unsigned loop = 0; while (v != pk) { @@ -28,7 +30,8 @@ unsigned find_loop_length(unsigned pk) { return loop; } -unsigned apply(unsigned pk, unsigned loop_size) { +unsigned apply(unsigned pk, unsigned loop_size) +{ unsigned v = 1; for (unsigned i = 0; i < loop_size; ++i) { v = lcg(v, pk); @@ -37,7 +40,8 @@ unsigned apply(unsigned pk, unsigned loop_size) { return v; } -int main(void) { +int main(void) +{ std::string line; std::getline(std::cin, line); unsigned pk1 = std::stoul(line); diff --git a/2021/puzzle-01-01.cc b/2021/puzzle-01-01.cc index 0bcb7a9..cdd7a0e 100644 --- a/2021/puzzle-01-01.cc +++ b/2021/puzzle-01-01.cc @@ -3,21 +3,22 @@ // #include +#include #include #include -#include -int main(void) { - unsigned long prev{std::numeric_limits::max()}; - unsigned incrs{0}; - std::string line; - while (std::getline(std::cin, line)) { - auto current{std::stoul(line)}; - if (current > prev) { - ++incrs; - } - prev = current; +int main(void) +{ + unsigned long prev{std::numeric_limits::max()}; + unsigned incrs{0}; + std::string line; + while (std::getline(std::cin, line)) { + auto current{std::stoul(line)}; + if (current > prev) { + ++incrs; } - std::cout << "Number of increments: " << incrs << '\n'; - return EXIT_SUCCESS; + prev = current; + } + std::cout << "Number of increments: " << incrs << '\n'; + return EXIT_SUCCESS; } diff --git a/2021/puzzle-01-02.cc b/2021/puzzle-01-02.cc index 953b056..153df1a 100644 --- a/2021/puzzle-01-02.cc +++ b/2021/puzzle-01-02.cc @@ -3,30 +3,31 @@ // #include +#include #include #include -#include #include -int main(void) { - std::vector nums; - unsigned incrs{0}; - std::string line; - while (std::getline(std::cin, line)) { - nums.push_back(std::stoul(line)); +int main(void) +{ + std::vector nums; + unsigned incrs{0}; + std::string line; + while (std::getline(std::cin, line)) { + nums.push_back(std::stoul(line)); + } + assert(nums.size() >= 3); + unsigned long prev{std::numeric_limits::max()}; + for (std::size_t pos = 0; pos <= nums.size() - 3; ++pos) { + unsigned long current{0}; + for (std::size_t i = pos + 0; i < pos + 3; ++i) { + current += nums[i]; } - assert(nums.size() >= 3); - unsigned long prev{std::numeric_limits::max()}; - for (std::size_t pos = 0; pos <= nums.size() - 3; ++pos) { - unsigned long current{0}; - for (std::size_t i = pos + 0; i < pos + 3; ++i) { - current += nums[i]; - } - if (current > prev) { - ++incrs; - } - prev = current; + if (current > prev) { + ++incrs; } - std::cout << "Number of increments: " << incrs << '\n'; - return EXIT_SUCCESS; + prev = current; + } + std::cout << "Number of increments: " << incrs << '\n'; + return EXIT_SUCCESS; } diff --git a/2021/puzzle-02-01.cc b/2021/puzzle-02-01.cc index acb4a51..f2212eb 100644 --- a/2021/puzzle-02-01.cc +++ b/2021/puzzle-02-01.cc @@ -2,31 +2,31 @@ // Created by Matthew Gretton-Dann on 02/12/2021. // -#include #include +#include int main() { - using namespace std::string_literals; + using namespace std::string_literals; - static const auto fwd{"forward "s}; - static const auto down{"down "s}; - static const auto up{"up "s}; + static const auto fwd{"forward "s}; + static const auto down{"down "s}; + static const auto up{"up "s}; - std::string line; - unsigned long horiz{0}; - unsigned long depth{0}; - while (std::getline(std::cin, line)) { - if (line.size() > fwd.size() && line.substr(0,fwd.size()) == fwd) { - horiz += std::stoul(line.substr(fwd.size())); - } - if (line.size() > down.size() && line.substr(0,down.size()) == down) { - depth += std::stoul(line.substr(down.size())); - } - if (line.size() > up.size() && line.substr(0,up.size()) == up) { - depth -= std::stoul(line.substr(up.size())); - } + std::string line; + unsigned long horiz{0}; + unsigned long depth{0}; + while (std::getline(std::cin, line)) { + if (line.size() > fwd.size() && line.substr(0, fwd.size()) == fwd) { + horiz += std::stoul(line.substr(fwd.size())); } + if (line.size() > down.size() && line.substr(0, down.size()) == down) { + depth += std::stoul(line.substr(down.size())); + } + if (line.size() > up.size() && line.substr(0, up.size()) == up) { + depth -= std::stoul(line.substr(up.size())); + } + } - std::cout << "Distance * depth: " << horiz << " * " << depth << " = " << horiz * depth << '\n'; + std::cout << "Distance * depth: " << horiz << " * " << depth << " = " << horiz * depth << '\n'; } \ No newline at end of file diff --git a/2021/puzzle-02-02.cc b/2021/puzzle-02-02.cc index 91ee5dc..9a52283 100644 --- a/2021/puzzle-02-02.cc +++ b/2021/puzzle-02-02.cc @@ -2,34 +2,34 @@ // Created by Matthew Gretton-Dann on 02/12/2021. // -#include #include +#include int main() { - using namespace std::string_literals; + using namespace std::string_literals; - static const auto fwd{"forward "s}; - static const auto down{"down "s}; - static const auto up{"up "s}; + static const auto fwd{"forward "s}; + static const auto down{"down "s}; + static const auto up{"up "s}; - std::string line; - unsigned long horiz{0}; - unsigned long depth{0}; - unsigned long aim{0}; - while (std::getline(std::cin, line)) { - if (line.size() > fwd.size() && line.substr(0,fwd.size()) == fwd) { - auto dist{std::stoul(line.substr(fwd.size()))}; - horiz += dist; - depth += aim * dist; - } - if (line.size() > down.size() && line.substr(0,down.size()) == down) { - aim += std::stoul(line.substr(down.size())); - } - if (line.size() > up.size() && line.substr(0,up.size()) == up) { - aim -= std::stoul(line.substr(up.size())); - } + std::string line; + unsigned long horiz{0}; + unsigned long depth{0}; + unsigned long aim{0}; + while (std::getline(std::cin, line)) { + if (line.size() > fwd.size() && line.substr(0, fwd.size()) == fwd) { + auto dist{std::stoul(line.substr(fwd.size()))}; + horiz += dist; + depth += aim * dist; } + if (line.size() > down.size() && line.substr(0, down.size()) == down) { + aim += std::stoul(line.substr(down.size())); + } + if (line.size() > up.size() && line.substr(0, up.size()) == up) { + aim -= std::stoul(line.substr(up.size())); + } + } - std::cout << "Distance * depth: " << horiz << " * " << depth << " = " << horiz * depth << '\n'; + std::cout << "Distance * depth: " << horiz << " * " << depth << " = " << horiz * depth << '\n'; } \ No newline at end of file