Skip to content

Commit b2bdcdf

Browse files
committedJul 20, 2024
Add 2016 day 21 cpp
1 parent b009a48 commit b2bdcdf

File tree

5 files changed

+289
-2
lines changed

5 files changed

+289
-2
lines changed
 

‎2016/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ This folder contains solutions to each of the problems in Advent of Code 2016 in
2424
| <nobr> [Day 18: Like a Rogue](https://adventofcode.com/2016/day/18) </nobr> | <nobr> [Part 1](/2016/cpp/day_18a.cpp) [Part 2](/2016/cpp/day_18b.cpp) </nobr> |[Link](/2016/input/day_18_input)|[Link](/2016/sample_input/day_18_sample_input)|[Link](/2016/puzzles/day_18_puzzle)|
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)|
27-
| <nobr> [Day 21: ](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)|
27+
| <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)|
2828
| <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)|
2929
| <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)|
3030
| <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)|

‎2016/cpp/day_21a.cpp

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#include <algorithm>
2+
#include <fstream>
3+
#include <iostream>
4+
#include <string>
5+
#include <ranges>
6+
#include <vector>
7+
#include <unordered_map>
8+
9+
int sv_to_int (const std::string_view sv) {
10+
bool negative = (sv[0] == '-') ? true : false;
11+
int n = 0;
12+
for (int i = negative ? 1 : 0; i < sv.size(); i++) {
13+
n = n * 10 + (sv[i] - '0');
14+
}
15+
if (negative) {
16+
n *= -1;
17+
}
18+
return n;
19+
}
20+
21+
int main(int argc, char* argv[]) {
22+
std::string input = "../input/day_21_input";
23+
if (argc > 1) {
24+
input = argv[1];
25+
}
26+
27+
std::ifstream file(input);
28+
std::string line;
29+
std::unordered_map<char, int> char_to_idx;
30+
std::unordered_map<int, char> idx_to_char;
31+
for (int i = 0; i <= 'h' - 'a'; i++) {
32+
char_to_idx[i + 'a'] = i;
33+
idx_to_char[i] = i + 'a';
34+
}
35+
std::vector<char> s(char_to_idx.size());
36+
for (const auto [c, idx] : char_to_idx) {
37+
s[idx] = c;
38+
}
39+
for (const auto ele : s) {
40+
std::cout << ele;
41+
}
42+
std::cout << '\n';
43+
const int n = char_to_idx.size();
44+
while(std::getline(file, line)) {
45+
// std::cout << line << '\n';
46+
std::vector<std::string_view> split_line;
47+
std::ranges::copy(line
48+
| std::ranges::views::split(' ')
49+
| std::ranges::views::transform([](auto&& rng) {return std::string_view(rng);}),
50+
std::back_inserter(split_line));
51+
if (split_line[0] == "swap" && split_line[1] == "position") {
52+
const int idx_1 = sv_to_int(split_line[2]);
53+
const int idx_2 = sv_to_int(split_line[5]);
54+
const char c1 = idx_to_char[idx_1];
55+
const char c2 = idx_to_char[idx_2];
56+
idx_to_char[idx_1] = c2;
57+
idx_to_char[idx_2] = c1;
58+
char_to_idx[c1] = idx_2;
59+
char_to_idx[c2] = idx_1;
60+
}
61+
else if (split_line[0] == "swap" && split_line[1] == "letter") {
62+
const char c1 = split_line[2][0];
63+
const char c2 = split_line[5][0];
64+
const int idx_1 = char_to_idx[c1];
65+
const int idx_2 = char_to_idx[c2];
66+
idx_to_char[idx_1] = c2;
67+
idx_to_char[idx_2] = c1;
68+
char_to_idx[c1] = idx_2;
69+
char_to_idx[c2] = idx_1;
70+
}
71+
else if (split_line[0] == "rotate" && split_line[1] == "left") {
72+
const int rotate_n = (-sv_to_int(split_line[2]) + n) % n;
73+
for (auto& [c, idx] : char_to_idx) {
74+
idx = (idx + rotate_n) % n;
75+
idx_to_char[idx] = c;
76+
}
77+
}
78+
else if (split_line[0] == "rotate" && split_line[1] == "right") {
79+
const int rotate_n = sv_to_int(split_line[2]) % n;
80+
for (auto& [c, idx] : char_to_idx) {
81+
idx = (idx + rotate_n) % n;
82+
idx_to_char[idx] = c;
83+
}
84+
}
85+
else if (split_line[0] == "rotate" && split_line[1] == "based") {
86+
const auto c_idx = char_to_idx[split_line[6][0]];
87+
const int rotate_n = 1 + c_idx + ((c_idx >= 4) ? 1 : 0);
88+
for (auto& [c, idx] : char_to_idx) {
89+
idx = (idx + rotate_n) % n;
90+
idx_to_char[idx] = c;
91+
}
92+
}
93+
else if (split_line[0] == "reverse") {
94+
const int idx_1 = sv_to_int(split_line[2]);
95+
const int idx_2 = sv_to_int(split_line[4]);
96+
for (auto& [c, idx] : char_to_idx) {
97+
if (idx >= idx_1 && idx <= idx_2) {
98+
idx = idx_2 - idx + idx_1;
99+
idx_to_char[idx] = c;
100+
}
101+
}
102+
}
103+
else if (split_line[0] == "move" && split_line[1] == "position") {
104+
const int idx_1 = sv_to_int(split_line[2]);
105+
const int idx_2 = sv_to_int(split_line[5]);
106+
char c_prime = idx_to_char[idx_1];
107+
if (idx_2 > idx_1) {
108+
for (auto& [c, idx] : char_to_idx) {
109+
if (idx > idx_1 && idx <= idx_2) {
110+
idx--;
111+
idx_to_char[idx] = c;
112+
}
113+
}
114+
char_to_idx[c_prime] = idx_2;
115+
idx_to_char[idx_2] = c_prime;
116+
} else if (idx_1 > idx_2) {
117+
for (auto& [c, idx] : char_to_idx) {
118+
if (idx >= idx_2 && idx < idx_1) {
119+
idx++;
120+
idx_to_char[idx] = c;
121+
}
122+
}
123+
char_to_idx[c_prime] = idx_2;
124+
idx_to_char[idx_2] = c_prime;
125+
}
126+
}
127+
std::vector<char> s(char_to_idx.size());
128+
for (const auto [c, idx] : char_to_idx) {
129+
s[idx] = c;
130+
}
131+
for (const auto ele : s) {
132+
std::cout << ele;
133+
}
134+
std::cout << '\n';
135+
}
136+
return 0;
137+
}

‎2016/cpp/day_21b.cpp

+142
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#include <algorithm>
2+
#include <fstream>
3+
#include <iostream>
4+
#include <string>
5+
#include <ranges>
6+
#include <vector>
7+
#include <unordered_map>
8+
9+
int sv_to_int (const std::string_view sv) {
10+
bool negative = (sv[0] == '-') ? true : false;
11+
int n = 0;
12+
for (int i = negative ? 1 : 0; i < sv.size(); i++) {
13+
n = n * 10 + (sv[i] - '0');
14+
}
15+
if (negative) {
16+
n *= -1;
17+
}
18+
return n;
19+
}
20+
21+
int main(int argc, char* argv[]) {
22+
std::string input = "../input/day_21_input";
23+
if (argc > 1) {
24+
input = argv[1];
25+
}
26+
27+
std::ifstream file(input);
28+
std::string line;
29+
std::unordered_map<char, int> char_to_idx;
30+
std::unordered_map<int, char> idx_to_char;
31+
32+
std::vector<std::vector<std::string>> instructions;
33+
std::string p = "abcdefgh";
34+
std::string result = "00000000";
35+
const int n = p.size();
36+
37+
while(std::getline(file, line)) {
38+
// std::cout << line << '\n';
39+
std::vector<std::string> split_line;
40+
std::ranges::copy(line
41+
| std::ranges::views::split(' ')
42+
| std::ranges::views::transform([](auto&& rng) {return std::string{std::string_view(rng)};}),
43+
std::back_inserter(split_line));
44+
instructions.push_back(split_line);
45+
}
46+
// Assumes a valid permutation will be found
47+
while(true){
48+
// std::cout << p << '\n';
49+
for (int i = 0; i <= p.size(); i++) {
50+
char_to_idx[p[i]] = i;
51+
idx_to_char[i] = p[i];
52+
}
53+
for (const auto& split_line : instructions) {
54+
if (split_line[0] == "swap" && split_line[1] == "position") {
55+
const int idx_1 = sv_to_int(split_line[2]);
56+
const int idx_2 = sv_to_int(split_line[5]);
57+
const char c1 = idx_to_char[idx_1];
58+
const char c2 = idx_to_char[idx_2];
59+
idx_to_char[idx_1] = c2;
60+
idx_to_char[idx_2] = c1;
61+
char_to_idx[c1] = idx_2;
62+
char_to_idx[c2] = idx_1;
63+
}
64+
else if (split_line[0] == "swap" && split_line[1] == "letter") {
65+
const char c1 = split_line[2][0];
66+
const char c2 = split_line[5][0];
67+
const int idx_1 = char_to_idx[c1];
68+
const int idx_2 = char_to_idx[c2];
69+
idx_to_char[idx_1] = c2;
70+
idx_to_char[idx_2] = c1;
71+
char_to_idx[c1] = idx_2;
72+
char_to_idx[c2] = idx_1;
73+
}
74+
else if (split_line[0] == "rotate" && split_line[1] == "left") {
75+
const int rotate_n = (-sv_to_int(split_line[2]) + n) % n;
76+
for (auto& [c, idx] : char_to_idx) {
77+
idx = (idx + rotate_n) % n;
78+
idx_to_char[idx] = c;
79+
}
80+
}
81+
else if (split_line[0] == "rotate" && split_line[1] == "right") {
82+
const int rotate_n = sv_to_int(split_line[2]) % n;
83+
for (auto& [c, idx] : char_to_idx) {
84+
idx = (idx + rotate_n) % n;
85+
idx_to_char[idx] = c;
86+
}
87+
}
88+
else if (split_line[0] == "rotate" && split_line[1] == "based") {
89+
const auto c_idx = char_to_idx[split_line[6][0]];
90+
const int rotate_n = 1 + c_idx + ((c_idx >= 4) ? 1 : 0);
91+
for (auto& [c, idx] : char_to_idx) {
92+
idx = (idx + rotate_n + n) % n;
93+
idx_to_char[idx] = c;
94+
}
95+
}
96+
else if (split_line[0] == "reverse") {
97+
const int idx_1 = sv_to_int(split_line[2]);
98+
const int idx_2 = sv_to_int(split_line[4]);
99+
for (auto& [c, idx] : char_to_idx) {
100+
if (idx >= idx_1 && idx <= idx_2) {
101+
idx = idx_2 - idx + idx_1;
102+
idx_to_char[idx] = c;
103+
}
104+
}
105+
}
106+
else if (split_line[0] == "move" && split_line[1] == "position") {
107+
const int idx_1 = sv_to_int(split_line[2]);
108+
const int idx_2 = sv_to_int(split_line[5]);
109+
char c_prime = idx_to_char[idx_1];
110+
if (idx_2 > idx_1) {
111+
for (auto& [c, idx] : char_to_idx) {
112+
if (idx > idx_1 && idx <= idx_2) {
113+
idx--;
114+
idx_to_char[idx] = c;
115+
}
116+
}
117+
char_to_idx[c_prime] = idx_2;
118+
idx_to_char[idx_2] = c_prime;
119+
} else if (idx_1 > idx_2) {
120+
for (auto& [c, idx] : char_to_idx) {
121+
if (idx >= idx_2 && idx < idx_1) {
122+
idx++;
123+
idx_to_char[idx] = c;
124+
}
125+
}
126+
char_to_idx[c_prime] = idx_2;
127+
idx_to_char[idx_2] = c_prime;
128+
}
129+
}
130+
}
131+
for (const auto [c, idx] : char_to_idx) {
132+
result[idx] = c;
133+
}
134+
if (result == "fbgdceah") {
135+
std::cout << p << '\n';
136+
return 0;
137+
}
138+
std::next_permutation(p.begin(), p.end());
139+
}
140+
std::cout << "No valid permutation found" << '\n';
141+
return 0;
142+
}

‎2016/sample_input/day_21_sample_input

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
swap position 4 with position 0
2+
swap letter d with letter b
3+
reverse positions 0 through 4
4+
rotate left 1 step
5+
move position 1 to position 4
6+
move position 3 to position 0
7+
rotate based on position of letter b
8+
rotate based on position of letter d

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ This repository contains solutions to the Advent of Code puzzles.
1313
|2019 |1-25 |1-7 | [Link](/2019/) |[Link](/2019/README.md) |
1414
|2018 |1-25 | - | [Link](/2018/) |[Link](/2018/README.md) |
1515
|2017 |1-25 | - | [Link](/2017/) |[Link](/2017/README.md) |
16-
|2017 |1-20 | - | [Link](/2016/) |[Link](/2016/README.md) |
16+
|2017 |1-21 | - | [Link](/2016/) |[Link](/2016/README.md) |
1717

1818
## To run ##
1919

0 commit comments

Comments
 (0)
Please sign in to comment.