Skip to content

Commit 6f8c424

Browse files
committed
Add 2018 day 13 cpp
1 parent c5d5fdc commit 6f8c424

File tree

5 files changed

+270
-2
lines changed

5 files changed

+270
-2
lines changed

2018/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ This folder contains solutions to each of the problems in Advent of Code 2018 in
1919
| <nobr> [Day 10: The Stars Align](https://adventofcode.com/2018/day/10) </nobr> | <nobr> [Part 1](/2018/cpp/day_10a.cpp) [Part 2](/2018/cpp/day_10b.cpp) </nobr> | <nobr> [Part 1](/2018/python/day_10a.py) [Part 2](/2018/python/day_10b.py) </nobr> |[Link](/2018/input/day_10_input)|[Link](/2018/sample_input/day_10_sample_input)|[Link](/2018/puzzles/day_10_puzzle)|
2020
| <nobr> [Day 11: Chronal Charge](https://adventofcode.com/2018/day/11) </nobr> | <nobr> [Part 1](/2018/cpp/day_11a.cpp) [Part 2](/2018/cpp/day_11b.cpp) </nobr> | <nobr> [Part 1](/2018/python/day_11a.py) [Part 2](/2018/python/day_11b.py) </nobr> |[Link](/2018/input/day_11_input)|[Link](/2018/sample_input/day_11_sample_input)|[Link](/2018/puzzles/day_11_puzzle)|
2121
| <nobr> [Day 12: Subterranean Sustainability](https://adventofcode.com/2018/day/12) </nobr> | <nobr> [Part 1](/2018/cpp/day_12a.cpp) [Part 2](/2018/cpp/day_12b.cpp) </nobr> | <nobr> [Part 1](/2018/python/day_12a.py) [Part 2](/2018/python/day_12b.py) </nobr> |[Link](/2018/input/day_12_input)|[Link](/2018/sample_input/day_12_sample_input)|[Link](/2018/puzzles/day_12_puzzle)|
22-
| <nobr> [Day 13: ](https://adventofcode.com/2018/day/13) </nobr> | <nobr> [Part 1](/2018/cpp/day_13a.cpp) [Part 2](/2018/cpp/day_13b.cpp) </nobr> | <nobr> [Part 1](/2018/python/day_13a.py) [Part 2](/2018/python/day_13b.py) </nobr> |[Link](/2018/input/day_13_input)|[Link](/2018/sample_input/day_13_sample_input)|[Link](/2018/puzzles/day_13_puzzle)|
22+
| <nobr> [Day 13: Mine Cart Madness](https://adventofcode.com/2018/day/13) </nobr> | <nobr> [Part 1](/2018/cpp/day_13a.cpp) [Part 2](/2018/cpp/day_13b.cpp) </nobr> | <nobr> [Part 1](/2018/python/day_13a.py) [Part 2](/2018/python/day_13b.py) </nobr> |[Link](/2018/input/day_13_input)|[Link](/2018/sample_input/day_13_sample_input)|[Link](/2018/puzzles/day_13_puzzle)|
2323
| <nobr> [Day 14: ](https://adventofcode.com/2018/day/14) </nobr> | <nobr> [Part 1](/2018/cpp/day_14a.cpp) [Part 2](/2018/cpp/day_14b.cpp) </nobr> | <nobr> [Part 1](/2018/python/day_14a.py) [Part 2](/2018/python/day_14b.py) </nobr> |[Link](/2018/input/day_14_input)|[Link](/2018/sample_input/day_14_sample_input)|[Link](/2018/puzzles/day_14_puzzle)|
2424
| <nobr> [Day 15: ](https://adventofcode.com/2018/day/15) </nobr> | <nobr> [Part 1](/2018/cpp/day_15a.cpp) [Part 2](/2018/cpp/day_15b.cpp) </nobr> | <nobr> [Part 1](/2018/python/day_15a.py) [Part 2](/2018/python/day_15b.py) </nobr> |[Link](/2018/input/day_15_input)|[Link](/2018/sample_input/day_15_sample_input)|[Link](/2018/puzzles/day_15_puzzle)|
2525
| <nobr> [Day 16: ](https://adventofcode.com/2018/day/16) </nobr> | <nobr> [Part 1](/2018/cpp/day_16a.cpp) [Part 2](/2018/cpp/day_16b.cpp) </nobr> | <nobr> [Part 1](/2018/python/day_16a.py) [Part 2](/2018/python/day_16b.py) </nobr> |[Link](/2018/input/day_16_input)|[Link](/2018/sample_input/day_16_sample_input)|[Link](/2018/puzzles/day_16_puzzle)|

2018/cpp/day_13a.cpp

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#include <algorithm>
2+
#include <array>
3+
#include <fstream>
4+
#include <iostream>
5+
#include <string>
6+
#include <vector>
7+
8+
struct Point {
9+
int x, y;
10+
11+
Point& operator += (const Point& v) { x += v.x; y += v.y; return *this; }
12+
Point& operator -= (const Point& v) { x -= v.x; y -= v.y; return *this; }
13+
};
14+
bool operator == (const Point& a, const Point& b) { return a.y == b.y && a.x == b.x; }
15+
bool operator != (const Point& a, const Point& b) { return a.y != b.y || a.x != b.x; }
16+
bool operator < (const Point& a, const Point& b) { return (a.y != b.y) ? (a.y < b.y) : (a.x < b.x); }
17+
18+
19+
static constexpr std::array<Point, 4> Dir = {
20+
Point{ 0, -1 }, // north
21+
Point{ 1, 0 }, // east
22+
Point{ 0, 1 }, // south
23+
Point{ -1, 0 }, // west
24+
};
25+
26+
static constexpr int North = 0;
27+
static constexpr int East = 1;
28+
static constexpr int South = 2;
29+
static constexpr int West = 3;
30+
31+
struct Cart {
32+
Point pos;
33+
int dir;
34+
int next_turn = -1;
35+
36+
void turn() {
37+
dir = (dir + 4 + next_turn) & 0x3;
38+
next_turn = ((next_turn + 2) % 3) - 1;
39+
}
40+
41+
void move() {
42+
pos += Dir[dir];
43+
}
44+
};
45+
46+
47+
bool order_by_pos(const Cart& a, const Cart& b)
48+
{
49+
if (a.pos != b.pos)
50+
return (a.pos < b.pos);
51+
return (a.dir != b.dir) ? (a.dir < b.dir) : (a.next_turn < b.next_turn);
52+
}
53+
54+
55+
struct Map {
56+
std::vector<std::string> grid;
57+
std::vector<Cart> carts;
58+
59+
// Returns index of the first cart which crashed, or -1 if no crashes yet.
60+
int tick() {
61+
std::sort(carts.begin(), carts.end(), order_by_pos);
62+
for (int i = 0; i < carts.size(); i++) {
63+
carts[i].move();
64+
switch (grid[carts[i].pos.y][carts[i].pos.x]) {
65+
case '/': carts[i].dir ^= 1; break;
66+
case '\\': carts[i].dir ^= 3; break;
67+
case '+': carts[i].turn(); break;
68+
default: break;
69+
}
70+
for (int j = 0; j < carts.size(); j++) {
71+
if (j != i && carts[j].pos == carts[i].pos) {
72+
return i;
73+
}
74+
}
75+
}
76+
return -1;
77+
}
78+
};
79+
80+
81+
int main(int argc, char* argv[]) {
82+
std::string input = "../input/day_13_input";
83+
if (argc > 1) {
84+
input = argv[1];
85+
}
86+
std::ifstream file(input);
87+
std::string line;
88+
89+
Map map;
90+
while (std::getline(file, line)) {
91+
map.grid.push_back(line);
92+
}
93+
94+
Point pos;
95+
for (pos.y = 0; pos.y < map.grid.size(); pos.y++) {
96+
for (pos.x = 0; pos.x < map.grid[pos.y].size(); pos.x++) {
97+
switch (map.grid[pos.y][pos.x]) {
98+
case '^':
99+
map.carts.push_back(Cart{ pos, North, -1 });
100+
map.grid[pos.y][pos.x] = '|';
101+
break;
102+
case '>':
103+
map.carts.push_back(Cart{ pos, East, -1 }); break;
104+
map.grid[pos.y][pos.x] = '-';
105+
break;
106+
case 'v':
107+
map.carts.push_back(Cart{ pos, South, -1 }); break;
108+
map.grid[pos.y][pos.x] = '|';
109+
break;
110+
case '<':
111+
map.carts.push_back(Cart{ pos, West, -1 }); break;
112+
map.grid[pos.y][pos.x] = '-';
113+
break;
114+
default:
115+
break;
116+
}
117+
}
118+
}
119+
120+
int crashed = map.tick();
121+
while (crashed == -1) {
122+
crashed = map.tick();
123+
}
124+
std::cout << map.carts[crashed].pos.x << "," << map.carts[crashed].pos.y << '\n';
125+
return 0;
126+
}

2018/cpp/day_13b.cpp

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#include <algorithm>
2+
#include <array>
3+
#include <fstream>
4+
#include <iostream>
5+
#include <string>
6+
#include <vector>
7+
8+
struct Point {
9+
int x, y;
10+
11+
Point& operator += (const Point& v) { x += v.x; y += v.y; return *this; }
12+
Point& operator -= (const Point& v) { x -= v.x; y -= v.y; return *this; }
13+
};
14+
bool operator == (const Point& a, const Point& b) { return a.y == b.y && a.x == b.x; }
15+
bool operator != (const Point& a, const Point& b) { return a.y != b.y || a.x != b.x; }
16+
bool operator < (const Point& a, const Point& b) { return (a.y != b.y) ? (a.y < b.y) : (a.x < b.x); }
17+
18+
19+
static constexpr std::array<Point, 4> Dir = {
20+
Point{ 0, -1 }, // north
21+
Point{ 1, 0 }, // east
22+
Point{ 0, 1 }, // south
23+
Point{ -1, 0 }, // west
24+
};
25+
26+
static constexpr int North = 0;
27+
static constexpr int East = 1;
28+
static constexpr int South = 2;
29+
static constexpr int West = 3;
30+
31+
struct Cart {
32+
Point pos;
33+
int dir;
34+
int next_turn = -1;
35+
36+
void turn() {
37+
dir = (dir + 4 + next_turn) & 0x3;
38+
next_turn = ((next_turn + 2) % 3) - 1;
39+
}
40+
41+
void move() {
42+
pos += Dir[dir];
43+
}
44+
};
45+
46+
bool order_by_pos(const Cart& a, const Cart& b)
47+
{
48+
if (a.pos != b.pos)
49+
return (a.pos < b.pos);
50+
return (a.dir != b.dir) ? (a.dir < b.dir) : (a.next_turn < b.next_turn);
51+
}
52+
53+
struct Map {
54+
std::vector<std::string> grid;
55+
std::vector<Cart> carts;
56+
57+
// Returns index of the first cart which crashed, or -1 if no crashes yet.
58+
std::vector<int> tick() {
59+
std::vector<int> crashed;
60+
std::sort(carts.begin(), carts.end(), order_by_pos);
61+
for (int i = 0; i < carts.size(); i++) {
62+
carts[i].move();
63+
switch (grid[carts[i].pos.y][carts[i].pos.x]) {
64+
case '/': carts[i].dir ^= 1; break;
65+
case '\\': carts[i].dir ^= 3; break;
66+
case '+': carts[i].turn(); break;
67+
default: break;
68+
}
69+
for (int j = 0; j < carts.size(); j++) {
70+
if (j != i && carts[j].pos == carts[i].pos) {
71+
crashed.push_back(i);
72+
crashed.push_back(j);
73+
}
74+
}
75+
}
76+
return crashed;
77+
}
78+
};
79+
80+
int main(int argc, char* argv[]) {
81+
std::string input = "../input/day_13_input";
82+
if (argc > 1) {
83+
input = argv[1];
84+
}
85+
std::ifstream file(input);
86+
std::string line;
87+
88+
Map map;
89+
while (std::getline(file, line)) {
90+
map.grid.push_back(line);
91+
}
92+
93+
Point pos;
94+
for (pos.y = 0; pos.y < map.grid.size(); pos.y++) {
95+
for (pos.x = 0; pos.x < map.grid[pos.y].size(); pos.x++) {
96+
switch (map.grid[pos.y][pos.x]) {
97+
case '^':
98+
map.carts.push_back(Cart{ pos, North, -1 });
99+
map.grid[pos.y][pos.x] = '|';
100+
break;
101+
case '>':
102+
map.carts.push_back(Cart{ pos, East, -1 }); break;
103+
map.grid[pos.y][pos.x] = '-';
104+
break;
105+
case 'v':
106+
map.carts.push_back(Cart{ pos, South, -1 }); break;
107+
map.grid[pos.y][pos.x] = '|';
108+
break;
109+
case '<':
110+
map.carts.push_back(Cart{ pos, West, -1 }); break;
111+
map.grid[pos.y][pos.x] = '-';
112+
break;
113+
default:
114+
break;
115+
}
116+
}
117+
}
118+
119+
while (map.carts.size() > 1) {
120+
auto crashed = map.tick();
121+
std::sort(std::begin(crashed), std::end(crashed), std::greater<>());
122+
for (const auto index : crashed) {
123+
map.carts.erase(std::begin(map.carts) + index);
124+
}
125+
}
126+
printf("%d,%d\n", map.carts[0].pos.x, map.carts[0].pos.y);
127+
return 0;
128+
}

2018/sample_input/day_13_sample_input

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/->-\
2+
| | /----\
3+
| /-+--+-\ |
4+
| | | | v |
5+
\-+-/ \-+--/
6+
\------/
7+
8+
/>-<\
9+
| |
10+
| /<+-\
11+
| | | v
12+
\>+</ |
13+
| ^
14+
\<->/

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ This repository contains solutions to the Advent of Code puzzles.
99
|2021 |1-25 | - | [Link](/2021/) |[Link](/2021/README.md) |
1010
|2020 |1-25 |1-25 | [Link](/2020/) |[Link](/2020/README.md) |
1111
|2019 |1-25 |1-7 | [Link](/2019/) |[Link](/2019/README.md) |
12-
|2018 |1-12 | | [Link](/2018/) |[Link](/2018/README.md) |
12+
|2018 |1-13 | | [Link](/2018/) |[Link](/2018/README.md) |

0 commit comments

Comments
 (0)