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