diff options
author | Federico Igne <undyamon@disroot.org> | 2023-12-09 11:58:03 +0100 |
---|---|---|
committer | Federico Igne <undyamon@disroot.org> | 2023-12-09 11:58:03 +0100 |
commit | e3d8709aeac42e5c2658e97fbf0fefaab0443e7c (patch) | |
tree | 3d20f53c6569f0b29521ebed4aaebc51e2a9b4e3 /2023 | |
parent | a2215340eba265d0debdd92b68a1853c2a0a039c (diff) | |
download | aoc-e3d8709aeac42e5c2658e97fbf0fefaab0443e7c.tar.gz aoc-e3d8709aeac42e5c2658e97fbf0fefaab0443e7c.zip |
aoc(2309): Mirage Maintenance
Diffstat (limited to '2023')
-rw-r--r-- | 2023/09/Makefile | 19 | ||||
-rw-r--r-- | 2023/09/resources/input_small.txt | 3 | ||||
-rw-r--r-- | 2023/09/src/part1.cpp | 43 | ||||
-rw-r--r-- | 2023/09/src/part2.cpp | 45 |
4 files changed, 110 insertions, 0 deletions
diff --git a/2023/09/Makefile b/2023/09/Makefile new file mode 100644 index 0000000..af6a294 --- /dev/null +++ b/2023/09/Makefile | |||
@@ -0,0 +1,19 @@ | |||
1 | CXXFLAGS := -std=c++17 | ||
2 | CPPFLAGS := -I../include | ||
3 | EXE := part1 part2 | ||
4 | |||
5 | .PHONY: all clean configure | ||
6 | |||
7 | all: $(EXE) | ||
8 | |||
9 | configure: | ||
10 | bear -- $(MAKE) all | ||
11 | |||
12 | %.o: %.cpp | ||
13 | $(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $< -o $@ | ||
14 | |||
15 | clean: | ||
16 | rm -rf $(EXE) src/*.o compile_commands.json | ||
17 | |||
18 | %: src/%.o | ||
19 | $(CXX) $^ -o $@ | ||
diff --git a/2023/09/resources/input_small.txt b/2023/09/resources/input_small.txt new file mode 100644 index 0000000..539a763 --- /dev/null +++ b/2023/09/resources/input_small.txt | |||
@@ -0,0 +1,3 @@ | |||
1 | 0 3 6 9 12 15 | ||
2 | 1 3 6 10 15 21 | ||
3 | 10 13 16 21 30 45 | ||
diff --git a/2023/09/src/part1.cpp b/2023/09/src/part1.cpp new file mode 100644 index 0000000..4087486 --- /dev/null +++ b/2023/09/src/part1.cpp | |||
@@ -0,0 +1,43 @@ | |||
1 | #include <fstream> | ||
2 | #include <iostream> | ||
3 | #include <sstream> | ||
4 | #include <vector> | ||
5 | |||
6 | void solve(std::vector<int> v, int& answer) | ||
7 | { | ||
8 | for (int a = v.size() - 1; a > 0; --a) | ||
9 | { | ||
10 | answer += v[a]; | ||
11 | for (int b = 0; b < a; ++b) | ||
12 | { | ||
13 | v[b] = v[b+1] - v[b]; | ||
14 | } | ||
15 | } | ||
16 | } | ||
17 | |||
18 | int main(int argc, char* argv[]) | ||
19 | { | ||
20 | int answer{}; | ||
21 | |||
22 | std::ifstream input{ argv[1] }; | ||
23 | if (input.is_open()) | ||
24 | { | ||
25 | std::string line; | ||
26 | while (not std::getline(input,line).eof()) | ||
27 | { | ||
28 | int n; | ||
29 | std::vector<int> v{}; | ||
30 | std::istringstream sline{ line }; | ||
31 | while(sline >> n) | ||
32 | { | ||
33 | v.push_back(n); | ||
34 | } | ||
35 | |||
36 | solve(std::move(v), answer); | ||
37 | } | ||
38 | } | ||
39 | input.close(); | ||
40 | |||
41 | std::cout << answer << std::endl; | ||
42 | return 0; | ||
43 | } | ||
diff --git a/2023/09/src/part2.cpp b/2023/09/src/part2.cpp new file mode 100644 index 0000000..df1ec5a --- /dev/null +++ b/2023/09/src/part2.cpp | |||
@@ -0,0 +1,45 @@ | |||
1 | #include <numeric> | ||
2 | #include <fstream> | ||
3 | #include <iostream> | ||
4 | #include <sstream> | ||
5 | #include <vector> | ||
6 | |||
7 | void solve(std::vector<int> v, int& answer) | ||
8 | { | ||
9 | for (int a = 0; a < v.size() - 1; ++a) | ||
10 | { | ||
11 | answer += v[a]; | ||
12 | for (int b = v.size() - 1; b > a; --b) | ||
13 | { | ||
14 | v[b] = v[b-1] - v[b]; | ||
15 | } | ||
16 | } | ||
17 | } | ||
18 | |||
19 | int main(int argc, char* argv[]) | ||
20 | { | ||
21 | int answer{}; | ||
22 | |||
23 | std::ifstream input{ argv[1] }; | ||
24 | if (input.is_open()) | ||
25 | { | ||
26 | std::string line; | ||
27 | while (not std::getline(input,line).eof()) | ||
28 | { | ||
29 | int n; | ||
30 | std::vector<int> v{}; | ||
31 | std::istringstream sline{ line }; | ||
32 | while(sline >> n) | ||
33 | { | ||
34 | v.push_back(n); | ||
35 | } | ||
36 | |||
37 | solve(std::move(v), answer); | ||
38 | } | ||
39 | } | ||
40 | input.close(); | ||
41 | |||
42 | std::cout << answer << std::endl; | ||
43 | return 0; | ||
44 | } | ||
45 | |||