summaryrefslogtreecommitdiff
path: root/2023
diff options
context:
space:
mode:
authorFederico Igne <undyamon@disroot.org>2023-12-06 14:28:33 +0100
committerFederico Igne <undyamon@disroot.org>2023-12-06 14:28:33 +0100
commit325117d6949f7bf2900d5f4d6a118f3e3b6b2501 (patch)
tree80f7d4b7d4f6909f8d795d67c0f1e64da9bebd62 /2023
parent8ed68d64a9f49635209c7d6e231c6cc4d3443172 (diff)
downloadaoc-325117d6949f7bf2900d5f4d6a118f3e3b6b2501.tar.gz
aoc-325117d6949f7bf2900d5f4d6a118f3e3b6b2501.zip
aoc(2306): Wait For It
Diffstat (limited to '2023')
-rw-r--r--2023/06/Makefile18
-rw-r--r--2023/06/resources/input.txt2
-rw-r--r--2023/06/resources/input_small.txt2
-rw-r--r--2023/06/src/part1.cpp54
-rw-r--r--2023/06/src/part2.cpp50
5 files changed, 126 insertions, 0 deletions
diff --git a/2023/06/Makefile b/2023/06/Makefile
new file mode 100644
index 0000000..a3c2cea
--- /dev/null
+++ b/2023/06/Makefile
@@ -0,0 +1,18 @@
1CXXFLAGS := -std=c++17
2EXE := part1 part2
3
4.PHONY: all clean configure
5
6all: $(EXE)
7
8configure:
9 bear -- $(MAKE) all
10
11%.o: %.cpp
12 $(CXX) -c $(CXXFLAGS) $< -o $@
13
14clean:
15 rm -rf $(EXE) src/*.o compile_commands.json
16
17%: src/%.o
18 $(CXX) $^ -o $@
diff --git a/2023/06/resources/input.txt b/2023/06/resources/input.txt
new file mode 100644
index 0000000..e0996e8
--- /dev/null
+++ b/2023/06/resources/input.txt
@@ -0,0 +1,2 @@
1Time: 60 80 86 76
2Distance: 601 1163 1559 1300
diff --git a/2023/06/resources/input_small.txt b/2023/06/resources/input_small.txt
new file mode 100644
index 0000000..28f5ae9
--- /dev/null
+++ b/2023/06/resources/input_small.txt
@@ -0,0 +1,2 @@
1Time: 7 15 30
2Distance: 9 40 200
diff --git a/2023/06/src/part1.cpp b/2023/06/src/part1.cpp
new file mode 100644
index 0000000..2d50385
--- /dev/null
+++ b/2023/06/src/part1.cpp
@@ -0,0 +1,54 @@
1#include <iostream>
2#include <fstream>
3#include <sstream>
4#include <cstring>
5#include <vector>
6#include <cmath>
7
8int main(void)
9{
10 int answer{ 1 };
11 std::vector<int> time;
12 std::vector<int> distance;
13
14 std::ifstream input{ "resources/input.txt" };
15 if (input.is_open())
16 {
17 int n;
18 std::string line;
19
20 /* Time */
21 {
22 std::getline(input,line);
23 line.erase(0, std::strlen("Time:"));
24 std::istringstream sline{ line };
25 while(sline >> n)
26 {
27 time.push_back(n);
28 }
29 }
30
31 /* Distance */
32 {
33 std::getline(input,line);
34 line.erase(0, std::strlen("Distance:"));
35 std::istringstream sline{ line };
36 while(sline >> n)
37 {
38 distance.push_back(n);
39 }
40 }
41 }
42 input.close();
43
44 for (int i = 0; i < time.size(); ++i)
45 {
46 double delta = std::sqrt(time[i] * time[i] - 4 * distance[i]);
47 int from{ static_cast<int>(1 + (time[i] - delta) / 2)};
48 int to{ static_cast<int>(std::ceil((time[i] + delta) / 2)) };
49 answer *= to - from;
50 }
51
52 std::cout << answer << std::endl;
53 return 0;
54}
diff --git a/2023/06/src/part2.cpp b/2023/06/src/part2.cpp
new file mode 100644
index 0000000..db35b57
--- /dev/null
+++ b/2023/06/src/part2.cpp
@@ -0,0 +1,50 @@
1#include <iostream>
2#include <fstream>
3#include <sstream>
4#include <cstring>
5#include <vector>
6#include <cmath>
7
8int main(void)
9{
10 long time{};
11 long distance{};
12
13 std::ifstream input{ "resources/input.txt" };
14 if (input.is_open())
15 {
16 long n;
17 std::string line;
18
19 /* Time */
20 std::getline(input,line);
21 for(unsigned char c : line)
22 {
23 if (std::isdigit(c))
24 {
25 time *= 10;
26 time += c - '0';
27 }
28 }
29
30 /* Distance */
31 std::getline(input,line);
32 for(unsigned char c : line)
33 {
34 if (std::isdigit(c))
35 {
36 distance *= 10;
37 distance += c - '0';
38 }
39 }
40
41 }
42 input.close();
43
44 double delta = std::sqrt(time * time - 4 * distance);
45 long from{ static_cast<long>(1 + (time - delta) / 2)};
46 long to{ static_cast<long>(std::ceil((time + delta) / 2)) };
47
48 std::cout << to - from << std::endl;
49 return 0;
50}