aboutsummaryrefslogtreecommitdiff
path: root/2015
diff options
context:
space:
mode:
Diffstat (limited to '2015')
-rw-r--r--2015/src/day03.cpp93
1 files changed, 93 insertions, 0 deletions
diff --git a/2015/src/day03.cpp b/2015/src/day03.cpp
new file mode 100644
index 0000000..e81da90
--- /dev/null
+++ b/2015/src/day03.cpp
@@ -0,0 +1,93 @@
1#include <fstream>
2#include <iostream>
3#include <set>
4#include <string>
5
6using namespace std;
7
8auto
9read_file(string_view filename)
10{
11 fstream input{ filename };
12 return string{ istreambuf_iterator<char>{ input }, {} };
13}
14
15void
16part1(string_view directions)
17{
18 set<tuple<int, int>> positions{};
19 int xpos = 0;
20 int ypos = 0;
21
22 positions.emplace(xpos, ypos);
23 for ( const auto direction: directions ) {
24 switch ( direction ) {
25 case '^':
26 --ypos;
27 break;
28 case 'v':
29 ++ypos;
30 break;
31 case '<':
32 --xpos;
33 break;
34 case '>':
35 ++xpos;
36 break;
37 default:
38 break;
39 }
40 positions.emplace(xpos, ypos);
41 }
42 cout << positions.size() << endl;
43}
44
45void
46part2(string_view directions)
47{
48 set<tuple<int, int>> positions{};
49
50 for ( string_view::size_type start = 0; start != 2; ++start ) {
51 int xpos = 0;
52 int ypos = 0;
53
54 positions.emplace(xpos, ypos);
55
56 for ( string_view::size_type idx = start; idx < directions.length(); idx += 2 ) {
57 const auto direction = directions[idx];
58
59 switch ( direction ) {
60 case '^':
61 --ypos;
62 break;
63 case 'v':
64 ++ypos;
65 break;
66 case '<':
67 --xpos;
68 break;
69 case '>':
70 ++xpos;
71 break;
72 default:
73 break;
74 }
75 positions.emplace(xpos, ypos);
76 }
77 }
78 cout << positions.size() << endl;
79}
80
81int
82main()
83{
84 auto directions = read_file("data/day03.txt");
85 // part1(">");
86 // part1("^>v<");
87 // part1("^v^v^v^v^v");
88 part1(directions);
89 // part2("^v");
90 // part2("^>v<");
91 // part2("^v^v^v^v^v");
92 part2(directions);
93}