-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path03.cpp
112 lines (91 loc) · 2.32 KB
/
03.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
#include <chrono>
#include <functional>
#include <iostream>
#include <unordered_map>
using std::unordered_map;
struct Point {
int x;
int y;
void operator+=(const Point &other) noexcept {
x += other.x;
y += other.y;
}
bool operator==(const Point &other) const noexcept {
return x == other.x && y == other.y;
}
};
int solve_pt1(int n) {
Point pos = {0, 0};
Point directions[] = {{1, 0}, {0, -1}, {-1, 0}, {0, 1}};
int steps = 1;
unsigned int dir = 0;
for (int i = 1; i < n;) {
for (int j = 0; j < 2 && i < n; j++) {
for (int k = 0; k < steps && i < n; k++, i++) {
pos += directions[dir];
}
dir = (dir + 1) % 4;
}
steps += 1;
}
return abs(pos.x - 0) + abs(pos.y - 0);
}
int solve_pt2(int n) {
Point pos = {0, 0};
Point directions[] = {{1, 0}, {0, -1}, {-1, 0}, {0, 1}};
int steps = 1;
unsigned int dir = 0;
auto hash = [](const Point &p) {
return std::hash<int>()(p.x) ^ (std::hash<int>()(p.y) << 16);
};
unordered_map<Point, int, decltype(hash)> grid(0, hash);
grid[pos] = 1;
auto sum_neighbors = [&grid](const Point &p) {
int sum = 0;
for (int y = -1; y <= 1; y++) {
for (int x = -1; x <= 1; x++) {
// skip self
if ((x | y) == 0) {
continue;
}
sum += grid[{p.x + x, p.y + y}];
}
}
return sum;
};
for (int i = 1;;) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < steps; k++, i++) {
pos += directions[dir];
int value = sum_neighbors(pos);
if (value > n) {
return value;
}
grid[pos] = value;
}
dir = (dir + 1) % 4;
}
steps += 1;
}
return -1;
}
int main() {
auto tstart = std::chrono::high_resolution_clock::now();
int pt1 = 0;
int pt2 = 0;
std::string input;
std::getline(std::cin, input);
int n = std::stoi(input);
pt1 = solve_pt1(n);
pt2 = solve_pt2(n);
std::cout << "--- Day 3: Spiral Memory ---\n";
std::cout << "Part 1: " << pt1 << "\n";
std::cout << "Part 2: " << pt2 << "\n";
auto tstop = std::chrono::high_resolution_clock::now();
auto duration =
std::chrono::duration_cast<std::chrono::microseconds>(tstop - tstart);
std::cout << "Time: " << (static_cast<double>(duration.count()) / 1000.0)
<< " ms"
<< "\n";
return EXIT_SUCCESS;
}