Skip to content

Commit ea8dd33

Browse files
committed
clang-format
1 parent 4131d19 commit ea8dd33

File tree

14 files changed

+342
-250
lines changed

14 files changed

+342
-250
lines changed

.clang-format

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# https://clang.llvm.org/docs/ClangFormatStyleOptions.html
2+
3+
Standard: Cpp11
4+
BasedOnStyle: LLVM
5+
IndentWidth: 4
6+
ColumnLimit: 0
7+
AccessModifierOffset: -4
8+
UseTab: Never
9+
LineEnding: LF
10+
NamespaceIndentation: All
11+
BreakBeforeBraces: Custom
12+
BraceWrapping:
13+
AfterEnum: true
14+
AfterStruct: true
15+
AfterClass: true
16+
SplitEmptyFunction: true
17+
AfterControlStatement: false
18+
AfterNamespace: false
19+
AfterFunction: true
20+
AfterUnion: true
21+
AfterExternBlock: false
22+
BeforeCatch: false
23+
BeforeElse: false
24+
SplitEmptyRecord: true
25+
SplitEmptyNamespace: true

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Made for fun 😎 and to practice Rust. Many thanks to [Eric Wastl](https://twit
1515
Puzzle | Stars | Languages
1616
---------------------------------------------------------------------- | ----- | -----------
1717
[Day 1: Historian Hysteria](https://adventofcode.com/2024/day/1) | ⭐⭐ | [![Rust](./scripts/assets/rust.png)](src/year2024/day1/day1.rs) [![Python](./scripts/assets/python.png)](src/year2024/day1/day1.py) [![C](./scripts/assets/c.png)](src/year2024/day1/day1.c) [![C++](./scripts/assets/cpp.png)](src/year2024/day1/day1.cpp) [![Go](./scripts/assets/go.png)](src/year2024/day1/day1.go) [![Ruby](./scripts/assets/ruby.png)](src/year2024/day1/day1.rb) [![Perl](./scripts/assets/perl.png)](src/year2024/day1/day1.pl) [![Lua](./scripts/assets/lua.png)](src/year2024/day1/day1.lua) [![JS](./scripts/assets/javascript.png)](src/year2024/day1/day1.js) [![Bash](./scripts/assets/bash.png)](src/year2024/day1/day1.sh) [![Swift](./scripts/assets/swift.png)](src/year2024/day1/day1.swift) [![Java](./scripts/assets/java.png)](src/year2024/day1/day1.java) [![C#](./scripts/assets/csharp.png)](src/year2024/day1/day1.cs) [![SQLite](./scripts/assets/sqlite.png)](src/year2024/day1/day1.sql)
18-
[Day 2: Red-Nosed Reports](https://adventofcode.com/2024/day/2) | ⭐⭐ | [![Rust](./scripts/assets/rust.png)](src/year2024/day2/day2.rs) [![Python](./scripts/assets/python.png)](src/year2024/day2/day2.py) [![Go](./scripts/assets/go.png)](src/year2024/day2/day2.go)
18+
[Day 2: Red-Nosed Reports](https://adventofcode.com/2024/day/2) | ⭐⭐ | [![Rust](./scripts/assets/rust.png)](src/year2024/day2/day2.rs) [![Python](./scripts/assets/python.png)](src/year2024/day2/day2.py) [![C](./scripts/assets/c.png)](src/year2024/day2/day2.c) [![Go](./scripts/assets/go.png)](src/year2024/day2/day2.go)
1919
[Day 3: Mull It Over](https://adventofcode.com/2024/day/3) | ⭐⭐ | [![Rust](./scripts/assets/rust.png)](src/year2024/day3/day3.rs) [![Python](./scripts/assets/python.png)](src/year2024/day3/day3.py) [![Go](./scripts/assets/go.png)](src/year2024/day3/day3.go) [![Perl](./scripts/assets/perl.png)](src/year2024/day3/day3.pl)
2020
[Day 4: Ceres Search](https://adventofcode.com/2024/day/4) | ⭐⭐ | [![Rust](./scripts/assets/rust.png)](src/year2024/day4/day4.rs) [![Python](./scripts/assets/python.png)](src/year2024/day4/day4.py) [![Go](./scripts/assets/go.png)](src/year2024/day4/day4.go)
2121
[Day 5: Print Queue](https://adventofcode.com/2024/day/5) | ⭐⭐ | [![Rust](./scripts/assets/rust.png)](src/year2024/day5/day5.rs) [![Go](./scripts/assets/go.png)](src/year2024/day5/day5.go)

src/year2018/day9_c/day9.c

Lines changed: 14 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,9 @@
1-
// cc -o day9 -Wall -Wextra -DSTANDALONE -O2 day9.c
1+
// cc -std=c11 -o day9 -Wall -Wextra -DSTANDALONE -O2 day9.c
22

3+
#include <inttypes.h>
34
#include <stdio.h>
45
#include <stdlib.h>
56
#include <string.h>
6-
#include <inttypes.h>
7-
#include <stdbool.h>
8-
#include <time.h>
9-
#include <stdarg.h>
10-
#include <unistd.h>
11-
#include <signal.h>
12-
#include <sys/time.h>
13-
#include <sys/types.h>
14-
#include <sys/stat.h>
15-
#include <assert.h>
167

178
struct Marble
189
{
@@ -26,13 +17,10 @@ struct Marble *ll_insert(struct Marble *cur, uint32_t value)
2617

2718
m->value = value;
2819

29-
if (cur == NULL)
30-
{
20+
if (cur == NULL) {
3121
m->next = m;
3222
m->prev = m;
33-
}
34-
else
35-
{
23+
} else {
3624
m->next = cur->next;
3725
m->prev = cur;
3826

@@ -66,19 +54,15 @@ uint32_t solve(uint32_t elves, uint32_t points)
6654

6755
current = ll_insert(NULL, 0);
6856

69-
for (uint32_t i = 1; i < points; ++i)
70-
{
57+
for (uint32_t i = 1; i < points; ++i) {
7158

72-
if (i % 23 == 0)
73-
{
59+
if (i % 23 == 0) {
7460
for (int k = 0; k < 7; ++k)
7561
current = current->prev;
7662
scores[i % elves] += i + current->value;
7763

7864
current = ll_remove(current);
79-
}
80-
else
81-
{
65+
} else {
8266
for (int k = 0; k < 1; ++k)
8367
current = current->next;
8468

@@ -87,8 +71,7 @@ uint32_t solve(uint32_t elves, uint32_t points)
8771
}
8872

8973
uint32_t max = scores[0];
90-
for (uint32_t i = 1; i < elves; i++)
91-
{
74+
for (uint32_t i = 1; i < elves; i++) {
9275
if (max < scores[i])
9376
max = scores[i];
9477
}
@@ -106,7 +89,6 @@ uint32_t c_solve(uint32_t elves, uint32_t points)
10689
return solve(elves, points);
10790
}
10891

109-
11092
#ifdef STANDALONE
11193

11294
void tests()
@@ -118,17 +100,13 @@ void tests()
118100
{30, 5807, 37305}};
119101
int ret = EXIT_SUCCESS;
120102

121-
for (size_t i = 0; i < sizeof(testcases) / sizeof(testcases[0]); ++i)
122-
{
103+
for (size_t i = 0; i < sizeof(testcases) / sizeof(testcases[0]); ++i) {
123104
const uint32_t *t = testcases[i];
124105
uint32_t res = solve(t[0], t[1]);
125106
printf("solve(%2u, %4u) = %6u ", t[0], t[1], res);
126-
if (res == t[2])
127-
{
107+
if (res == t[2]) {
128108
printf("\033[32mOK\033[0m\n");
129-
}
130-
else
131-
{
109+
} else {
132110
printf("\033[31mFAIL\033[0m (expected: %u)\n", t[2]);
133111
ret = EXIT_FAILURE;
134112
}
@@ -142,20 +120,17 @@ int main(int argc, char *argv[])
142120
uint32_t elves, points;
143121
FILE *input;
144122

145-
if (argc >= 2 && strcmp(argv[1], "-t") == 0)
146-
{
123+
if (argc >= 2 && strcmp(argv[1], "-t") == 0) {
147124
tests();
148125
return EXIT_SUCCESS;
149126
}
150127

151128
input = fopen(argc <= 1 ? "input.txt" : argv[1], "r");
152-
if (input == NULL)
153-
{
129+
if (input == NULL) {
154130
printf("cannot read input\n");
155131
return EXIT_FAILURE;
156132
}
157-
if (fscanf(input, "%u players; last marble is worth %u points", &elves, &points) != 2)
158-
{
133+
if (fscanf(input, "%u players; last marble is worth %u points", &elves, &points) != 2) {
159134
printf("cannot parse input\n");
160135
return EXIT_FAILURE;
161136
}

src/year2019/day1/day1.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// [Day 1: The Tyranny of the Rocket Equation](https://adventofcode.com/2019/day/1)
2-
// cc -o day1 -Wall -O2 day1.c
2+
// cc -std=c11 -o day1 -Wall -O2 day1.c
33

44
#include <stdio.h>
55

@@ -8,13 +8,11 @@ int main(int argc, char *argv[])
88
FILE *f = fopen((argc >= 2) ? argv[1] : "input.txt", "r");
99

1010
int mass, part1 = 0, part2 = 0;
11-
while (fscanf(f, "%d", &mass) == 1)
12-
{
11+
while (fscanf(f, "%d", &mass) == 1) {
1312
part1 += mass / 3 - 2;
1413

1514
int fuel = mass;
16-
while ((fuel = fuel / 3 - 2) > 0)
17-
{
15+
while ((fuel = fuel / 3 - 2) > 0) {
1816
part2 += fuel;
1917
}
2018
}

src/year2019/day16/day16.c

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
// cc -o day16 -Wall -O2 day16.c
1+
// cc -std=c11 -o day16 -Wall -O2 day16.c
22
// https://adventofcode.com/2019/day/16
33

44
#pragma GCC diagnostic ignored "-Wunused-result"
55

6+
#include <inttypes.h>
67
#include <stdio.h> // well... is it really necessary ?
78
#include <stdlib.h>
89
#include <string.h>
9-
#include <inttypes.h>
1010

1111
int main(int argc, char *argv[])
1212
{
@@ -45,14 +45,12 @@ int main(int argc, char *argv[])
4545
memcpy(p, data, size); // no check
4646
for (int k = 0; k < 100; k++) // ++i or i++ ? I should decide !
4747
{
48-
for (int n = 0; n < size; n++)
49-
{
48+
for (int n = 0; n < size; n++) {
5049
int s = 0;
51-
for (int i = 0; i < size; ++i)
52-
{
50+
for (int i = 0; i < size; ++i) {
5351
s += pattern[((i + 1) / (n + 1)) % 4] * p[i]; // mix between int and int8_t
54-
} // and only a SAST could verify there is no overflow here
55-
t[n] = abs(s) % 10; // other silent cast int to int8_t, even it is legitimate
52+
} // and only a SAST could verify there is no overflow here
53+
t[n] = abs(s) % 10; // other silent cast int to int8_t, even it is legitimate
5654
}
5755
memcpy(p, t, size);
5856
}
@@ -72,8 +70,7 @@ int main(int argc, char *argv[])
7270
// printf("%d\n", n);
7371
p = malloc(n); // check, element type, cast ?
7472
t = malloc(n);
75-
for (int i = 0; i < n; ++i)
76-
{ // accolade or not accolade for a only one statement ?
73+
for (int i = 0; i < n; ++i) { // accolade or not accolade for a only one statement ?
7774
p[i] = data[(i + offset) % size]; // I should remember that data is at most size byte wide
7875
}
7976
for (int k = 0; k < 100; ++k) // naming this variable is useless

src/year2022/day11/day11.cpp

Lines changed: 23 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
#include <vector>
1+
#include <algorithm>
2+
#include <cassert>
3+
#include <cstdint>
4+
#include <cstring>
5+
#include <fstream>
26
#include <iostream>
37
#include <list>
4-
#include <fstream>
58
#include <sstream>
69
#include <stdexcept>
7-
#include <cstring>
8-
#include <cassert>
9-
#include <cstdint>
10-
#include <algorithm>
10+
#include <vector>
1111

1212
struct Operation
1313
{
@@ -17,8 +17,7 @@ struct Operation
1717
uint64_t calc(uint64_t n) const
1818
{
1919
// WARNING no overflow checks in C++
20-
switch (this->opcode)
21-
{
20+
switch (this->opcode) {
2221
case '^':
2322
return n * n;
2423
case '+':
@@ -46,50 +45,39 @@ void solve(const std::vector<Monkey> &monkeys_orig, int rounds)
4645
std::vector<Monkey> monkeys = monkeys_orig;
4746

4847
uint64_t modulus = 1;
49-
for (const auto &monkey : monkeys)
50-
{
48+
for (const auto &monkey : monkeys) {
5149
modulus *= monkey.divisible_by;
5250
}
5351

54-
for (int round = 0; round < rounds; ++round)
55-
{
56-
for (auto &monkey : monkeys)
57-
{
52+
for (int round = 0; round < rounds; ++round) {
53+
for (auto &monkey : monkeys) {
5854
// Nota: this loop can run forever if if_true/if_false refers the current monkey and C++ allows it.
5955
// In Rust it's not possible because of the borrowing that forces to copy the items vector
60-
while (!monkey.items.empty())
61-
{
56+
while (!monkey.items.empty()) {
6257
monkey.inspections += 1;
6358

6459
uint64_t item = monkey.items.front();
6560
monkey.items.pop_front();
6661

6762
uint64_t worry_level = monkey.operation.calc(item);
6863

69-
if (rounds == 20)
70-
{
64+
if (rounds == 20) {
7165
worry_level = worry_level / 3;
72-
}
73-
else
74-
{
66+
} else {
7567
worry_level = worry_level % modulus;
7668
}
7769

78-
if (worry_level % monkey.divisible_by == 0)
79-
{
70+
if (worry_level % monkey.divisible_by == 0) {
8071
monkeys[monkey.if_true].items.push_back(worry_level);
81-
}
82-
else
83-
{
72+
} else {
8473
monkeys[monkey.if_false].items.push_back(worry_level);
8574
}
8675
}
8776
}
8877
}
8978

9079
std::sort(monkeys.begin(), monkeys.end(),
91-
[](const Monkey &a, const Monkey &b) -> bool
92-
{
80+
[](const Monkey &a, const Monkey &b) -> bool {
9381
return a.inspections > b.inspections;
9482
});
9583
uint64_t monkey_business_level = (uint64_t)monkeys[0].inspections * (uint64_t)monkeys[1].inspections;
@@ -104,13 +92,11 @@ void read_data(const char *filename, std::vector<Monkey> &monkeys)
10492

10593
f.open(filename);
10694

107-
if (!f.is_open())
108-
{
95+
if (!f.is_open()) {
10996
throw std::logic_error("bad filename");
11097
}
11198

112-
while (true)
113-
{
99+
while (true) {
114100
Monkey monkey{
115101
.items = std::list<uint64_t>(),
116102
.operation = Operation{.opcode = 0, .param = 0},
@@ -129,31 +115,23 @@ void read_data(const char *filename, std::vector<Monkey> &monkeys)
129115
line = line.substr(strlen(" Starting items: "));
130116
std::string item;
131117
std::stringstream ss(line);
132-
while (std::getline(ss, item, ','))
133-
{
118+
while (std::getline(ss, item, ',')) {
134119
monkey.items.push_back(std::stoul(item));
135120
}
136121

137122
// equally tedious parsing
138123
std::getline(f, line);
139124
assert(line.find(" Operation: new = ") == 0);
140125
line = line.substr(strlen(" Operation: new = "));
141-
if (line == "old * old")
142-
{
126+
if (line == "old * old") {
143127
monkey.operation.opcode = '^';
144-
}
145-
else if (line.find("old + ") == 0)
146-
{
128+
} else if (line.find("old + ") == 0) {
147129
monkey.operation.opcode = '+';
148130
monkey.operation.param = std::stoul(line.substr(strlen("old + ")));
149-
}
150-
else if (line.find("old * ") == 0)
151-
{
131+
} else if (line.find("old * ") == 0) {
152132
monkey.operation.opcode = '*';
153133
monkey.operation.param = std::stoul(line.substr(strlen("old * ")));
154-
}
155-
else
156-
{
134+
} else {
157135
throw std::logic_error("unknown operation");
158136
}
159137

0 commit comments

Comments
 (0)