Skip to content

Commit 84cc2a9

Browse files
committed
Add 2016 days 22-25 cpp
1 parent b2bdcdf commit 84cc2a9

13 files changed

Lines changed: 1002 additions & 5 deletions

2016/README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ This folder contains solutions to each of the problems in Advent of Code 2016 in
2525
| <nobr> [Day 19: An Elephant Named Joseph](https://adventofcode.com/2016/day/19) </nobr> | <nobr> [Part 1](/2016/cpp/day_19a.cpp) [Part 2](/2016/cpp/day_19b.cpp) </nobr> |[Link](/2016/input/day_19_input)|[Link](/2016/sample_input/day_19_sample_input)|[Link](/2016/puzzles/day_19_puzzle)|
2626
| <nobr> [Day 20: Firewall Rules](https://adventofcode.com/2016/day/20) </nobr> | <nobr> [Part 1](/2016/cpp/day_20a.cpp) [Part 2](/2016/cpp/day_20b.cpp) </nobr> |[Link](/2016/input/day_20_input)|[Link](/2016/sample_input/day_20_sample_input)|[Link](/2016/puzzles/day_20_puzzle)|
2727
| <nobr> [Day 21: Scrambled Letters and Hash](https://adventofcode.com/2016/day/21) </nobr> | <nobr> [Part 1](/2016/cpp/day_21a.cpp) [Part 2](/2016/cpp/day_21b.cpp) </nobr> |[Link](/2016/input/day_21_input)|[Link](/2016/sample_input/day_21_sample_input)|[Link](/2016/puzzles/day_21_puzzle)|
28-
| <nobr> [Day 22: ](https://adventofcode.com/2016/day/22) </nobr> | <nobr> [Part 1](/2016/cpp/day_22a.cpp) [Part 2](/2016/cpp/day_22b.cpp) </nobr> |[Link](/2016/input/day_22_input)|[Link](/2016/sample_input/day_22_sample_input)|[Link](/2016/puzzles/day_22_puzzle)|
29-
| <nobr> [Day 23: ](https://adventofcode.com/2016/day/23) </nobr> | <nobr> [Part 1](/2016/cpp/day_23a.cpp) [Part 2](/2016/cpp/day_23b.cpp) </nobr> |[Link](/2016/input/day_23_input)|[Link](/2016/sample_input/day_23_sample_input)|[Link](/2016/puzzles/day_23_puzzle)|
30-
| <nobr> [Day 24: ](https://adventofcode.com/2016/day/24) </nobr> | <nobr> [Part 1](/2016/cpp/day_24a.cpp) [Part 2](/2016/cpp/day_24b.cpp) </nobr> |[Link](/2016/input/day_24_input)|[Link](/2016/sample_input/day_24_sample_input)|[Link](/2016/puzzles/day_24_puzzle)|
31-
| <nobr> [Day 25: ](https://adventofcode.com/2016/day/25) </nobr> | <nobr> [Part 1](/2016/cpp/day_25a.cpp) </nobr> | [Link](/2016/input/day_25_input)|[Link](/2016/sample_input/day_25_sample_input)|[Link](/2016/puzzles/day_25_puzzle)|
28+
| <nobr> [Day 22: Grid Computing](https://adventofcode.com/2016/day/22) </nobr> | <nobr> [Part 1](/2016/cpp/day_22a.cpp) [Part 2](/2016/cpp/day_22b.cpp) </nobr> |[Link](/2016/input/day_22_input)|[Link](/2016/sample_input/day_22_sample_input)|[Link](/2016/puzzles/day_22_puzzle)|
29+
| <nobr> [Day 23: Safe Cracking](https://adventofcode.com/2016/day/23) </nobr> | <nobr> [Part 1](/2016/cpp/day_23a.cpp) [Part 2](/2016/cpp/day_23b.cpp) </nobr> |[Link](/2016/input/day_23_input)|[Link](/2016/sample_input/day_23_sample_input)|[Link](/2016/puzzles/day_23_puzzle)|
30+
| <nobr> [Day 24: Air Duct Spelunking](https://adventofcode.com/2016/day/24) </nobr> | <nobr> [Part 1](/2016/cpp/day_24a.cpp) [Part 2](/2016/cpp/day_24b.cpp) </nobr> |[Link](/2016/input/day_24_input)|[Link](/2016/sample_input/day_24_sample_input)|[Link](/2016/puzzles/day_24_puzzle)|
31+
| <nobr> [Day 25: Clock Signal](https://adventofcode.com/2016/day/25) </nobr> | <nobr> [Part 1](/2016/cpp/day_25a.cpp) </nobr> | [Link](/2016/input/day_25_input)|[Link](/2016/sample_input/day_25_sample_input)|[Link](/2016/puzzles/day_25_puzzle)|

2016/cpp/day_22a.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#include <algorithm>
2+
#include <fstream>
3+
#include <iostream>
4+
#include <string>
5+
#include <ranges>
6+
#include <regex>
7+
#include <vector>
8+
9+
struct Node {
10+
std::string name;
11+
int x;
12+
int y;
13+
int size;
14+
int used;
15+
int avail;
16+
int p_used;
17+
18+
};
19+
20+
int sv_to_int (const std::string_view sv) {
21+
bool negative = (sv[0] == '-') ? true : false;
22+
int n = 0;
23+
for (int i = negative ? 1 : 0; i < sv.size(); i++) {
24+
n = n * 10 + (sv[i] - '0');
25+
}
26+
if (negative) {
27+
n *= -1;
28+
}
29+
return n;
30+
}
31+
32+
int main(int argc, char* argv[]) {
33+
std::string input = "../input/day_22_input";
34+
if (argc > 1) {
35+
input = argv[1];
36+
}
37+
38+
std::ifstream file(input);
39+
std::string line;
40+
std::getline(file, line);
41+
std::getline(file, line);
42+
std::vector<Node> nodes;
43+
const std::regex pattern(R"((/dev/grid/node-x([0-9]+)-y([0-9]+))[ ]+([0-9]+)T[ ]+([0-9]+)T[ ]+([0-9]+)T[ ]+([0-9]+)%)");
44+
while(std::getline(file, line)) {
45+
std::cout << line << '\n';
46+
std::smatch match;
47+
std::regex_search(line, match, pattern);
48+
for(const auto ele : match) {
49+
std::cout << ele << ' ';
50+
}
51+
std::cout << '\n';
52+
Node node;
53+
node.name = match[1];
54+
node.x = std::stoi(match[2]);
55+
node.y = std::stoi(match[3]);
56+
node.size = std::stoi(match[4]);
57+
node.used = std::stoi(match[5]);
58+
node.avail = std::stoi(match[6]);
59+
node.p_used = std::stoi(match[7]);
60+
nodes.push_back(node);
61+
}
62+
int count = 0;
63+
for (int idx_1 = 0; idx_1 < nodes.size(); idx_1++) {
64+
if (nodes[idx_1].used == 0) continue;
65+
for (int idx_2 = 0; idx_2 < nodes.size(); idx_2++) {
66+
if (idx_2 == idx_1) continue;
67+
if (nodes[idx_2].avail >= nodes[idx_1].used) {
68+
count++;
69+
}
70+
}
71+
}
72+
std::cout << count << '\n';
73+
return 0;
74+
}

2016/cpp/day_22b.cpp

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
#include <algorithm>
2+
#include <fstream>
3+
#include <iostream>
4+
#include <string>
5+
#include <queue>
6+
#include <ranges>
7+
#include <regex>
8+
#include <unordered_set>
9+
#include <vector>
10+
#include <iomanip>
11+
12+
// Solve by hand.
13+
// Code to print map then replicate solve by hand solution
14+
15+
struct Node {
16+
int x;
17+
int y;
18+
int size;
19+
int used;
20+
int avail;
21+
int p_used;
22+
std::string name;
23+
};
24+
25+
struct NodeMove {
26+
Node node;
27+
int move;
28+
bool operator == (const NodeMove& nm) const {
29+
return nm.node.x == node.x && nm.node.y == node.y;
30+
}
31+
};
32+
33+
struct NodeMoveHash {
34+
std::size_t operator () (const NodeMove& nm) const {
35+
return nm.node.x + nm.node.y * 100;
36+
}
37+
};
38+
39+
int sv_to_int (const std::string_view sv) {
40+
bool negative = (sv[0] == '-') ? true : false;
41+
int n = 0;
42+
for (int i = negative ? 1 : 0; i < sv.size(); i++) {
43+
n = n * 10 + (sv[i] - '0');
44+
}
45+
if (negative) {
46+
n *= -1;
47+
}
48+
return n;
49+
}
50+
51+
const std::array<Node, 4> moves {
52+
Node(0, 1, 0, 0, 0, 0, ""),
53+
Node(1, 0, 0, 0, 0, 0, ""),
54+
Node(0, -1, 0, 0, 0, 0, ""),
55+
Node(-1, 0, 0, 0, 0, 0, "")
56+
};
57+
58+
int search (const Node& start_node, const Node& goal_node, const std::vector<Node>& nodes, const int max_x, const int max_y) {
59+
NodeMove start;
60+
start.node = start_node;
61+
start.move = 0;
62+
std::unordered_set<NodeMove, NodeMoveHash> seen;
63+
std::queue<NodeMove> q;
64+
q.push(start);
65+
while (!q.empty()) {
66+
const auto current = q.front();
67+
q.pop();
68+
if (seen.find(current) != seen.end()) continue;
69+
seen.insert(current);
70+
if (current.node.x == goal_node.x && current.node.y == goal_node.y) {
71+
return current.move;
72+
}
73+
for (const auto& move : moves) {
74+
Node next;
75+
next.x = current.node.x + move.x;
76+
next.y = current.node.y + move.y;
77+
if (next.x < 0 || next.x > max_x || next.y < 0 || next.y > max_y) continue;
78+
next = *std::find_if(std::begin(nodes), std::end(nodes), [&next](const auto& node) {return node.x == next.x && node.y == next.y; });
79+
if (next.used > 99) continue; // Assume obstacle
80+
NodeMove next_nm;
81+
next_nm.node = next;
82+
next_nm.move = current.move + 1;
83+
if (seen.find(next_nm) != seen.end()) continue;
84+
85+
q.push(next_nm);
86+
}
87+
}
88+
return -1;
89+
}
90+
91+
int main(int argc, char* argv[]) {
92+
std::string input = "../input/day_22_input";
93+
if (argc > 1) {
94+
input = argv[1];
95+
}
96+
97+
std::ifstream file(input);
98+
std::string line;
99+
std::getline(file, line);
100+
std::getline(file, line);
101+
std::vector<Node> nodes;
102+
const std::regex pattern(R"((/dev/grid/node-x([0-9]+)-y([0-9]+))[ ]+([0-9]+)T[ ]+([0-9]+)T[ ]+([0-9]+)T[ ]+([0-9]+)%)");
103+
int max_x = 0;
104+
int max_y = 0;
105+
while(std::getline(file, line)) {
106+
// std::cout << line << '\n';
107+
std::smatch match;
108+
std::regex_search(line, match, pattern);
109+
// for(const auto ele : match) {
110+
// std::cout << ele << ' ';
111+
// }
112+
// std::cout << '\n';
113+
Node node;
114+
node.name = match[1];
115+
node.x = std::stoi(match[2]);
116+
max_x = std::max(node.x, max_x);
117+
node.y = std::stoi(match[3]);
118+
max_y = std::max(node.y, max_y);
119+
node.size = std::stoi(match[4]);
120+
node.used = std::stoi(match[5]);
121+
node.avail = std::stoi(match[6]);
122+
node.p_used = std::stoi(match[7]);
123+
nodes.push_back(node);
124+
}
125+
auto map = std::vector<std::vector<std::pair<int, int>>>(max_y+1,
126+
std::vector<std::pair<int, int>>(
127+
max_x+1, {0, 0}
128+
)
129+
);
130+
for (const auto& node : nodes) {
131+
map[node.y][node.x] = {node.used, node.size};
132+
}
133+
for (const auto& row : map) {
134+
for (const auto& ele : row) {
135+
std::cout << std::setw(3) << ele.first ;//<< "|" << ele.second;
136+
}
137+
std::cout << '\n';
138+
}
139+
140+
Node empty;
141+
Node start;
142+
Node goal;
143+
for (const auto& node : nodes) {
144+
if (node.used == 0) { // This might be a small value?
145+
empty = node;
146+
} else if (node.x == max_x && node.y == 0) {
147+
start = node;
148+
} else if (node.x == 0 && node.y == 0) {
149+
goal = node;
150+
}
151+
}
152+
153+
// Get the empty node to the top right corner
154+
// Then move the data in the top right corner to the top left
155+
// The assumption here is that to move the data one step, the empty node will need a minimum of 5 moves.
156+
// Start: | Move 1: | Move 2: | Move 3: | Move 4: | Move 5: | Repeat
157+
// . D E . | . D . . | . D . . | . D . . | E D . . | D E . . |
158+
// . . . . | . . E . | . E . . | E . . . | . . . . | . . . . |
159+
// Number of steps -1 as this is not necessary in the first move when moving data out of the top right corner
160+
// Assume that the minimum is always possible.
161+
std::cout << search(empty, start, nodes, max_x, max_y) + (search (start, goal, nodes, max_x, max_y) - 1 ) * 5 << '\n';
162+
return 0;
163+
}
164+

0 commit comments

Comments
 (0)