Last active
December 22, 2021 06:19
-
-
Save progheal/36a3fd390495e10a22c5284a437cac2e to your computer and use it in GitHub Desktop.
Advent of Code 2021 Day 22: https://www.reddit.com/r/adventofcode/comments/rlxhmg/2021_day_22_solutions/hpizerm/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <string> | |
#include <algorithm> | |
using namespace std; | |
struct Op | |
{ | |
int xm, xM, ym, yM, zm, zM, sw; | |
}; | |
int cube[101][101][101] = {}; | |
int main() | |
{ | |
vector<Op> vop; | |
vector<int> xs, ys, zs; | |
string s; | |
while(getline(cin, s)) | |
{ | |
Op op; | |
op.sw = s[1] == 'n' ? 1 : 0; | |
size_t p = s.find('='); | |
op.xm = stoi(s.substr(p+1)); | |
xs.push_back(op.xm); | |
p = s.find('.', p); | |
op.xM = stoi(s.substr(p+2)); | |
xs.push_back(op.xM+1); | |
p = s.find('=', p); | |
op.ym = stoi(s.substr(p+1)); | |
ys.push_back(op.ym); | |
p = s.find('.', p); | |
op.yM = stoi(s.substr(p+2)); | |
ys.push_back(op.yM+1); | |
p = s.find('=', p); | |
op.zm = stoi(s.substr(p+1)); | |
zs.push_back(op.zm); | |
p = s.find('.', p); | |
op.zM = stoi(s.substr(p+2)); | |
zs.push_back(op.zM+1); | |
vop.push_back(op); | |
} | |
// part 1 | |
for(auto& op : vop) | |
{ | |
if(op.xm < -50 || op.xM > 50 || op.ym < -50 || op.yM > 50 || op.zm < -50 || op.zM > 50) | |
continue; | |
for(int x = op.xm; x <= op.xM; x++) | |
for(int y = op.ym; y <= op.yM; y++) | |
for(int z = op.zm; z <= op.zM; z++) | |
cube[x+50][y+50][z+50] = op.sw; | |
} | |
int count = 0; | |
for(int x = 0; x <= 100; x++) | |
for(int y = 0; y <= 100; y++) | |
for(int z = 0; z <= 100; z++) | |
count += cube[x][y][z]; | |
cout << count << endl; | |
// part 2 | |
sort(xs.begin(), xs.end()); | |
sort(ys.begin(), ys.end()); | |
sort(zs.begin(), zs.end()); | |
vector<vector<vector<int>>> dcube; | |
dcube.resize(xs.size(), vector<vector<int>>(ys.size(), vector<int>(zs.size()))); | |
for(auto& op : vop) | |
{ | |
int xm = lower_bound(xs.begin(), xs.end(), op.xm) - xs.begin(), | |
xM = lower_bound(xs.begin(), xs.end(), op.xM+1) - xs.begin(), | |
ym = lower_bound(ys.begin(), ys.end(), op.ym) - ys.begin(), | |
yM = lower_bound(ys.begin(), ys.end(), op.yM+1) - ys.begin(), | |
zm = lower_bound(zs.begin(), zs.end(), op.zm) - zs.begin(), | |
zM = lower_bound(zs.begin(), zs.end(), op.zM+1) - zs.begin(); | |
for(int xi = xm; xi < xM; xi++) | |
for(int yi = ym; yi < yM; yi++) | |
for(int zi = zm; zi < zM; zi++) | |
dcube[xi][yi][zi] = op.sw; | |
} | |
int64_t allCount = 0; | |
for(int xi = 0; xi < xs.size() - 1; xi++) | |
for(int yi = 0; yi < ys.size() - 1; yi++) | |
for(int zi = 0; zi < zs.size() - 1; zi++) | |
if(dcube[xi][yi][zi]) | |
allCount += (int64_t)(xs[xi+1] - xs[xi]) * (ys[yi+1] - ys[yi]) * (zs[zi+1] - zs[zi]); | |
cout << allCount << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment