aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2023-12-26 14:41:03 +0100
committerThomas Schmucker <ts@its1.de>2023-12-26 14:41:03 +0100
commit64a43bcf7657c612f2d7bb0099610d59bf2a7b97 (patch)
tree6d7a0919cb8b87589278c6fd1ecb2d8ea8797933 /src
parentfc989f4274f02aed8b895dcb9412dad6280ba55f (diff)
downloadadvent-of-code-64a43bcf7657c612f2d7bb0099610d59bf2a7b97.tar.gz
advent-of-code-64a43bcf7657c612f2d7bb0099610d59bf2a7b97.tar.bz2
advent-of-code-64a43bcf7657c612f2d7bb0099610d59bf2a7b97.zip
Lösungen für Tag 22, Teil 1
Diffstat (limited to 'src')
-rw-r--r--src/day22.cpp98
1 files changed, 98 insertions, 0 deletions
diff --git a/src/day22.cpp b/src/day22.cpp
new file mode 100644
index 0000000..0ac5faf
--- /dev/null
+++ b/src/day22.cpp
@@ -0,0 +1,98 @@
1#include <fstream>
2#include <iostream>
3#include <map>
4#include <regex>
5#include <set>
6#include <string>
7#include <vector>
8using namespace std;
9
10struct piece {
11 int x1, y1, z1;
12 int x2, y2, z2;
13};
14
15vector<piece>
16read_file(string_view filename)
17{
18 static const regex pattern{ R"((\d+),(\d+),(\d+)~(\d+),(\d+),(\d+))" };
19
20 fstream input{ filename };
21 vector<piece> data;
22
23 for ( string line; getline(input, line); ) {
24 smatch matches;
25 if ( regex_search(line, matches, pattern) ) {
26 data.emplace_back(piece{
27 stoi(matches[1]),
28 stoi(matches[2]),
29 stoi(matches[3]),
30 stoi(matches[4]),
31 stoi(matches[5]),
32 stoi(matches[6]) });
33 }
34 }
35 sort(data.begin(), data.end(), [](const auto& lhs, const auto& rhs) { return lhs.z1 < rhs.z1; });
36
37 return data;
38}
39
40void
41part1()
42{
43 auto puzzle = read_file("data/day22.txt");
44
45 map<int, map<int, int>> grid;
46
47 map<size_t, vector<size_t>> supported_by;
48 map<size_t, vector<size_t>> supports;
49
50 for ( size_t idx = 0; idx != puzzle.size(); ++idx ) {
51 auto& piece = puzzle[idx];
52
53 int max_z = 0;
54 for ( auto x = piece.x1; x <= piece.x2; ++x ) {
55 for ( auto y = piece.y1; y <= piece.y2; ++y ) {
56 max_z = max(max_z, grid[x][y]);
57 }
58 }
59
60 auto height = piece.z2 - piece.z1 + 1;
61
62 for ( auto x = piece.x1; x <= piece.x2; ++x ) {
63 for ( auto y = piece.y1; y <= piece.y2; ++y ) {
64 grid[x][y] = max_z + height;
65 }
66 }
67
68 piece.z1 = max_z + 1;
69 piece.z2 = max_z + height;
70
71 for ( size_t idx2 = idx; idx2-- > 0; ) {
72 if ( piece.x1 > puzzle[idx2].x2 || puzzle[idx2].x1 > piece.x2 ) {
73 continue;
74 }
75 if ( piece.y1 > puzzle[idx2].y2 || puzzle[idx2].y1 > piece.y2 ) {
76 continue;
77 }
78 if ( puzzle[idx].z1 == puzzle[idx2].z2 + 1 ) {
79 supports[idx2].emplace_back(idx);
80 supported_by[idx].emplace_back(idx2);
81 }
82 }
83 }
84
85 long number = 0;
86 for ( size_t idx = 0; idx != puzzle.size(); ++idx ) {
87 if ( all_of(supports[idx].begin(), supports[idx].end(), [&](const auto& item) { return supported_by[item].size() > 1; }) ) {
88 ++number;
89 }
90 }
91 cout << number << endl;
92}
93
94int
95main()
96{
97 part1();
98}