-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday12.cpp
More file actions
114 lines (102 loc) · 3.28 KB
/
day12.cpp
File metadata and controls
114 lines (102 loc) · 3.28 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
#include "utils/helpers.hpp"
#define UP 0
#define RIGHT 1
#define DOWN 2
#define LEFT 3
typedef std::map<char, std::set<Coord>> LetterGroups;
typedef std::vector<std::set<Coord>> Plots;
Coord dirs[] = {Coord(0, 1), Coord(0, -1), Coord(1, 0), Coord(-1, 0)};
ui calcCost(const std::set<Coord>& plot, ui part) {
ui cost = 0;
std::set<std::pair<Coord, int>> fences;
int voisin;
ui sides = 0;
for (Coord c: plot) {
for (Coord dir: dirs) {
if (plot.find(c + dir) == plot.end())
{
++cost;
if (dir == Coord(0, 1))
fences.insert(std::make_pair((c + dir), DOWN));
else if (dir == Coord (0, -1))
fences.insert(std::make_pair((c + dir), UP));
else if (dir == Coord (1, 0))
fences.insert(std::make_pair((c + dir), RIGHT));
else
fences.insert(std::make_pair((c + dir), LEFT));
}
}
}
if (part == 1)
return cost * plot.size();
else {
for (std::pair<Coord, int> cf : fences) {
voisin = 0;
for (Coord dir: dirs)
{
if (fences.find(std::make_pair(cf.first + dir, cf.second)) != fences.end())
++voisin;
}
switch (voisin) {
case 0:
sides += 2;
break;
case 1:
++sides;
break;
}
}
return (sides / 2) * plot.size();
}
}
void lookForPlots(std::set<Coord>& coords, Plots& plots) {
std::set<Coord>::iterator found;
std::deque<Coord> tasks;
std::set<Coord> build;
std::set<Coord> fixLoopInf;
Coord current;
while (!coords.empty()) {
build.clear();
tasks.push_back(*(coords.begin()));
while (!tasks.empty()) {
current = tasks.front();
coords.erase(current);
tasks.pop_front();
build.insert(current);
for (Coord dir: dirs) {
found = coords.find(current + dir);
if (fixLoopInf.find(current + dir) == fixLoopInf.end() && found != coords.end()) {
tasks.push_back(current + dir);
}
fixLoopInf.insert(current + dir);
}
}
plots.push_back(build);
}
}
int main() {
ui part = 0;
std::ifstream input;
LetterGroups allLetters;
Plots plots;
ui costCount = 0;
std::pair<char, Coord> read = std::make_pair(0, Coord(0, 0));
if (getFileAndPart(12, input, part))
return errno;
read.first = input.get();
while (!input.eof()) {
if (read.first == '\r')
read.first = input.get();
if (read.first == '\n')
read = std::make_pair(input.get(), Coord(0, read.second.second + 1));
allLetters[read.first].insert(read.second);
read.second += Coord(1, 0);
read.first = input.get();
}
for (LetterGroups::iterator it = allLetters.begin(); it != allLetters.end(); ++it) {
lookForPlots(it->second, plots);
}
for (const std::set<Coord>& plot: plots)
costCount += calcCost(plot, part);
std::cout << "result is " << costCount << '\n';
}