solve day 8

This commit is contained in:
Leon Vatthauer 2024-12-09 13:15:22 +01:00
parent 3c68620dea
commit 17b0648d15
Signed by: leonv
SSH key fingerprint: SHA256:G4+ddwoZmhLPRB1agvXzZMXIzkVJ36dUYZXf5NxT+u8
3 changed files with 243 additions and 0 deletions

12
08/example Normal file
View file

@ -0,0 +1,12 @@
............
........0...
.....0......
.......0....
....0.......
......A.....
............
............
........A...
.........A..
............
............

50
08/input Normal file
View file

@ -0,0 +1,50 @@
...........V..................b.g.................
..................................g...............
.............................c....................
............T........Z.......P....................
.x........................VP......................
..........................PH......................
.................H.....Z.......g.R................
......f............T.V....b......A................
......................P...........................
.......f..................A.............R.........
........x..............T.......l..H.....A.c.......
..k..x..............Z.............................
........5....S...............0.A..................
.............N....L...............................
.f............................T........s.....N....
..................l..........bH.......tc.R..N.....
......Z...6......n......l...k.N...0...............
...........g....S......l.r.................t..s...
..L................b.......K..t...................
................5....n........0.............c.....
.....L......n............................E........
.k.......L................m.....................Es
..............St.....5....Rm......................
............6..5...................3...0..........
...........k.................W........3...........
................n......K...E....2S..........3.....
....................................E....Q........
..........M.....x...............K.................
..h.............................1.................
.6............z..............4...e.........WY....y
........f............a.......Y..y...s.............
...h............r.............v....m..............
.....h.................v....m.....Y.Q.....W3......
.........................Yq....Q.................7
.........6..............7.................9.......
...................X..........y..q.....2..........
............r..............q.....y...........7.8..
..B..............M....4............9..............
...1.......M...X.......CGzp...4..B...2..K.........
.....................z...v....Q.....8...........9.
B.......X.F....rM...v...............2...8..D......
h1..............................7..D.....8....d...
...............F.....................9D....4....d.
..........a......p............F.........W.D......d
.........................G..C...........q.........
...B..................................C...........
.........w..........z....p.....................e..
.a............G....w........p........F........e...
........a...w.....................................
........w...............XC.......G................

181
08/main.cc Normal file
View file

@ -0,0 +1,181 @@
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
#include <set>
using namespace std;
vector<vector<char>> parseGrid(const char *path) {
vector<vector<char>> grid;
ifstream input(path);
string line;
char c;
while (getline(input, line)) {
stringstream line_stream;
line_stream << line;
vector<char> row;
while (line_stream >> c) {
row.push_back(c);
}
grid.push_back(row);
}
return grid;
}
void printGrid(vector<vector<char>> grid) {
for (int y = 0; y < grid.size(); y++) {
for (int x = 0; x < grid[y].size(); x++) {
cout << grid[y][x];
}
cout << endl;
}
}
void printAntinodes(vector<vector<char>> grid, vector<vector<bool>> antinodes) {
for (int y = 0; y < antinodes.size(); y++) {
for (int x = 0; x < antinodes[y].size(); x++) {
if (antinodes[y][x]) {
cout << '#';
} else {
cout << grid[y][x];
}
}
cout << endl;
}
}
int countAntinodes(vector<vector<char>> grid, vector<vector<bool>> &antinodes, int y1, int x1, char c) {
int sum = 0;
for (int y2 = 0; y2 < grid.size(); y2++) {
for (int x2 = 0; x2 < grid[y2].size(); x2++) {
if (grid[y2][x2] == c && y1 != y2 && x1 != x2) {
// distance vector
int x_delta = x1 - x2;
int y_delta = y1 - y2;
// first antinode
int x3 = x1 + x_delta;
int y3 = y1 + y_delta;
if (x3 >= 0 && x3 < grid[0].size() && y3 >= 0 && y3 < grid.size() && !antinodes[y3][x3]) {
antinodes[y3][x3] = true;
sum++;
}
// second antinode
x3 = x2 - x_delta;
y3 = y2 - y_delta;
if (x3 >= 0 && x3 < grid[0].size() && y3 >= 0 && y3 < grid.size() && !antinodes[y3][x3]) {
antinodes[y3][x3] = true;
sum++;
}
}
}
}
// cout << "Antinodes after " << c << ":" << endl;
// printAntinodes(grid, antinodes);
return sum;
}
int countAntinodesAdjusted(vector<vector<char>> grid, vector<vector<bool>> &antinodes, int y1, int x1, char c) {
int sum = 0;
for (int y2 = 0; y2 < grid.size(); y2++) {
for (int x2 = 0; x2 < grid[y2].size(); x2++) {
if (grid[y2][x2] == c && y1 != y2 && x1 != x2) {
// distance vector
int x_delta = x1 - x2;
int y_delta = y1 - y2;
int x3 = x1;
int y3 = y1;
while (x3 >= 0 && x3 < grid[0].size() && y3 >= 0 && y3 < grid.size()) {
if(!antinodes[y3][x3]) {
sum++;
antinodes[y3][x3] = true;
}
x3 += x_delta;
y3 += y_delta;
}
x3 = x2;
y3 = y2;
while (x3 >= 0 && x3 < grid[0].size() && y3 >= 0 && y3 < grid.size()) {
if(!antinodes[y3][x3]) {
sum++;
antinodes[y3][x3] = true;
}
x3 -= x_delta;
y3 -= y_delta;
}
}
}
}
// cout << "Antinodes after " << c << ":" << endl;
// printAntinodes(grid, antinodes);
return sum;
}
int part1(const char *path) {
vector<vector<char>> grid = parseGrid(path);
vector<vector<bool>> antinodes;
// initialize antinodes grid
for (int y = 0; y < grid.size(); y++) {
vector<bool> row;
for (int x = 0; x < grid[y].size(); x++) {
row.push_back(false);
}
antinodes.push_back(row);
}
set<char, greater<char>> visited;
int sum = 0;
for (int y = 0; y < grid.size(); y++) {
for (int x = 0; x < grid[y].size(); x++) {
if (grid[y][x] != '.' && visited.count(grid[y][x]) == 0) {
sum += countAntinodes(grid, antinodes, y, x, grid[y][x]);
}
}
}
return sum;
}
int part2(const char *path) {
vector<vector<char>> grid = parseGrid(path);
vector<vector<bool>> antinodes;
// initialize antinodes grid
for (int y = 0; y < grid.size(); y++) {
vector<bool> row;
for (int x = 0; x < grid[y].size(); x++) {
row.push_back(false);
}
antinodes.push_back(row);
}
set<char, greater<char>> visited;
int sum = 0;
for (int y = 0; y < grid.size(); y++) {
for (int x = 0; x < grid[y].size(); x++) {
if (grid[y][x] != '.' && visited.count(grid[y][x]) == 0) {
sum += countAntinodesAdjusted(grid, antinodes, y, x, grid[y][x]);
}
}
}
return sum;
}
int main(int argc, char *argv[]) {
cout << "example1: " << part1("example") << endl;
cout << "input1: " << part1("input") << endl;
cout << "example2: " << part2("example") << endl;
cout << "input2: " << part2("input") << endl;
return 0;
}