-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDec12.cpp
More file actions
123 lines (92 loc) · 2.61 KB
/
Dec12.cpp
File metadata and controls
123 lines (92 loc) · 2.61 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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
#include <queue>
#include "../graph_algorithms.h"
using Nodes = std::pair<int,int>;
class Graph{
public:
std::vector<std::vector<int>> map{};
Nodes start{};
Nodes target{};
std::vector<std::pair<int,int>> directions = {{1,0},{0,1},{-1,0},{0,-1}};
bool part_one{};
void readFile(std::ifstream&);
void print() const;
bool isGoal(const Nodes&) const;
std::vector<Nodes> getNeighbours(const Nodes&) const;
};
void Graph::readFile(std::ifstream& file){
int row{};
int col{};
std::string str{};
while(getline(file,str)){
std::stringstream ss{str};
char c{};
std::vector<int> line{};
col=0;
while (ss.get(c)){
if (c=='S'){
line.push_back(0);
start = std::make_pair(row,col);
}else if (c=='E'){
line.push_back('z'-'a'+1);
target = std::make_pair(row,col);
}else{
line.push_back(c-'a');
}
++col;
}
map.push_back(line);
++row;
}
}
void Graph::print() const{
for (auto it=map.begin();it<map.end();++it){
for (auto itt=(*it).begin();itt<(*it).end();++itt){
std::cout<<*itt<<' ';
}
std::cout<<'\n';
}
}
bool Graph::isGoal(const Nodes& node) const{
if (part_one){
return (node == target);
}else{
return (map[node.first][node.second]==0);
}
}
std::vector<Nodes> Graph::getNeighbours(const Nodes& node) const{
std::vector<Nodes> neighbours{};
for (int i=0;i<4;++i){
Nodes cur{node};
cur.first += directions[i].first;
cur.second += directions[i].second;
if (cur.first<0 || cur.first>map.size()-1 || cur.second<0 || cur.second>map[0].size()-1) continue;
if (part_one){
if (map[cur.first][cur.second]<=map[node.first][node.second]+1){
neighbours.push_back(cur);
}
}else{
if (map[cur.first][cur.second]>=map[node.first][node.second]-1){
neighbours.push_back(cur);
}
}
}
return neighbours;
}
int main(){
std::ifstream file("data12.txt");
Graph graph{};
graph.readFile(file);
//graph.print();
graph.part_one = true;
std::pair<Nodes,int> result = bfs(graph.start,graph);
std::cout<<"Part one: "<<result.second<<'\n';
graph.part_one = false;
result = bfs(graph.target,graph);
std::cout<<"Part two: "<<result.second<<'\n';
return 0;
}