-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday23.cpp
173 lines (139 loc) · 4.41 KB
/
day23.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using namespace std;
#include <iostream>
#include <string>
#include <utility>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <unordered_map>
#include "Helpers/HelperFunctions.h"
void renderGrid(unordered_map<int, unordered_map<int, bool>> &grid, vector<pair<int,int>> &elves);
int main() {
cout << "Day 23" << endl;
ifstream file(R"(C:\Users\gwen\Documents\2_Programming\Advent of Code\Advent of Code 2022\inputs\day23.txt)");
string str;
unordered_map<int, unordered_map<int, bool>> grid;
vector<pair<int,int>> elves;
int y = 0;
while (getline(file, str)) {
int x = 0;
for (auto &it : str) {
if (it == '#') {
grid[x][y] = true;
elves.emplace_back(x, y);
}
x++;
}
y++;
}
int startingStep = 0;
int i = 0;
for (i = 0; ; ++i) {
// renderGrid(grid, elves);
map<pair<int,int>, int> proposed;
vector<pair<pair<int,int>, pair<int,int>>> nextPossible;
vector<pair<int,int>> next;
bool didMove = false;
for (auto &elf : elves) {
int currX = elf.first;
int currY = elf.second;
bool breakOut = false;
if (grid[currX - 1][currY - 1] || grid[currX][currY - 1] | grid[currX + 1][currY - 1]
|| grid[currX - 1][currY] || grid[currX + 1][currY]
|| grid[currX - 1][currY + 1] || grid[currX][currY + 1] || grid[currX + 1][currY + 1]) {
for (int j = 0; j < 4; j++) {
switch ((startingStep + j) % 4) {
case 0: // north
if (!grid[currX - 1][currY - 1] && !grid[currX][currY - 1] && !grid[currX + 1][currY - 1]) {
proposed[make_pair(currX, currY - 1)]++;
nextPossible.emplace_back(elf, make_pair(currX, currY - 1));
breakOut = true;
}
break;
case 1: // south
if (!grid[currX - 1][currY + 1] && !grid[currX][currY + 1] && !grid[currX + 1][currY + 1]) {
proposed[make_pair(currX, currY + 1)]++;
nextPossible.emplace_back(elf, make_pair(currX, currY + 1));
breakOut = true;
}
break;
case 2: // west
if (!grid[currX - 1][currY - 1] && !grid[currX - 1][currY] && !grid[currX - 1][currY + 1]) {
proposed[make_pair(currX - 1, currY)]++;
nextPossible.emplace_back(elf, make_pair(currX - 1, currY));
breakOut = true;
}
break;
case 3: // east
if (!grid[currX + 1][currY - 1] && !grid[currX + 1][currY] && !grid[currX + 1][currY + 1]) {
proposed[make_pair(currX + 1, currY)]++;
nextPossible.emplace_back(elf, make_pair(currX + 1, currY));
breakOut = true;
}
break;
}
if (breakOut) break;
}
}
if (!breakOut) { // stay in place;
next.push_back(elf);
} else {
didMove = true;
}
}
if (!didMove) break;
for (auto &it : nextPossible) {
if (proposed[it.second] > 1) {
next.push_back(it.first);
} else {
next.push_back(it.second);
}
}
for (auto &it : elves) {
grid[it.first][it.second] = false;
}
elves.clear();
for (auto &it : next) {
grid[it.first][it.second] = true;
elves.push_back(it);
}
startingStep++;
startingStep %= 4;
if (i == 9) {
int maxX = INT16_MIN;
int minX = INT16_MAX;
int maxY = INT16_MIN;
int minY = INT16_MAX;
for (auto &it : elves) {
maxX = max(maxX, it.first);
minX = min(minX, it.first);
maxY = max(maxY, it.second);
minY = min(minY, it.second);
}
int ans = (maxX - minX + 1) * (maxY - minY + 1) - elves.size();
cout << "Part 1: " << ans << endl;
}
}
cout << "Part 2: " << i + 1 << endl;
return 0;
}
void renderGrid(unordered_map<int, unordered_map<int, bool>> &grid, vector<pair<int,int>> &elves) {
int maxX = INT16_MIN;
int minX = INT16_MAX;
int maxY = INT16_MIN;
int minY = INT16_MAX;
for (auto &it : elves) {
maxX = max(maxX, it.first);
minX = min(minX, it.first);
maxY = max(maxY, it.second);
minY = min(minY, it.second);
}
for (int y = minY; y <= maxY; y++) {
for (int x = minX; x <= maxX; x++) {
cout << ((grid[x][y]) ? '#' : '.');
}
cout << endl;
}
cout << endl;
}