From 988c5552d079ab743d31768f7f5ee720a41b07ff Mon Sep 17 00:00:00 2001 From: Thraix Date: Sun, 23 Aug 2026 12:12:53 +0200 Subject: [PATCH] Update clang-format to disallow single line for-loops --- .clang-format | 2 +- src/2021/Day08.cpp | 6 ++++-- src/2021/Day11.cpp | 9 ++++++--- src/2021/Day18.cpp | 3 ++- src/2022/Day11.cpp | 3 ++- src/2022/Day13.cpp | 3 ++- src/2022/Day22.cpp | 6 ++++-- src/2022/Day25.cpp | 3 ++- src/2023/Day03.cpp | 9 ++++++--- src/2023/Day04.cpp | 6 ++++-- src/2023/Day05.cpp | 3 ++- src/2023/Day06.cpp | 3 ++- src/2023/Day09.cpp | 3 ++- src/2024/Day03.cpp | 3 ++- src/2024/Day07.cpp | 3 ++- src/2024/Day11.cpp | 9 ++++++--- src/2024/Day15.cpp | 3 ++- src/2024/Day16.cpp | 3 ++- src/2024/Day23.cpp | 6 ++++-- src/common/lib/Helper.h | 6 ++++-- src/common/lib/Input.h | 24 ++++++++++++++++-------- src/common/lib/JsonUtil.cpp | 3 ++- 22 files changed, 79 insertions(+), 40 deletions(-) diff --git a/.clang-format b/.clang-format index 0dd9657..de03ecf 100644 --- a/.clang-format +++ b/.clang-format @@ -81,7 +81,7 @@ AllowShortEnumsOnASingleLine: true AllowShortFunctionsOnASingleLine: false AllowShortIfStatementsOnASingleLine: Never AllowShortLambdasOnASingleLine: All -AllowShortLoopsOnASingleLine: true +AllowShortLoopsOnASingleLine: false AllowShortNamespacesOnASingleLine: false AlwaysBreakAfterDefinitionReturnType: None AlwaysBreakBeforeMultilineStrings: true diff --git a/src/2021/Day08.cpp b/src/2021/Day08.cpp index 3483ddc..83b928a 100644 --- a/src/2021/Day08.cpp +++ b/src/2021/Day08.cpp @@ -28,7 +28,8 @@ namespace y2021::day08 std::string s; ss >> s; std::set set; - for (char c : s) set.emplace(c); + for (char c : s) + set.emplace(c); signal.numbers.emplace_back(set); } char c; @@ -38,7 +39,8 @@ namespace y2021::day08 std::string s; ss >> s; std::set set; - for (char c : s) set.emplace(c); + for (char c : s) + set.emplace(c); signal.display.emplace_back(set); } signals.emplace_back(signal); diff --git a/src/2021/Day11.cpp b/src/2021/Day11.cpp index c5fb8df..0cf0a40 100644 --- a/src/2021/Day11.cpp +++ b/src/2021/Day11.cpp @@ -5,7 +5,8 @@ namespace y2021::day11 std::set CalculateFlashing(Array2D& array) { - for (auto& val : array) val++; + for (auto& val : array) + val++; std::set flashed; bool hasChanged = true; while (hasChanged) @@ -59,7 +60,8 @@ namespace y2021::day11 { std::set flashed = CalculateFlashing(array); count += flashed.size(); - for (auto& flash : flashed) array[flash] = 0; + for (auto& flash : flashed) + array[flash] = 0; } return count; } @@ -74,7 +76,8 @@ namespace y2021::day11 std::set flashed = CalculateFlashing(array); if (flashed.size() == array.width * array.height) return count; - for (auto& flash : flashed) array[flash] = 0; + for (auto& flash : flashed) + array[flash] = 0; count++; } return count; diff --git a/src/2021/Day18.cpp b/src/2021/Day18.cpp index b0b07a5..a764b8a 100644 --- a/src/2021/Day18.cpp +++ b/src/2021/Day18.cpp @@ -272,7 +272,8 @@ namespace y2021::day18 while (changed) { changed = false; - while (node.Explode()) changed = true; + while (node.Explode()) + changed = true; if (node.Split()) changed = true; } diff --git a/src/2022/Day11.cpp b/src/2022/Day11.cpp index 31aeb23..60e7dd5 100644 --- a/src/2022/Day11.cpp +++ b/src/2022/Day11.cpp @@ -68,7 +68,8 @@ namespace y$1::day11 std::getline(input, str); std::stringstream ss{str}; ss >> "Starting items: "; - while (std::getline(ss, str, ',')) monkey.items.emplace_back(std::stoi(str)); + while (std::getline(ss, str, ',')) + monkey.items.emplace_back(std::stoi(str)); } { // operator std::getline(input, str); diff --git a/src/2022/Day13.cpp b/src/2022/Day13.cpp index 902e0fa..ce30a9d 100644 --- a/src/2022/Day13.cpp +++ b/src/2022/Day13.cpp @@ -77,7 +77,8 @@ namespace y$1::day13 Node node; node.nodes = std::stoi(str.substr(index)); nodes.emplace_back(node); - while (str[index] >= '0' && str[index] <= '9') index++; + while (str[index] >= '0' && str[index] <= '9') + index++; } else { diff --git a/src/2022/Day22.cpp b/src/2022/Day22.cpp index 60bb9c1..a93a0fa 100644 --- a/src/2022/Day22.cpp +++ b/src/2022/Day22.cpp @@ -131,7 +131,8 @@ namespace y$1::day22 if (input.directions[i] >= '0' && input.directions[i] <= '9') { int amount = std::stoi(&input.directions[i]); - while (input.directions[i] >= '0' && input.directions[i] <= '9') i++; + while (input.directions[i] >= '0' && input.directions[i] <= '9') + i++; for (int j = 0; j < amount; j++) { @@ -219,7 +220,8 @@ namespace y$1::day22 if (input.directions[i] >= '0' && input.directions[i] <= '9') { int amount = std::stoi(&input.directions[i]); - while (input.directions[i] >= '0' && input.directions[i] <= '9') i++; + while (input.directions[i] >= '0' && input.directions[i] <= '9') + i++; for (int j = 0; j < amount; j++) { diff --git a/src/2022/Day25.cpp b/src/2022/Day25.cpp index 1da30f4..78e54ba 100644 --- a/src/2022/Day25.cpp +++ b/src/2022/Day25.cpp @@ -39,7 +39,8 @@ namespace y$1::day25 // Convert to base 5 std::string base5 = ""; int64_t numbers = log(base10) / log(5) + 1; - for (int i = 0; i < numbers + 1; i++) base5.push_back('0'); + for (int i = 0; i < numbers + 1; i++) + base5.push_back('0'); int64_t base5Max = Pow(5, numbers - 2); for (int i = 1; i < base5.size(); i++) diff --git a/src/2023/Day03.cpp b/src/2023/Day03.cpp index 4271c13..6f51fc6 100644 --- a/src/2023/Day03.cpp +++ b/src/2023/Day03.cpp @@ -55,7 +55,8 @@ namespace y2023::day03 if (Helper::IsDigit(array.Get(x, y))) { Index2D numberStart = Index2D{x, y}; - while (numberStart.x >= 0 && Helper::IsDigit(array.Get(numberStart))) numberStart.x--; + while (numberStart.x >= 0 && Helper::IsDigit(array.Get(numberStart))) + numberStart.x--; numberStart.x++; int number = GetNumber(array, numberStart); @@ -64,7 +65,8 @@ namespace y2023::day03 else return firstNum * number; - while (x < array.width && Helper::IsDigit(array.Get(x, y))) x++; + while (x < array.width && Helper::IsDigit(array.Get(x, y))) + x++; x--; } } @@ -88,7 +90,8 @@ namespace y2023::day03 if (Helper::IsDigit(input.Get(x, y))) { int i = 1; - while (x + i < input.width && Helper::IsDigit(input.Get(x + i, y))) i++; + while (x + i < input.width && Helper::IsDigit(input.Get(x + i, y))) + i++; if (HasAdjecent(input, Index2D{x, y}, i)) { sum += GetNumber(input, Index2D{x, y}); diff --git a/src/2023/Day04.cpp b/src/2023/Day04.cpp index 2d0c8f0..98fe4f8 100644 --- a/src/2023/Day04.cpp +++ b/src/2023/Day04.cpp @@ -28,12 +28,14 @@ namespace y2023::day04 std::stringstream ss{str}; ss >> "Card " >> i >> ":"; - while (ss >> i) game.winning.emplace_back(i); + while (ss >> i) + game.winning.emplace_back(i); ss.clear(); // clear the failbit to be able to read more data ss >> "|"; - while (ss >> i) game.numbers.emplace(i); + while (ss >> i) + game.numbers.emplace(i); games.emplace_back(game); } diff --git a/src/2023/Day05.cpp b/src/2023/Day05.cpp index c89b335..5514855 100644 --- a/src/2023/Day05.cpp +++ b/src/2023/Day05.cpp @@ -82,7 +82,8 @@ namespace y2023::day05 std::stringstream ss{str}; int64_t i; ss >> "seeds: "; - while (ss >> i) plantation.seeds.emplace_back(i); + while (ss >> i) + plantation.seeds.emplace_back(i); std::getline(input, str); while (std::getline(input, str)) { diff --git a/src/2023/Day06.cpp b/src/2023/Day06.cpp index eafd7a5..546211c 100644 --- a/src/2023/Day06.cpp +++ b/src/2023/Day06.cpp @@ -18,7 +18,8 @@ namespace y2023::day06 std::stringstream ss{str}; int i; ss >> "Time: "; - while (ss >> i) games.emplace_back(i, 0); + while (ss >> i) + games.emplace_back(i, 0); std::getline(input, str); std::stringstream ss2{str}; diff --git a/src/2023/Day09.cpp b/src/2023/Day09.cpp index ea23213..d798888 100644 --- a/src/2023/Day09.cpp +++ b/src/2023/Day09.cpp @@ -18,7 +18,8 @@ namespace y2023::day09 std::vector numbers; std::stringstream ss{str}; int i; - while (ss >> i) numbers.emplace_back(i); + while (ss >> i) + numbers.emplace_back(i); ints.emplace_back(numbers); } diff --git a/src/2024/Day03.cpp b/src/2024/Day03.cpp index 5da13bd..1b19607 100644 --- a/src/2024/Day03.cpp +++ b/src/2024/Day03.cpp @@ -26,7 +26,8 @@ namespace y2024::day03 { int32_t sum = 0; std::vector matches = Helper::GetAllRegexMatches(input, "mul\\([0-9]*,[0-9]*\\)"); - for (auto& str : matches) sum += Mult(str); + for (auto& str : matches) + sum += Mult(str); return sum; } diff --git a/src/2024/Day07.cpp b/src/2024/Day07.cpp index c565267..7492aad 100644 --- a/src/2024/Day07.cpp +++ b/src/2024/Day07.cpp @@ -50,7 +50,8 @@ namespace y2024::day07 int64_t Concat(int64_t i1, int64_t i2) { int64_t base = 10; - while (i2 >= base) base *= 10; + while (i2 >= base) + base *= 10; return i1 * base + i2; } diff --git a/src/2024/Day11.cpp b/src/2024/Day11.cpp index 8610ae0..9195045 100644 --- a/src/2024/Day11.cpp +++ b/src/2024/Day11.cpp @@ -13,7 +13,8 @@ namespace y2024::day11 READ_INPUT(input) { Stones stones; - for (auto i : Input::ReadInt64s(input, ' ')) stones[i]++; + for (auto i : Input::ReadInt64s(input, ' ')) + stones[i]++; return stones; } @@ -33,7 +34,8 @@ namespace y2024::day11 { int64_t pow = 1; - for (int j = 0; j < i; j++) pow *= 10; + for (int j = 0; j < i; j++) + pow *= 10; return pow; } @@ -74,7 +76,8 @@ namespace y2024::day11 { std::map cpy = stones; - for (int i = 0; i < iterations; i++) Step(cpy); + for (int i = 0; i < iterations; i++) + Step(cpy); int64_t sum = 0; for (auto& [cur, count] : cpy) diff --git a/src/2024/Day15.cpp b/src/2024/Day15.cpp index bd62b4b..f72accd 100644 --- a/src/2024/Day15.cpp +++ b/src/2024/Day15.cpp @@ -21,7 +21,8 @@ namespace y2024::day15 map.map = Input::ReadArray2D(input); std::string str; - while (std::getline(input, str)) map.input += str; + while (std::getline(input, str)) + map.input += str; return map; } diff --git a/src/2024/Day16.cpp b/src/2024/Day16.cpp index c3860ad..e9dd0b3 100644 --- a/src/2024/Day16.cpp +++ b/src/2024/Day16.cpp @@ -48,7 +48,8 @@ namespace y2024::day16 Helper::DijkstrasAllVisited(input, std::pair{input.Find('S'), Index2D{1, 0}}, Branch, Goal); std::set solutions; - for (const auto& [pos, dir] : allVisited) solutions.emplace(pos); + for (const auto& [pos, dir] : allVisited) + solutions.emplace(pos); return solutions.size(); } diff --git a/src/2024/Day23.cpp b/src/2024/Day23.cpp index 6b9d134..2b0ccb3 100644 --- a/src/2024/Day23.cpp +++ b/src/2024/Day23.cpp @@ -92,13 +92,15 @@ namespace y2024::day23 FindLargest(input, computers, largest); } - for (const auto& s : largest) std::cout << s << ","; + for (const auto& s : largest) + std::cout << s << ","; std::cout << std::endl; // Some arbitrary solution number to have some form of verification in the code. // Sum all letters of the computers (with a=0, b=1, etc) int64_t sum = 0; - for (const auto& s : largest) sum += (s[0] - 'a') + (s[1] - 'a'); + for (const auto& s : largest) + sum += (s[0] - 'a') + (s[1] - 'a'); return sum; } diff --git a/src/common/lib/Helper.h b/src/common/lib/Helper.h index f131214..76af015 100644 --- a/src/common/lib/Helper.h +++ b/src/common/lib/Helper.h @@ -313,7 +313,8 @@ struct Helper { std::map visited; std::multimap> open; - for (int i = 0; i < initial.size(); i++) open.emplace(0, std::pair{0, initial[i]}); + for (int i = 0; i < initial.size(); i++) + open.emplace(0, std::pair{0, initial[i]}); while (!open.empty()) { auto it = open.begin(); @@ -355,7 +356,8 @@ struct Helper { std::map visited; std::multimap>> open; - for (int i = 0; i < initial.size(); i++) open.emplace(0, std::pair>{0, {initial[i]}}); + for (int i = 0; i < initial.size(); i++) + open.emplace(0, std::pair>{0, {initial[i]}}); while (!open.empty()) { const auto& [heuristicVal, state] = *open.begin(); diff --git a/src/common/lib/Input.h b/src/common/lib/Input.h index bfe89b3..59ba6c3 100644 --- a/src/common/lib/Input.h +++ b/src/common/lib/Input.h @@ -13,9 +13,11 @@ static std::istream& operator>>(std::istream& stream, char const* pattern) { char c; - while (stream.peek() == ' ') stream.get(c); + while (stream.peek() == ' ') + stream.get(c); - while (*pattern != '\0' && stream && *pattern == stream.peek() && stream.get(c)) ++pattern; + while (*pattern != '\0' && stream && *pattern == stream.peek() && stream.get(c)) + ++pattern; if (*pattern != '\0') stream.setstate(std::ios::failbit); @@ -37,7 +39,8 @@ struct Input break; width = (int)str.length(); height++; - for (auto c : str) data.emplace_back(c); + for (auto c : str) + data.emplace_back(c); } return Array2D(width, height, data); } @@ -52,7 +55,8 @@ struct Input { width = (int)str.length(); height++; - for (auto c : str) data.emplace_back(c - '0'); + for (auto c : str) + data.emplace_back(c - '0'); } return Array2D(width, height, data); } @@ -93,7 +97,8 @@ struct Input { std::vector ints; std::string str; - while (getline(input, str, delimiter)) ints.emplace_back(std::atoi(str.c_str())); + while (getline(input, str, delimiter)) + ints.emplace_back(std::atoi(str.c_str())); return ints; } @@ -101,7 +106,8 @@ struct Input { std::vector ints; std::string str; - while (getline(input, str, delimiter)) ints.emplace_back(std::atoll(str.c_str())); + while (getline(input, str, delimiter)) + ints.emplace_back(std::atoll(str.c_str())); return ints; } @@ -109,7 +115,8 @@ struct Input { std::vector strings; std::string str; - while (getline(input, str)) strings.emplace_back(str); + while (getline(input, str)) + strings.emplace_back(str); return strings; } @@ -117,7 +124,8 @@ struct Input { std::string result; std::string str; - while (getline(input, str)) result += str + '\n'; + while (getline(input, str)) + result += str + '\n'; if (!result.empty()) result.pop_back(); return result; diff --git a/src/common/lib/JsonUtil.cpp b/src/common/lib/JsonUtil.cpp index c2408a1..37be886 100644 --- a/src/common/lib/JsonUtil.cpp +++ b/src/common/lib/JsonUtil.cpp @@ -97,7 +97,8 @@ bool JsonUtil::ReadString(const std::string& str, uint64_t& pos, Json& json) uint64_t stringStart = pos; while (true) { - while (pos < str.size() && str[pos] != '\"') pos++; + while (pos < str.size() && str[pos] != '\"') + pos++; if (pos >= str.size() || str[pos - 1] != '\\') break; }