-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday11.cpp
More file actions
103 lines (84 loc) · 2.22 KB
/
Copy pathday11.cpp
File metadata and controls
103 lines (84 loc) · 2.22 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
using namespace std;
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <math.h>
#include <numeric>
#include <queue>
#include "Helpers/HelperFunctions.h"
struct coord {
int x;
int y;
bool operator=(const coord &o) const {
return x == o.x && y == o.y;
}
bool operator<(const coord &o) const {
return x < o.x || (x == o.x && y < o.y);
}
};
bool operator==(const coord& lhs, const coord& rhs) {
return (lhs.x == rhs.x && lhs.y == rhs.y);
}
coord operator-(const coord& lhs, const coord& rhs) {
return {lhs.x - rhs.x, lhs.y - rhs.y};
}
ostream& operator<<(ostream& os, const coord &o) {
return os << o.x << "," << o.y;
}
int main() {
cout << "Day 11" << endl;
ifstream file(R"(C:\Users\gwen\Documents\2_Programming\Advent of Code\Advent of Code 2023\inputs\day11.txt)");
string str;
vector<string> data;
while (getline(file, str)) {
cout << str << endl;
vector<string> out;
data.push_back(str);
}
map<int, long long int> xExpansion;
map<int, long long int> yExpansion;
int expansionFactor = 1000000;
xExpansion[0] = 0;
yExpansion[0] = 0;
for (int i = 1; i < data.size(); i++) { // y
if (!data[i].contains('#')) {
yExpansion[i] = yExpansion[i - 1] + expansionFactor;
} else {
yExpansion[i] = yExpansion[i - 1] + 1;
}
}
vector<int> spots;
for (int x = 1; x < data[0].size(); x++) {
bool contains = false;
for (int y = 0; y < data.size(); y++) {
if (data[y][x] == '#') {
contains = true;
break;
}
}
if (!contains) {
xExpansion[x] = xExpansion[x - 1] + expansionFactor;
} else {
xExpansion[x] = xExpansion[x - 1] + 1;
}
}
vector<coord> galaxies;
for (int y = 0; y < data.size(); y++) {
for (int x = 0; x < data[0].size(); x++) {
if (data[y][x] == '#') {
galaxies.push_back({x,y});
}
}
}
long long int sum = 0;
for (int i = 0; i < galaxies.size() - 1; i++) {
for (int j = i + 1; j < galaxies.size(); j++) {
sum += abs(xExpansion[galaxies[i].x] - xExpansion[galaxies[j].x]) + abs(yExpansion[galaxies[i].y] - yExpansion[galaxies[j].y]);
}
}
cout << "Part 1: " << sum << endl;
return 0;
}