Repository navigation
Expand file tree
/
Copy pathday06.cpp
More file actions
162 lines (139 loc) · 5.38 KB
/
Copy pathday06.cpp
File metadata and controls
162 lines (139 loc) · 5.38 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "../utils/containers.cpp"
#include "../utils/files.cpp"
#include <algorithm>
#include <chrono>
#include <string>
#include <unordered_set>
#include <utility>
#include <vector>
template <typename T> struct Vec2 {
T y, x;
Vec2(T y = T(0), T x = T(0)) : y(y), x(x) {}
Vec2 operator+(const Vec2& other) const { return Vec2(y + other.y, x + other.x); }
Vec2 operator-(const Vec2& other) const { return Vec2(y - other.y, x - other.x); }
bool operator==(const Vec2& other) const { return y == other.y && x == other.x; }
bool operator!=(const Vec2& other) const { return !(*this == other); }
std::string toString() const { return std::format("({}, {})", y, x); }
};
namespace std {
template <typename T> struct hash<Vec2<T>> {
size_t operator()(const Vec2<T>& v) const {
size_t h1 = hash<T>{}(v.x);
size_t h2 = hash<T>{}(v.y);
return h1 ^ (h2 << 1);
}
};
template <typename T1, typename T2> struct hash<std::pair<T1, T2>> {
size_t operator()(const std::pair<T1, T2>& p) const {
size_t h1 = std::hash<T1>{}(p.first);
size_t h2 = std::hash<T2>{}(p.second);
return h1 ^ (h2 << 1);
}
};
} // namespace std
std::vector<std::vector<int>> extractUpdates(std::string_view updatesStr) {
std::vector<std::vector<int>> updates;
std::istringstream stream{std::string(updatesStr)};
std::string line;
while (std::getline(stream, line)) {
std::vector<int> updateInstructions;
for (auto&& part : line | std::ranges::views::split(',')) {
updateInstructions.emplace_back(std::stoi(std::string(part.begin(), part.end())));
}
updates.push_back(updateInstructions);
}
return updates;
}
auto findGuardPosition(const std::string& content, size_t lineWidth) {
size_t guard_idx = content.find('^');
if (guard_idx == std::string::npos) {
throw std::runtime_error("Couldn't determine line length!");
}
size_t adjustedLineWidth = lineWidth + 1;
std::int64_t row = static_cast<std::int64_t>(guard_idx / adjustedLineWidth);
std::int64_t col = static_cast<std::int64_t>(guard_idx % adjustedLineWidth);
if (std::cmp_equal(col, adjustedLineWidth - 1)) {
col = 0;
row--;
}
return Vec2<std::int64_t>{row, col};
}
size_t getMapWidth(const std::string& content) {
size_t line_break_idx = content.find('\n');
if (line_break_idx == std::string::npos) {
throw std::runtime_error("Couldn't determine line length!");
}
return line_break_idx;
}
inline auto getChar(const Vec2<int64_t>& pos, const FileData& file, const size_t width) {
size_t index = width * pos.y + pos.x + pos.y;
return file.content[index];
};
inline auto isInsideMap(const Vec2<int64_t>& pos, const size_t width, const size_t height) {
return std::cmp_greater_equal(pos.x, 0) && std::cmp_less(pos.x, width) && std::cmp_greater_equal(pos.y, 0) &&
std::cmp_less(pos.y, height);
};
const std::array<Vec2<int64_t>, 4> DIRECTIONS = {Vec2<int64_t>{-1LL, 0LL}, Vec2<int64_t>{0LL, 1LL},
Vec2<int64_t>{1LL, 0LL}, Vec2<int64_t>{0LL, -1LL}};
auto part01(const FileData& file, const size_t width, const size_t height, Vec2<int64_t> pos) {
auto dirIdx = 0; // start position looking up
std::unordered_set<Vec2<std::int64_t>> visitedPositions;
while (isInsideMap(pos, width, height)) {
visitedPositions.insert(pos);
auto nextPos = pos + DIRECTIONS[dirIdx];
if (getChar(nextPos, file, width) == '#') {
dirIdx = (++dirIdx) % DIRECTIONS.size();
} else {
pos = nextPos;
}
}
return visitedPositions;
}
int part02(const FileData& file, const size_t width, const size_t height, Vec2<int64_t> initialPos,
const std::unordered_set<Vec2<std::int64_t>>& originalVisitedPositions) {
using PosType = decltype(initialPos);
auto hasLoop = [&file, &width, &height, &initialPos](PosType addedBlockage) {
auto dirIdx = 0; // start position looking up
auto pos = initialPos;
using DirectionType = decltype(dirIdx);
std::unordered_set<std::pair<PosType, DirectionType>> visitedPositionsWithDirections;
while (isInsideMap(pos, width, height)) {
auto nextPos = pos + DIRECTIONS[dirIdx];
if (addedBlockage == nextPos || getChar(nextPos, file, width) == '#') {
auto insertResult = visitedPositionsWithDirections.insert(std::make_pair(pos, dirIdx));
if (!insertResult.second) {
return true; // loop found!
}
dirIdx = (++dirIdx) % DIRECTIONS.size();
} else {
pos = nextPos;
}
}
return false;
};
auto loopCount{0L};
for (int64_t y = 0; std::cmp_less(y, height); y++) {
for (int64_t x = 0; std::cmp_less(x, width); x++) {
const Vec2<int64_t> blockagePos{y, x};
if (originalVisitedPositions.contains(blockagePos) && blockagePos != initialPos && hasLoop(blockagePos)) {
loopCount++;
}
}
}
return loopCount;
}
int main() {
auto start = std::chrono::high_resolution_clock::now();
auto file = readFile("../generated/aoc/inputs/2024/day06.txt");
const auto width = getMapWidth(file.content);
const auto height = file.line_count;
const auto initialPos = findGuardPosition(file.content, width);
const auto originalVisitedPositions = part01(file, width, height, initialPos);
auto part1 = originalVisitedPositions.size();
auto part2 = part02(file, width, height, initialPos, originalVisitedPositions);
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<double, std::milli> duration_ms = end - start;
std::cout << "Execution time: " << duration_ms.count() << " ms" << std::endl;
std::cout << "Part 01: " << (part1) << std::endl;
std::cout << "Part 02: " << (part2) << std::endl;
}