-
Notifications
You must be signed in to change notification settings - Fork 0
/
part2.cpp
74 lines (57 loc) · 1.94 KB
/
part2.cpp
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
#include "Day17.hpp"
#include <queue>
#include "../util/Md5Provider.hpp"
using namespace std;
Result Day17::solve_p2(){
Field maze[MAZE_SIZE][MAZE_SIZE] = {
{ {false, WALL, DOOR, DOOR, WALL},
{false, WALL, DOOR, DOOR, DOOR},
{false, WALL, DOOR, DOOR, DOOR},
{false, WALL, WALL, DOOR, DOOR} },
{ {false, DOOR, DOOR, DOOR, WALL},
{false, DOOR, DOOR, DOOR, DOOR},
{false, DOOR, DOOR, DOOR, DOOR},
{false, DOOR, WALL, DOOR, DOOR} },
{ {false, DOOR, DOOR, DOOR, WALL},
{false, DOOR, DOOR, DOOR, DOOR},
{false, DOOR, DOOR, DOOR, DOOR},
{false, DOOR, WALL, DOOR, DOOR} },
{ {false, DOOR, DOOR, WALL, WALL},
{false, DOOR, DOOR, WALL, DOOR},
{false, DOOR, DOOR, WALL, DOOR},
{true, DOOR, DOOR, DOOR, DOOR} },
};
queue<Task> tasks;
tasks.push({0, 0, ""});
Md5Provider md5;
unsigned longest = 0;
while( !tasks.empty() ){
Task task = tasks.front();
tasks.pop();
Field *cell = &(maze[task.x][task.y]);
if( cell->is_goal ){
if( task.path.length() > longest ){
longest = task.path.length();
}
continue;
}
string digest = md5.compute(data + task.path);
// up
if( cell->up == DOOR && is_open(digest[0]) ){
tasks.push({task.x - 1, task.y, task.path + "U"});
}
// down
if( cell->down == DOOR && is_open(digest[1]) ){
tasks.push({task.x + 1, task.y, task.path + "D"});
}
// left
if( cell->left == DOOR && is_open(digest[2]) ){
tasks.push({task.x, task.y - 1, task.path + "L"});
}
// right
if( cell->right == DOOR && is_open(digest[3]) ){
tasks.push({task.x, task.y + 1, task.path + "R"});
}
}
return {true, to_string(longest)};
}