aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--2015/src/day01.cpp54
-rw-r--r--makefile7
2 files changed, 60 insertions, 1 deletions
diff --git a/2015/src/day01.cpp b/2015/src/day01.cpp
new file mode 100644
index 0000000..5d6d482
--- /dev/null
+++ b/2015/src/day01.cpp
@@ -0,0 +1,54 @@
1#include <fstream>
2#include <iostream>
3#include <string>
4
5using namespace std;
6
7string
8read_file(string_view filename)
9{
10 fstream input{ filename };
11 return { istreambuf_iterator<char>{ input }, {} };
12}
13
14void
15part1(string_view puzzle)
16{
17 int floor = 0;
18 for (const auto& chr : puzzle) {
19 if (chr == '(') {
20 ++floor;
21 }
22 else if (chr == ')') {
23 --floor;
24 }
25 }
26 cout << floor << endl;
27}
28
29void
30part2(string_view puzzle)
31{
32 int floor = 0;
33 for (string_view::size_type pos = 0; pos != puzzle.length(); ++pos) {
34 const auto chr = puzzle[pos];
35 if (chr == '(') {
36 ++floor;
37 }
38 else if (chr == ')') {
39 --floor;
40 }
41 if (floor == -1) {
42 cout << pos+1 << endl;
43 break;
44 }
45 }
46}
47
48int
49main()
50{
51 auto puzzle = read_file("data/day01.txt");
52 part1(puzzle);
53 part2(puzzle);
54}
diff --git a/makefile b/makefile
index 494ced4..d88478f 100644
--- a/makefile
+++ b/makefile
@@ -1,12 +1,17 @@
1include config.mk 1include config.mk
2 2
3all: $(patsubst 2020/src/%.cpp,2020/bin/%,$(wildcard 2020/src/*.cpp)) \ 3all: $(patsubst 2015/src/%.cpp,2015/bin/%,$(wildcard 2015/src/*.cpp)) \
4 $(patsubst 2020/src/%.cpp,2020/bin/%,$(wildcard 2020/src/*.cpp)) \
4 $(patsubst 2022/src/%.cpp,2022/bin/%,$(wildcard 2022/src/*.cpp)) \ 5 $(patsubst 2022/src/%.cpp,2022/bin/%,$(wildcard 2022/src/*.cpp)) \
5 $(patsubst 2023/src/%.cpp,2023/bin/%,$(wildcard 2023/src/*.cpp)) 6 $(patsubst 2023/src/%.cpp,2023/bin/%,$(wildcard 2023/src/*.cpp))
6 7
7%/bin: 8%/bin:
8 mkdir -p $@ 9 mkdir -p $@
9 10
11# 2015
122015/bin/%: 2015/src/%.cpp | 2015/bin
13 c++ $(CPPFLAGS) $^ -o $@
14
10# 2020 15# 2020
112020/bin/%: 2020/src/%.cpp | 2020/bin 162020/bin/%: 2020/src/%.cpp | 2020/bin
12 c++ $(CPPFLAGS) $^ -o $@ 17 c++ $(CPPFLAGS) $^ -o $@