-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday6.cpp
More file actions
71 lines (58 loc) · 1.71 KB
/
day6.cpp
File metadata and controls
71 lines (58 loc) · 1.71 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
#include "utils/helpers.hpp"
typedef std::pair<Coord, int> Guard;
Guard turn(Guard g) {
return std::make_pair(g.first, ((g.second + 1) % 4));
}
Guard next(Guard guard, const Grid<char>& grid) {
Coord moves[] = {Coord(0, -1), Coord(1, 0), Coord(0, 1), Coord(-1, 0)};
Guard nextGuard = std::make_pair(guard.first + moves[guard.second], guard.second);
if (grid.inBounds(nextGuard.first) && grid.get(nextGuard.first) == '#') {
return turn(guard);
}
return nextGuard;
}
bool addObstruction(Coord obs, Guard guard, Grid<char>& grid) {
std::set<Guard> visited;
grid.get(obs) = '#';
while (grid.inBounds(guard.first)) {
guard = next(guard, grid);
if (!visited.insert(guard).second)
{
grid.get(obs) = '.';
return true;
}
}
grid.get(obs) = '.';
return false;
}
int main () {
ui part = 0;
std::ifstream input;
Grid<char> grid;
std::string line;
std::set<Coord> visited;
Guard guard;
ui resPart2 = 0;
if (getFileAndPart(6, input, part))
return errno;
while (!input.eof()) {
getline(input, line);
grid.addBackLine(line);
}
guard = std::make_pair(grid.findOne('^').value(), 0);
while (grid.inBounds(guard.first)) {
visited.insert(guard.first);
guard = next(guard, grid);
}
if (part == 1)
std::cout << "result is " << visited.size() << '\n';
else {
guard = std::make_pair(grid.findOne('^').value(), 0);
visited.erase(guard.first);
for (Coord c: visited) {
resPart2 += addObstruction(c, guard, grid);
}
std::cout << "result is " << resPart2 << '\n';
}
return 0;
}