-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday12a.cpp
100 lines (95 loc) · 2.15 KB
/
day12a.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
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
#include "utils.hpp"
void permute(std::pair<char, int> (*field)[70], int val, int x, int y)
{
if (x > 0)
{
if (field[x - 1][y].second == -1)
{
if (field[x - 1][y].first <= (field[x][y].first + 1))
{
field[x - 1][y].second = val + 1;
}
}
}
if (x < 40)
{
if (field[x + 1][y].second == -1)
{
if (field[x + 1][y].first <= (field[x][y].first + 1))
{
field[x + 1][y].second = val + 1;
}
}
}
if (y > 0)
{
if (field[x][y - 1].second == -1)
{
if (field[x][y - 1].first <= (field[x][y].first + 1))
{
field[x][y - 1].second = val + 1;
}
}
}
if (y < 69)
{
if (field[x][y + 1].second == -1)
{
if (field[x][y + 1].first <= (field[x][y].first + 1))
{
field[x][y + 1].second = val + 1;
}
}
}
}
void search(std::pair<char, int> (*field)[70], int val)
{
for (int i = 0; i < 41; i++)
{
for (int j = 0; j < 70; j++)
{
if (field[i][j].second == val)
{
permute(field, val, i, j);
}
}
}
}
int main(int argc, char *argv[])
{
auto input = read_input("day12_input");
std::pair<char, int> field[41][70]{};
int goal_i = 0;
int goal_j = 0;
int i = 0;
for (auto &&x : input)
{
int j = 0;
for (auto &&y : x)
{
if (y == 'S')
{
field[i][j] = std::make_pair('a', 0);
}
else if (y == 'E')
{
goal_i = i;
goal_j = j;
field[i][j] = std::make_pair(char(123), -1);
}
else
{
field[i][j] = std::make_pair(y, -1);
}
j++;
}
i++;
}
int search_val = 0;
while (field[goal_i][goal_j].second == -1)
{
search(field, search_val);
search_val++;
}
std::cout << field[goal_i][goal_j].second << "\n";
}