-
Notifications
You must be signed in to change notification settings - Fork 9
/
day_18a.cpp
136 lines (121 loc) · 3.4 KB
/
day_18a.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_set>
#include <vector>
#include <cassert>
struct DigPlanStep {
int direction;
int length;
std::string colour;
};
struct Point {
int row;
int col;
Point(const int row, const int col) : row(row), col(col) {}
Point operator + (const Point& p) const {
return Point (p.row + row, p.col + col);
}
bool operator == (const Point& p) const {
return p.row == row && p.col == col;
}
};
struct hasher {
std::size_t operator() (const Point& position) const {
return position.row + position.col;
}
};
const std::vector<Point> motions ={
Point(-1,0),
Point(0,1),
Point(1,0),
Point(0, -1),
};
bool in_limits(const std::vector<std::vector<int>>& map, const int row, const int col) {
return row >= 0 && row < map.size() && col >= 0 && col < map[0].size();
}
DigPlanStep parse (const std::string& line) {
DigPlanStep dps;
if (line[0] == 'U') {
dps.direction = 0;
} else if (line[0] == 'R') {
dps.direction = 1;
} else if (line[0] == 'D') {
dps.direction = 2;
} else if (line[0] == 'L') {
dps.direction = 3;
} else {
std::cout << "This should not happen" << '\n';
}
auto space_idx = line.find(' ', 2);
dps.length = std::stoi(line.substr(2, space_idx - 2));
dps.colour = line.substr(space_idx + 2, line.size() - 1 - space_idx - 2);
return dps;
}
int main(int argc, char * argv[]) {
std::string input = "../input/day_18_input";
if (argc > 1) {
input = argv[1];
}
std::string line;
std::fstream file(input);
std::vector<DigPlanStep> plan;
std::array<int, 4> borders = {{0,0,0,0}}; // ymin, xmax, ymax, xmin == N,E,S,W
auto current = Point(0,0);
while(std::getline(file, line)) {
plan.emplace_back(parse(line));
current.row = current.row + motions[plan.back().direction].row * plan.back().length;
current.col = current.col + motions[plan.back().direction].col * plan.back().length;
borders[0] = std::min(borders[0], current.row);
borders[1] = std::max(borders[1], current.col);
borders[2] = std::max(borders[2], current.row);
borders[3] = std::min(borders[3], current.col);
}
std::vector<std::vector<char>> map (borders[2] - borders[0] + 1, std::vector<char>(borders[1] - borders[3] + 1, '.'));
current = Point(-borders[0], -borders[3]);
for (const auto& step : plan) {
for (int i = 0; i < step.length; i++) {
current = current + motions[step.direction];
map[current.row][current.col] = '#';
}
}
// find point touching the LHS border
for (int i = 0; i < map.size(); i++) {
if (map[i][0] == '#') {
current.row = i+1;
current.col = 1;
// assert (map[current.row][current.col] == '.');
break;
}
}
std::queue<Point> q;
std::unordered_set<Point, hasher> seen;
q.push(current);
while(!q.empty()) {
current = q.front();
q.pop();
if (seen.find(current) != seen.end()) {
continue;
}
seen.insert(current);
map[current.row][current.col] = '#';
for (const auto& motion : motions) {
const auto new_point = current + motion;
if (map[new_point.row][new_point.col] == '.') {
q.push(new_point);
}
}
}
int count = 0;
for (const auto& row : map) {
for (const auto ele : row) {
if (ele == '#') count++;
}
}
std::cout << count << '\n';
return 0;
}