Skip to content

Commit 4653a00

Browse files
committed
adding threads to day 6 part 2
1 parent d022d45 commit 4653a00

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

2024/src/day13.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ int main(int argc, char **argv)
149149
Data data = parse();
150150

151151
int answer1 = 36870;
152-
int64_t answer2 = 0;
152+
int64_t answer2 = 78101482023732;
153153

154154
auto first = part1(data);
155155
std::cout << "Part 1: " << first << std::endl;

2024/src/day14.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ int part2(const Data &data)
8585
}
8686
}
8787
robots = data.robots;
88-
iterate(robots, min_t +1, width, height);
88+
iterate(robots, min_t + 1, width, height);
8989
std::vector<std::string> grid(height, std::string(width, '.'));
9090
for (auto &[px, py, _, __] : robots)
9191
{
@@ -157,7 +157,7 @@ int main(int argc, char **argv)
157157
Data data = parse();
158158

159159
int64_t answer1 = 225552000;
160-
int64_t answer2 = 0;
160+
int64_t answer2 = 7371;
161161

162162
auto first = part1(data);
163163
std::cout << "Part 1: " << first << std::endl;

2024/src/day6.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <tuple>
88
#include <unordered_set>
99
#include <aoc/2024/pos.h>
10+
#include <thread>
11+
#include <mutex>
1012

1113
struct Data
1214
{
@@ -62,16 +64,15 @@ int part2(const Data &data)
6264
{
6365
int cycles = 0;
6466
auto path = data.path;
67+
std::mutex mtx;
6568

66-
for (const auto &obstacle : path)
67-
{
69+
auto process_obstacle = [&](const Pos &obstacle) {
6870
std::unordered_set<std::pair<Pos, Pos>, PosPairHash> visited;
69-
7071
Pos p = data.p;
7172
Pos dir = data.dir;
7273

7374
if (obstacle == p)
74-
continue; // ignore the starting location
75+
return; // ignore the starting location
7576
while (true)
7677
{
7778
if (visited.find({p, dir}) == visited.end())
@@ -81,6 +82,7 @@ int part2(const Data &data)
8182
}
8283
else
8384
{
85+
std::lock_guard<std::mutex> lock(mtx);
8486
cycles += 1;
8587
break;
8688
}
@@ -94,6 +96,17 @@ int part2(const Data &data)
9496
}
9597
p += dir;
9698
}
99+
};
100+
101+
std::vector<std::thread> threads;
102+
for (const auto &obstacle : path)
103+
{
104+
threads.emplace_back(process_obstacle, obstacle);
105+
}
106+
107+
for (auto &thread : threads)
108+
{
109+
thread.join();
97110
}
98111

99112
return cycles;

0 commit comments

Comments
 (0)