Skip to content

Commit 027357f

Browse files
committed
Add 2017 day 22
1 parent 32b2198 commit 027357f

File tree

4 files changed

+251
-1
lines changed

4 files changed

+251
-1
lines changed

2017/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@ This folder contains solutions to each of the problems in Advent of Code 2017 in
2929
| <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)|
3030
| <nobr> [Day 20: Particle Swarm](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)|
3131
| <nobr> [Day 21: Fractal Art](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)|
32+
| <nobr> [Day 22: Sporifica Virus](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)|
3233

3334
#### TODO ####
3435

3536
|Puzzle|C++ Solutions|Input|Sample Input|Puzzle page with solutions|
3637
|:---:|:---:|:---:|:---:|:---:|
37-
| <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)|
3838
| <nobr> [Day 23: ](https://adventofcode.com/2017/day/23) </nobr> | <nobr> [Part 1](/2017/cpp/day_23a.cpp) [Part 2](/2017/cpp/day_23b.cpp) </nobr> |[Link](/2017/input/day_23_input)|[Link](/2017/sample_input/day_23_sample_input)|[Link](/2017/puzzles/day_23_puzzle)|
3939
| <nobr> [Day 24: ](https://adventofcode.com/2017/day/24) </nobr> | <nobr> [Part 1](/2017/cpp/day_24a.cpp) [Part 2](/2017/cpp/day_24b.cpp) </nobr> |[Link](/2017/input/day_24_input)|[Link](/2017/sample_input/day_24_sample_input)|[Link](/2017/puzzles/day_24_puzzle)|
4040

2017/cpp/day_22a.cpp

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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 <cassert>
11+
#include <cmath>
12+
13+
struct Coord2D {
14+
int row;
15+
int col;
16+
17+
Coord2D(const int row = 0, const int col = 0) : row(row) , col(col) {}
18+
19+
Coord2D operator + (const Coord2D& c) const {
20+
Coord2D ans;
21+
ans.row = row + c.row;
22+
ans.col = col + c.col;
23+
return ans;
24+
}
25+
26+
bool operator == (const Coord2D& c) const {
27+
return row == c.row && col == c.col;
28+
}
29+
30+
31+
32+
Coord2D operator - (const Coord2D& c) const {
33+
Coord2D ans;
34+
ans.row = row - c.row;
35+
ans.col = col - c.col;
36+
return ans;
37+
}
38+
39+
Coord2D operator += (const Coord2D& c) {
40+
row += c.row;
41+
col += c.col;
42+
return *this;
43+
}
44+
};
45+
46+
struct hasher {
47+
std::size_t operator()(const Coord2D& c) const {
48+
return 1000000 * c.row + c.col;
49+
}
50+
};
51+
52+
const std::vector<Coord2D> motion {
53+
Coord2D(-1,0),
54+
Coord2D(0,-1),
55+
Coord2D(1,0),
56+
Coord2D(0,1),
57+
};
58+
59+
void move(Coord2D& current, const int dir) {
60+
current.row += motion[dir].row;
61+
current.col += motion[dir].col;
62+
}
63+
64+
int main(int argc, char* argv[]) {
65+
const std::string input = (argc > 1) ? argv[1] : "../input/day_22_input" ;
66+
std::ifstream file(input);
67+
std::string line;
68+
std::unordered_set<Coord2D, hasher> infected;
69+
int n_cols = 0;
70+
int n_rows = 0;
71+
while(std::getline(file, line)) {
72+
if (n_cols == 0) {
73+
n_cols = line.size();
74+
}
75+
for (int i = 0; i < line.size(); i++) {
76+
if (line[i] == '#') {
77+
infected.insert(Coord2D(n_rows, i));
78+
}
79+
}
80+
n_rows++;
81+
}
82+
int count = 0;
83+
int dir = 0;
84+
auto current = Coord2D((n_rows)/2, (n_cols)/2);
85+
// std::cout <<
86+
for (int iteration = 0; iteration < 10000; iteration++) {
87+
// std::cout << "Iteration: " << iteration << " --> Infected: " << count << '\n';
88+
// if (iteration == 7) {
89+
// std::cout << iteration << ": " << count << '\n';
90+
// exit(0);
91+
// } else
92+
// if (iteration == 70) {
93+
// std::cout << iteration << ": " << count << '\n';
94+
// exit(0);
95+
// }
96+
if (infected.find(current) != infected.end()) {
97+
// std::cout << "Current node (" << current.row << ", " << current.col << ") is infected" << '\n';
98+
dir--;
99+
dir += 4;
100+
dir %= 4;
101+
infected.erase(current);
102+
} else {
103+
// std::cout << "Current node (" << current.row << ", " << current.col << ") is NOT infected" << '\n';
104+
dir += 1;
105+
dir %= 4;
106+
infected.insert(current);
107+
count++;
108+
}
109+
// std::cout << "It is now " << ((infected.find(current) != infected.end()) ? "infected" : "not infected") << '\n';
110+
move(current, dir);
111+
}
112+
std::cout << count << '\n';
113+
114+
return 0;
115+
}

2017/cpp/day_22b.cpp

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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 <cassert>
11+
#include <cmath>
12+
13+
struct Coord2D {
14+
int row;
15+
int col;
16+
17+
Coord2D(const int row = 0, const int col = 0) : row(row) , col(col) {}
18+
19+
Coord2D operator + (const Coord2D& c) const {
20+
Coord2D ans;
21+
ans.row = row + c.row;
22+
ans.col = col + c.col;
23+
return ans;
24+
}
25+
26+
bool operator == (const Coord2D& c) const {
27+
return row == c.row && col == c.col;
28+
}
29+
30+
31+
32+
Coord2D operator - (const Coord2D& c) const {
33+
Coord2D ans;
34+
ans.row = row - c.row;
35+
ans.col = col - c.col;
36+
return ans;
37+
}
38+
39+
Coord2D operator += (const Coord2D& c) {
40+
row += c.row;
41+
col += c.col;
42+
return *this;
43+
}
44+
};
45+
46+
struct hasher {
47+
std::size_t operator()(const Coord2D& c) const {
48+
return 1000000 * c.row + c.col;
49+
}
50+
};
51+
52+
const std::vector<Coord2D> motion {
53+
Coord2D(-1,0),
54+
Coord2D(0,-1),
55+
Coord2D(1,0),
56+
Coord2D(0,1),
57+
};
58+
59+
void move(Coord2D& current, const int dir) {
60+
current.row += motion[dir].row;
61+
current.col += motion[dir].col;
62+
}
63+
64+
enum class State {
65+
CLEAN,
66+
WEAKENED,
67+
INFECTED,
68+
FLAGGED
69+
};
70+
71+
int main(int argc, char* argv[]) {
72+
const std::string input = (argc > 1) ? argv[1] : "../input/day_22_input" ;
73+
std::ifstream file(input);
74+
std::string line;
75+
std::unordered_map<Coord2D, State, hasher> state;
76+
int n_cols = 0;
77+
int n_rows = 0;
78+
while(std::getline(file, line)) {
79+
if (n_cols == 0) {
80+
n_cols = line.size();
81+
}
82+
for (int i = 0; i < line.size(); i++) {
83+
if (line[i] == '#') {
84+
state.insert({Coord2D(n_rows, i), State::INFECTED});
85+
}
86+
}
87+
n_rows++;
88+
}
89+
int count = 0;
90+
int dir = 0;
91+
auto current = Coord2D((n_rows)/2, (n_cols)/2);
92+
for (int iteration = 0; iteration < 10000000; iteration++) {
93+
// std::cout << "Iteration: " << iteration << " --> Infected: " << count << '\n';
94+
// if (iteration == 100) {
95+
// std::cout << iteration << ": " << count << '\n';
96+
// exit(0);
97+
// }
98+
if (auto it = state.find(current); it != state.end()) {
99+
if (it->second == State::INFECTED) {
100+
// std::cout << "Current node (" << current.row << ", " << current.col << ") is infected" << '\n';
101+
dir--;
102+
dir += 4;
103+
dir %= 4;
104+
it->second = State::FLAGGED;
105+
// std::cout << "Current node (" << current.row << ", " << current.col << ") is now flagged" << '\n';
106+
} else if (it->second == State::WEAKENED) {
107+
// std::cout << "Current node (" << current.row << ", " << current.col << ") is weakened" << '\n';
108+
it->second = State::INFECTED;
109+
count++;
110+
// std::cout << "Current node (" << current.row << ", " << current.col << ") is now infected" << '\n';
111+
} else if (it->second == State::FLAGGED) {
112+
// std::cout << "Current node (" << current.row << ", " << current.col << ") is flagged" << '\n';
113+
dir += 2;
114+
dir %= 4;
115+
it->second = State::CLEAN;
116+
state.erase(it);
117+
// std::cout << "Current node (" << current.row << ", " << current.col << ") is now cleaned" << '\n';
118+
}
119+
} else { // CLEAN
120+
// std::cout << "Current node (" << current.row << ", " << current.col << ") is clean" << '\n';
121+
dir += 1;
122+
dir %= 4;
123+
state.insert({current, State::WEAKENED});
124+
// std::cout << "Current node (" << current.row << ", " << current.col << ") is now weakened" << '\n';
125+
}
126+
// std::cout << "Moving " << dir << '\n';
127+
move(current, dir);
128+
}
129+
std::cout << count << '\n';
130+
131+
return 0;
132+
}

2017/sample_input/day_22_sample_input

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
..#
2+
#..
3+
...

0 commit comments

Comments
 (0)