aboutsummaryrefslogtreecommitdiff
path: root/2016/src
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2025-01-10 19:34:39 +0100
committerThomas Schmucker <ts@its1.de>2025-01-10 19:34:39 +0100
commit5f92ea475dadc173b2172834227f9392fd7d5a7c (patch)
tree1313db95ed27ccaa44d3a825efad96c369fb05bf /2016/src
parent8f53e809e312021596be70317ea053e5bec6c816 (diff)
downloadadvent-of-code-5f92ea475dadc173b2172834227f9392fd7d5a7c.tar.gz
advent-of-code-5f92ea475dadc173b2172834227f9392fd7d5a7c.tar.bz2
advent-of-code-5f92ea475dadc173b2172834227f9392fd7d5a7c.zip
aoc 2016, day 1
Diffstat (limited to '2016/src')
-rw-r--r--2016/src/day01.cpp122
1 files changed, 122 insertions, 0 deletions
diff --git a/2016/src/day01.cpp b/2016/src/day01.cpp
new file mode 100644
index 0000000..f5d74a5
--- /dev/null
+++ b/2016/src/day01.cpp
@@ -0,0 +1,122 @@
1#include <array>
2#include <fstream>
3#include <iostream>
4#include <set>
5#include <string>
6#include <tuple>
7#include <vector>
8
9using namespace std;
10
11vector<string>
12split(string_view line, string_view delimiter)
13{
14 size_t pos_start = 0;
15 size_t pos_end = 0;
16
17 vector<string> res;
18
19 while ( (pos_end = line.find(delimiter, pos_start)) != string::npos ) {
20 auto token = line.substr(pos_start, pos_end - pos_start);
21 pos_start = pos_end + delimiter.length();
22
23 res.emplace_back(token);
24 }
25
26 if ( pos_start != line.size() ) {
27 res.emplace_back(line.substr(pos_start));
28 }
29 return res;
30}
31
32vector<tuple<char, long>>
33read_file(string_view filename)
34{
35 fstream input{ filename };
36 string content{ istreambuf_iterator<char>{ input }, istreambuf_iterator<char>{} };
37
38 vector<tuple<char, long>> result;
39 for ( const auto& part: split(content, ", ") ) {
40 result.emplace_back(part[0], stol(part.substr(1)));
41 }
42 return result;
43}
44
45void
46part1(const vector<tuple<char, long>>& input)
47{
48 array<tuple<long, long>, 4> dirs = {
49 make_tuple(1L, 0L),
50 make_tuple(0L, 1L),
51 make_tuple(-1L, 0L),
52 make_tuple(0L, -1L)
53 };
54
55 size_t dir = 0;
56 long pos_x = 0;
57 long pos_y = 0;
58
59 for ( const auto& [change, length]: input ) {
60 if ( change == 'L' ) {
61 dir = (dir + 1) % 4;
62 }
63 else {
64 dir = (dir + 3) % 4;
65 }
66
67 auto [delta_x, delta_y] = dirs.at(dir);
68
69 pos_x += length * delta_x;
70 pos_y += length * delta_y;
71 }
72
73 cout << abs(pos_x) + abs(pos_y) << endl;
74}
75
76void
77part2(const vector<tuple<char, long>>& input)
78{
79 array<tuple<long, long>, 4> dirs = {
80 make_tuple(1L, 0L),
81 make_tuple(0L, 1L),
82 make_tuple(-1L, 0L),
83 make_tuple(0L, -1L)
84 };
85
86 size_t dir = 0;
87 long pos_x = 0;
88 long pos_y = 0;
89
90 set<tuple<long, long>> visited;
91
92 for ( auto [change, length]: input ) {
93 if ( change == 'L' ) {
94 dir = (dir + 1) % 4;
95 }
96 else {
97 dir = (dir + 3) % 4;
98 }
99
100 auto [delta_x, delta_y] = dirs.at(dir);
101
102 while ( length-- > 0 ) {
103 pos_x += delta_x;
104 pos_y += delta_y;
105
106 if ( visited.contains({ pos_x, pos_y }) ) {
107 cout << abs(pos_x) + abs(pos_y) << endl;
108 return;
109 }
110
111 visited.emplace(pos_x, pos_y);
112 }
113 }
114}
115
116int
117main()
118{
119 auto input = read_file("data/day01.txt");
120 part1(input);
121 part2(input);
122}