Skip to content

Commit 5e84b44

Browse files
committed
Add 2017 day 19 cpp
1 parent 4b871d7 commit 5e84b44

File tree

4 files changed

+207
-1
lines changed

4 files changed

+207
-1
lines changed

2017/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ This folder contains solutions to each of the problems in Advent of Code 2017 in
2626
| <nobr> [Day 16: Permutation Promenade](https://adventofcode.com/2017/day/16) </nobr> | <nobr> [Part 1](/2017/cpp/day_16a.cpp) [Part 2](/2017/cpp/day_16b.cpp) </nobr> |[Link](/2017/input/day_16_input)|[Link](/2017/sample_input/day_16_sample_input)|[Link](/2017/puzzles/day_16_puzzle)|
2727
| <nobr> [Day 17: Spinlock](https://adventofcode.com/2017/day/17) </nobr> | <nobr> [Part 1](/2017/cpp/day_17a.cpp) [Part 2](/2017/cpp/day_17b.cpp) </nobr> |[Link](/2017/input/day_17_input)|[Link](/2017/sample_input/day_17_sample_input)|[Link](/2017/puzzles/day_17_puzzle)|
2828
| <nobr> [Day 18: Duet](https://adventofcode.com/2017/day/18) </nobr> | <nobr> [Part 1](/2017/cpp/day_18a.cpp) [Part 2](/2017/cpp/day_18b.cpp) </nobr> |[Link](/2017/input/day_18_input)|[Link](/2017/sample_input/day_18_sample_input)|[Link](/2017/puzzles/day_18_puzzle)|
29+
| <nobr> [Day 19: A Series of Tubes](https://adventofcode.com/2017/day/19) </nobr> | <nobr> [Part 1](/2017/cpp/day_19a.cpp) [Part 2](/2017/cpp/day_19b.cpp) </nobr> |[Link](/2017/input/day_19_input)|[Link](/2017/sample_input/day_19_sample_input)|[Link](/2017/puzzles/day_19_puzzle)|
2930

3031
#### TODO ####
3132

3233
|Puzzle|C++ Solutions|Input|Sample Input|Puzzle page with solutions|
3334
|:---:|:---:|:---:|:---:|:---:|
34-
| <nobr> [Day 19: ](https://adventofcode.com/2017/day/19) </nobr> | <nobr> [Part 1](/2017/cpp/day_19a.cpp) [Part 2](/2017/cpp/day_19b.cpp) </nobr> |[Link](/2017/input/day_19_input)|[Link](/2017/sample_input/day_19_sample_input)|[Link](/2017/puzzles/day_19_puzzle)|
3535
| <nobr> [Day 20: ](https://adventofcode.com/2017/day/20) </nobr> | <nobr> [Part 1](/2017/cpp/day_20a.cpp) [Part 2](/2017/cpp/day_20b.cpp) </nobr> |[Link](/2017/input/day_20_input)|[Link](/2017/sample_input/day_20_sample_input)|[Link](/2017/puzzles/day_20_puzzle)|
3636
| <nobr> [Day 21: ](https://adventofcode.com/2017/day/21) </nobr> | <nobr> [Part 1](/2017/cpp/day_21a.cpp) [Part 2](/2017/cpp/day_21b.cpp) </nobr> |[Link](/2017/input/day_21_input)|[Link](/2017/sample_input/day_21_sample_input)|[Link](/2017/puzzles/day_21_puzzle)|
3737
| <nobr> [Day 22: ](https://adventofcode.com/2017/day/22) </nobr> | <nobr> [Part 1](/2017/cpp/day_22a.cpp) [Part 2](/2017/cpp/day_22b.cpp) </nobr> |[Link](/2017/input/day_22_input)|[Link](/2017/sample_input/day_22_sample_input)|[Link](/2017/puzzles/day_22_puzzle)|

2017/cpp/day_19a.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include <algorithm>
2+
#include <fstream>
3+
#include <iostream>
4+
#include <string>
5+
#include <limits>
6+
#include <unordered_map>
7+
#include <unordered_set>
8+
#include <vector>
9+
#include <regex>
10+
#include <cmath>
11+
#include <queue>
12+
13+
struct Coord {
14+
int row;
15+
int col;
16+
};
17+
18+
Coord get_next_point(const int dir, const Coord& current_point) {
19+
Coord next_point;
20+
if (dir == 3) {
21+
next_point.row = current_point.row;
22+
next_point.col = current_point.col + 1;
23+
} else if (dir == 0) {
24+
next_point.row = current_point.row - 1;
25+
next_point.col = current_point.col;
26+
} else if (dir == 1) {
27+
next_point.row = current_point.row;
28+
next_point.col = current_point.col - 1;
29+
} else if (dir == 2) {
30+
next_point.row = current_point.row + 1;
31+
next_point.col = current_point.col;
32+
} else {
33+
std::cout << "This should not happen" << '\n';
34+
exit(0);
35+
}
36+
return next_point;
37+
}
38+
39+
bool in_map(const Coord& current, const std::vector<std::string>& map) {
40+
return current.row >= 0 && current.row < map.size() && current.col >= 0 && current.col < map[0].size();
41+
}
42+
43+
int main(int argc, char* argv[]) {
44+
const std::string input = (argc > 1) ? argv[1] : "../input/day_19_input" ;
45+
std::ifstream file(input);
46+
std::string line;
47+
std::vector<std::string> map;
48+
while(std::getline(file, line)) {
49+
map.push_back(line);
50+
}
51+
Coord current;
52+
current.row = 0;
53+
int dir = 2;
54+
std::string letters;
55+
for (int i = 0; i < map[0].size(); i++) {
56+
if (map[0][i] == '|') {
57+
current.col = i;
58+
break;
59+
}
60+
}
61+
62+
while(true) {
63+
// std::cout << current.row << ',' << current.col << ": " << map[current.row][current.col] << '\n';
64+
// Check if this assumption is always true or whether a turn can be the only option when at a point that is a letter
65+
if(map[current.row][current.col] != '+') {
66+
current = get_next_point(dir, current);
67+
} else {
68+
const auto current_dir = dir;
69+
dir = (dir + 1) % 4;
70+
while(true) {
71+
if (dir == current_dir) {
72+
// std::cout << "Reached end" << '\n';
73+
std::cout << letters << '\n';
74+
return 0;
75+
}
76+
if ((dir + current_dir) % 2 == 0) {
77+
dir = (dir + 1) % 4;
78+
continue;
79+
}
80+
const auto new_point = get_next_point(dir, current);
81+
if (in_map(new_point, map) && map[new_point.row][new_point.col] != ' ') {
82+
current = new_point;
83+
break;
84+
}
85+
dir = (dir + 1) % 4;
86+
}
87+
}
88+
if (isalpha(map[current.row][current.col]) && !isdigit(map[current.row][current.col])) {
89+
letters += map[current.row][current.col];
90+
// std::cout << letters << '\n';
91+
}
92+
if (map[current.row][current.col] == ' ') {
93+
// std::cout << "Reached end" << '\n';
94+
std::cout << letters << '\n';
95+
return 0;
96+
}
97+
}
98+
99+
return 0;
100+
}

2017/cpp/day_19b.cpp

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
#include <algorithm>
2+
#include <fstream>
3+
#include <iostream>
4+
#include <string>
5+
#include <limits>
6+
#include <unordered_map>
7+
#include <unordered_set>
8+
#include <vector>
9+
#include <regex>
10+
#include <cmath>
11+
#include <queue>
12+
13+
struct Coord {
14+
int row;
15+
int col;
16+
};
17+
18+
Coord get_next_point(const int dir, const Coord& current_point) {
19+
Coord next_point;
20+
if (dir == 3) {
21+
next_point.row = current_point.row;
22+
next_point.col = current_point.col + 1;
23+
} else if (dir == 0) {
24+
next_point.row = current_point.row - 1;
25+
next_point.col = current_point.col;
26+
} else if (dir == 1) {
27+
next_point.row = current_point.row;
28+
next_point.col = current_point.col - 1;
29+
} else if (dir == 2) {
30+
next_point.row = current_point.row + 1;
31+
next_point.col = current_point.col;
32+
} else {
33+
std::cout << "This should not happen" << '\n';
34+
exit(0);
35+
}
36+
return next_point;
37+
}
38+
39+
bool in_map(const Coord& current, const std::vector<std::string>& map) {
40+
return current.row >= 0 && current.row < map.size() && current.col >= 0 && current.col < map[0].size();
41+
}
42+
43+
int main(int argc, char* argv[]) {
44+
const std::string input = (argc > 1) ? argv[1] : "../input/day_19_input" ;
45+
std::ifstream file(input);
46+
std::string line;
47+
std::vector<std::string> map;
48+
while(std::getline(file, line)) {
49+
map.push_back(line);
50+
}
51+
Coord current;
52+
current.row = 0;
53+
int dir = 2;
54+
std::string letters;
55+
for (int i = 0; i < map[0].size(); i++) {
56+
if (map[0][i] == '|') {
57+
current.col = i;
58+
break;
59+
}
60+
}
61+
std::size_t steps = 0;
62+
while(true) {
63+
steps++;
64+
// std::cout << current.row << ',' << current.col << ": " << map[current.row][current.col] << '\n';
65+
// Check if this assumption is always true or whether a turn can be the only option when at a point that is a letter
66+
if(map[current.row][current.col] != '+') {
67+
current = get_next_point(dir, current);
68+
} else {
69+
const auto current_dir = dir;
70+
dir = (dir + 1) % 4;
71+
while(true) {
72+
if (dir == current_dir) {
73+
// std::cout << "Reached end" << '\n';
74+
std::cout << steps << '\n';
75+
return 0;
76+
}
77+
if ((dir + current_dir) % 2 == 0) {
78+
dir = (dir + 1) % 4;
79+
continue;
80+
}
81+
const auto new_point = get_next_point(dir, current);
82+
if (in_map(new_point, map) && map[new_point.row][new_point.col] != ' ') {
83+
current = new_point;
84+
break;
85+
}
86+
dir = (dir + 1) % 4;
87+
}
88+
}
89+
if (isalpha(map[current.row][current.col]) && !isdigit(map[current.row][current.col])) {
90+
letters += map[current.row][current.col];
91+
}
92+
if (map[current.row][current.col] == ' ') {
93+
// std::cout << "Reached end" << '\n';
94+
std::cout << steps << '\n';
95+
return 0;
96+
}
97+
}
98+
99+
return 0;
100+
}

2017/sample_input/day_19_sample_input

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
|
2+
| +--+
3+
A | C
4+
F---|----E|--+
5+
| | | D
6+
+B-+ +--+

0 commit comments

Comments
 (0)