From 17b0648d1518b34d2e60a4f0742a5141c7c9862e Mon Sep 17 00:00:00 2001 From: Leon Vatthauer Date: Mon, 9 Dec 2024 13:15:22 +0100 Subject: [PATCH] solve day 8 --- 08/example | 12 ++++ 08/input | 50 +++++++++++++++ 08/main.cc | 181 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 243 insertions(+) create mode 100644 08/example create mode 100644 08/input create mode 100644 08/main.cc diff --git a/08/example b/08/example new file mode 100644 index 0000000..de0f909 --- /dev/null +++ b/08/example @@ -0,0 +1,12 @@ +............ +........0... +.....0...... +.......0.... +....0....... +......A..... +............ +............ +........A... +.........A.. +............ +............ \ No newline at end of file diff --git a/08/input b/08/input new file mode 100644 index 0000000..a25d789 --- /dev/null +++ b/08/input @@ -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................ \ No newline at end of file diff --git a/08/main.cc b/08/main.cc new file mode 100644 index 0000000..3701d3e --- /dev/null +++ b/08/main.cc @@ -0,0 +1,181 @@ +#include +#include +#include +#include +#include + +using namespace std; + +vector> parseGrid(const char *path) { + vector> grid; + ifstream input(path); + string line; + char c; + + while (getline(input, line)) { + stringstream line_stream; + line_stream << line; + vector row; + while (line_stream >> c) { + row.push_back(c); + } + grid.push_back(row); + } + return grid; +} + +void printGrid(vector> 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> grid, vector> 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> grid, vector> &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> grid, vector> &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> grid = parseGrid(path); + vector> antinodes; + // initialize antinodes grid + for (int y = 0; y < grid.size(); y++) { + vector row; + for (int x = 0; x < grid[y].size(); x++) { + row.push_back(false); + } + antinodes.push_back(row); + } + + set> 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> grid = parseGrid(path); + vector> antinodes; + // initialize antinodes grid + for (int y = 0; y < grid.size(); y++) { + vector row; + for (int x = 0; x < grid[y].size(); x++) { + row.push_back(false); + } + antinodes.push_back(row); + } + + set> 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; +} \ No newline at end of file