Repository navigation
Expand file tree
/
Copy pathday06.cpp
More file actions
191 lines (172 loc) · 6.57 KB
/
Copy pathday06.cpp
File metadata and controls
191 lines (172 loc) · 6.57 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <algorithm>
#include <array>
#include <charconv>
#include <cstdlib>
#include <print>
#include <ranges>
#include <span>
#include <string_view>
#include <tuple>
#include <utility>
#include <vector>
struct Light {
std::array<std::size_t, 4> neighbors{};
std::size_t degree{};
constexpr void connect(std::size_t id) noexcept {
neighbors[degree++] = id;
}
constexpr auto connections() const noexcept {
return std::span{neighbors}.first(degree);
}
};
struct Point {
int x{};
int y{};
constexpr bool operator==(Point const&) const noexcept = default;
constexpr Point operator+(Point const& other) const noexcept {
return {x + other.x, y + other.y};
}
constexpr Point operator-(Point const& other) const noexcept {
return {x - other.x, y - other.y};
}
constexpr Point transpose() const noexcept {
return {y, x};
}
constexpr Point scale(Point const& other) const noexcept {
return {x * other.x, y * other.y};
}
};
constexpr auto make_lights(std::string_view input) {
constexpr auto num = [](std::string_view s) {
std::size_t n{0};
std::from_chars(s.data(), s.data() + s.size(), n);
return n;
};
constexpr auto take = [] (std::string_view& input, char delimiter) {
std::size_t const end{input.find(delimiter)};
std::string_view const token{input.substr(0, end)};
input.remove_prefix(end == std::string_view::npos ? input.size() : end + 1);
return token;
};
std::vector<Light> lights;
while (not input.empty()) {
std::string_view line{take(input, '\n')};
if (line.empty()) continue;
std::size_t const from{num(take(line, '-'))};
while (not line.empty()) {
std::size_t const to{num(take(line, ','))};
if (std::size_t const id{std::max(from, to)}; id >= lights.size()) {
lights.resize(id + 1);
}
lights[from].connect(to);
lights[to].connect(from);
}
}
return lights;
}
constexpr std::size_t corner(std::vector<Light> const& lights) {
for (std::size_t const id : std::views::iota(0zu, lights.size())) {
if (lights[id].degree == 2zu) return id;
}
throw "Missing corner";
}
constexpr std::size_t straight(std::vector<Light> const& lights, std::size_t prev, std::size_t curr) {
for (std::size_t const next : lights[curr].connections()) {
if (next == prev) continue;
auto const turns{std::ranges::any_of(lights[prev].connections(), [&](std::size_t neighbor) {
return neighbor != curr and std::ranges::contains(lights[next].connections(), neighbor);
})};
if (not turns) return next;
}
return 0zu;
}
constexpr std::size_t diagonal(std::vector<Light> const& lights, std::size_t curr, std::size_t a, std::size_t b) {
return *std::ranges::find_if(lights[a].connections(), [&](std::size_t id) {
return id != curr and std::ranges::contains(lights[b].connections(), id);
});
}
constexpr std::size_t side_length(std::vector<Light> const& lights, std::size_t start) {
std::size_t prev{start};
std::size_t curr{lights[start].connections()[0]};
std::size_t side{2};
while (lights[curr].degree != 2zu) {
auto const next{std::ranges::find_if(lights[curr].connections(), [&](std::size_t id) {
return id != prev and lights[id].degree < 4zu;
})};
std::tie(prev, curr) = std::tuple{curr, *next};
++side;
}
return side;
}
constexpr std::size_t solve_part1(std::vector<Light> const& lights) {
std::size_t curr{corner(lights)};
std::size_t const side{side_length(lights, curr)};
std::array<std::size_t, 2> prev{};
// step diagonally to the midpoint
for (std::size_t step{0}; step < side / 2; ++step) {
auto forward{lights[curr].connections() | std::views::filter([&](std::size_t id) {
return not std::ranges::contains(prev, id);
})};
std::array<std::size_t, 2> next{};
std::ranges::copy(forward, next.begin());
curr = diagonal(lights, curr, next[0], next[1]);
prev = next;
}
return curr;
}
constexpr Point solve_part2(std::vector<Light> const& lights) {
constexpr std::array ids{1zu, 2zu, 3zu, 2026zu};
constexpr std::array anchors{Point{47, 30}, Point{29, 59}, Point{56, 35}};
constexpr auto absval = [](std::signed_integral auto v) {
return (v < 0 ? -v : v);
};
constexpr auto visit_grid = [] (std::vector<Light> const& lights, auto&& visit) {
std::size_t row{corner(lights)};
std::size_t right{lights[row].connections()[0]};
std::size_t down{lights[row].connections()[1]};
for (int y{0}; ; ++y) {
std::size_t curr{row};
std::size_t next{right};
for (int x{0}; curr != 0zu; ++x) {
if (not visit(curr, Point{x, y})) return;
std::tie(curr, next) = std::tuple{next, next == 0zu ? 0zu : straight(lights, curr, next)};
}
if (down == 0zu) return;
std::tie(row, right, down) = std::tuple{down, diagonal(lights, row, right, down), straight(lights, row, down)};
}
};
std::array<Point, ids.size()> positions{};
std::size_t found{0};
// visit the grid, storing the relative positions of the ids of interest
visit_grid(lights, [&](std::size_t id, Point const& position) {
for (auto&& [key, value] : std::views::zip(ids, positions)) {
if (key != id) continue;
value = position;
++found;
break;
}
return found != ids.size();
});
auto const [p1, p2, p3, target] = positions;
// determine the orientation from the displacement between the first two anchors
Point const relative{p2 - p1};
Point const absolute{anchors[1] - anchors[0]};
bool const swap{absval(relative.x) != absval(absolute.x)};
Point const aligned{swap ? relative.transpose() : relative};
Point const signs{aligned.x == absolute.x ? 1 : -1, aligned.y == absolute.y ? 1 : -1};
// translate relative to the first anchor
Point const p{target - p1};
return anchors[0] + (swap ? p.transpose() : p).scale(signs);;
}
constexpr static auto solve = [] {
constexpr static char data_str[]{
#embed "inputs/day06.in"
};
std::vector const lights{make_lights({data_str, sizeof(data_str)})};
return std::pair{solve_part1(lights), solve_part2(lights)};
}();
const auto& [part1, part2] = solve;
int main() {
std::println("Part 1: {}", part1);
std::println("Part 2: {},{}", part2.x, part2.y);
}