aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--2022/src/day01.cpp73
-rw-r--r--makefile7
2 files changed, 79 insertions, 1 deletions
diff --git a/2022/src/day01.cpp b/2022/src/day01.cpp
new file mode 100644
index 0000000..ece0309
--- /dev/null
+++ b/2022/src/day01.cpp
@@ -0,0 +1,73 @@
1#include <fstream>
2#include <iostream>
3#include <numeric>
4#include <sstream>
5#include <string>
6#include <vector>
7using namespace std;
8
9string
10read_file(string_view filename)
11{
12 fstream input{ filename };
13 return { istreambuf_iterator<char>{ input }, {} };
14}
15
16vector<string>
17split(string_view line, string_view delimiter)
18{
19 string::size_type pos_start = 0;
20 string::size_type pos_end = 0;
21
22 vector<string> result;
23
24 while ( (pos_end = line.find(delimiter, pos_start)) != string::npos ) {
25 result.emplace_back(line.substr(pos_start, pos_end - pos_start));
26 pos_start = pos_end + delimiter.length();
27 }
28
29 if ( pos_start != line.size() ) {
30 result.emplace_back(line.substr(pos_start));
31 }
32
33 return result;
34}
35
36vector<long>
37split(const string& line)
38{
39 stringstream input{ line };
40 return { istream_iterator<long>(input), {} };
41}
42
43void
44part1(const vector<string>& parts)
45{
46 long max_so_far = 0;
47 for ( const auto& part: parts ) {
48 const auto nums = split(part);
49 max_so_far = max(accumulate(begin(nums), end(nums), 0L), max_so_far);
50 }
51 cout << max_so_far << endl;
52}
53
54void
55part2(const vector<string>& parts)
56{
57 vector<long> all;
58 for ( const auto& part: parts ) {
59 const auto nums = split(part);
60 all.emplace_back(accumulate(begin(nums), end(nums), 0L));
61 }
62 sort(begin(all), end(all), greater<>());
63 cout << all[0] + all[1] + all[2] << endl;
64}
65
66int
67main()
68{
69 const auto line = read_file("data/day01.txt");
70 const auto parts = split(line, "\n\n");
71 part1(parts);
72 part2(parts);
73}
diff --git a/makefile b/makefile
index e1318c1..494ced4 100644
--- a/makefile
+++ b/makefile
@@ -1,6 +1,7 @@
1include config.mk 1include config.mk
2 2
3all: $(patsubst 2020/src/%.cpp,2020/bin/%,$(wildcard 2020/src/*.cpp)) \ 3all: $(patsubst 2020/src/%.cpp,2020/bin/%,$(wildcard 2020/src/*.cpp)) \
4 $(patsubst 2022/src/%.cpp,2022/bin/%,$(wildcard 2022/src/*.cpp)) \
4 $(patsubst 2023/src/%.cpp,2023/bin/%,$(wildcard 2023/src/*.cpp)) 5 $(patsubst 2023/src/%.cpp,2023/bin/%,$(wildcard 2023/src/*.cpp))
5 6
6%/bin: 7%/bin:
@@ -10,6 +11,10 @@ all: $(patsubst 2020/src/%.cpp,2020/bin/%,$(wildcard 2020/src/*.cpp)) \
102020/bin/%: 2020/src/%.cpp | 2020/bin 112020/bin/%: 2020/src/%.cpp | 2020/bin
11 c++ $(CPPFLAGS) $^ -o $@ 12 c++ $(CPPFLAGS) $^ -o $@
12 13
14# 2022
152022/bin/%: 2022/src/%.cpp | 2022/bin
16 c++ $(CPPFLAGS) $^ -o $@
17
13# 2023 18# 2023
142023/bin/%: 2023/src/%.cpp | 2023/bin 192023/bin/%: 2023/src/%.cpp | 2023/bin
15 c++ $(CPPFLAGS) $^ -o $@ 20 c++ $(CPPFLAGS) $^ -o $@
@@ -18,6 +23,6 @@ all: $(patsubst 2020/src/%.cpp,2020/bin/%,$(wildcard 2020/src/*.cpp)) \
18 c++ $(CPPFLAGS) -Wno-sign-conversion -I/usr/local/include $^ -L/usr/local/lib -lz3 -o $@ 23 c++ $(CPPFLAGS) -Wno-sign-conversion -I/usr/local/include $^ -L/usr/local/lib -lz3 -o $@
19 24
20clean: 25clean:
21 rm -rf 2020/bin 2023/bin 26 rm -rf 2020/bin 2022/bin 2023/bin
22 27
23.PHONY: all clean 28.PHONY: all clean