aboutsummaryrefslogtreecommitdiff
path: root/2022
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2024-05-03 15:41:16 +0200
committerThomas Schmucker <ts@its1.de>2024-05-03 15:41:16 +0200
commit7336485d0f3c2f956ca982de2610f5f3f647fadb (patch)
tree92b023b3a59b9030a7322b482f75d29476ba9c72 /2022
parentc0c631ce08d09150b4cfbddc2217a6f87a9c2b2d (diff)
downloadadvent-of-code-7336485d0f3c2f956ca982de2610f5f3f647fadb.tar.gz
advent-of-code-7336485d0f3c2f956ca982de2610f5f3f647fadb.tar.bz2
advent-of-code-7336485d0f3c2f956ca982de2610f5f3f647fadb.zip
day 9, advent of code 2022
Diffstat (limited to '2022')
-rw-r--r--2022/src/day09.cpp116
1 files changed, 116 insertions, 0 deletions
diff --git a/2022/src/day09.cpp b/2022/src/day09.cpp
new file mode 100644
index 0000000..8b99942
--- /dev/null
+++ b/2022/src/day09.cpp
@@ -0,0 +1,116 @@
1#include <array>
2#include <fstream>
3#include <iostream>
4#include <map>
5#include <set>
6#include <vector>
7using namespace std;
8
9vector<tuple<char, int>>
10read_file(string_view filename)
11{
12 fstream input{ filename };
13 vector<tuple<char, int>> result;
14
15 char dir = 0;
16 int count = 0;
17
18 while ( input >> dir >> count ) {
19 result.emplace_back(dir, count);
20 }
21
22 return result;
23}
24
25int
26distance(const tuple<int, int>& head, const tuple<int, int>& tail)
27{
28 auto dist_x = abs(get<0>(head) - get<0>(tail));
29 auto dist_y = abs(get<1>(head) - get<1>(tail));
30 return max(dist_x, dist_y);
31};
32
33static map<char, tuple<int, int>> direction = {
34 { 'R', { +1, 0 } },
35 { 'L', { -1, 0 } },
36 { 'U', { 0, +1 } },
37 { 'D', { 0, -1 } }
38};
39
40void
41part1(const vector<tuple<char, int>>& data)
42{
43 tuple<int, int> head = { 0, 0 };
44 tuple<int, int> tail = head;
45
46 set<tuple<int, int>> positions;
47
48 for ( auto [dir, count]: data ) {
49 while ( count-- > 0 ) {
50 const auto [delta_x, delta_y] = direction[dir];
51 const auto old_head = head;
52
53 get<0>(head) += delta_x;
54 get<1>(head) += delta_y;
55
56 if ( distance(head, tail) > 1 ) {
57 tail = old_head;
58 }
59
60 positions.insert(tail);
61 }
62 }
63
64 cout << positions.size() << endl;
65}
66
67void
68part2(const vector<tuple<char, int>>& data)
69{
70 static const auto rope_size = 10;
71
72 array<tuple<int, int>, rope_size> rope;
73
74 set<tuple<int, int>> positions;
75
76 for ( auto [dir, count]: data ) {
77 while ( count-- > 0 ) {
78 auto [delta_x, delta_y] = direction[dir];
79
80 get<0>(rope[0]) += delta_x;
81 get<1>(rope[0]) += delta_y;
82
83 for ( size_t i = 1; i < rope.size(); ++i ) {
84 auto& head = rope.at(i - 1);
85 auto& tail = rope.at(i);
86
87 if ( distance(head, tail) > 1 ) {
88 delta_x = get<0>(head) - get<0>(tail);
89 delta_y = get<1>(head) - get<1>(tail);
90
91 if ( abs(delta_x) > 1 ) {
92 delta_x /= abs(delta_x);
93 }
94 if ( abs(delta_y) > 1 ) {
95 delta_y /= abs(delta_y);
96 }
97
98 get<0>(tail) += delta_x;
99 get<1>(tail) += delta_y;
100 }
101 }
102
103 positions.insert(rope[9]);
104 }
105 }
106
107 cout << positions.size() << endl;
108}
109
110int
111main()
112{
113 const auto data = read_file("data/day09.txt");
114 part1(data);
115 part2(data);
116}