-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathday_03a.cpp
58 lines (56 loc) · 1.88 KB
/
day_03a.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
#include <fstream>
#include <iostream>
#include <string>
#include <charconv>
#include <limits>
#include <ranges>
int main(int argc, char* argv[]) {
const std::string input = (argc > 1) ? argv[1] : "../input/day_03_input" ;
std::ifstream file(input);
std::string line;
std::getline(file, line);
int number = std::stoi(line);
int box_side = 1;
if (number == 1) {
std::cout << 0 << '\n';
return 0;
}
while (number > (box_side * box_side)) {
box_side += 2;
}
box_side -= 2;
std::cout << "number: " << number << '\n';
std::cout << "box_side: " << box_side << '\n';
int distance = 0;
const auto box_side_2 = box_side * box_side;
if (number < box_side_2 + box_side + 1) {
std::cout << __LINE__ << '\n';
const auto x = box_side/2 + 1;
const auto y = -box_side/2 + number - box_side_2 - 1;
std::cout << "(" << x << ", " << y << ")" << '\n';
distance = std::abs(x) + std::abs(y);
}
else if (number < box_side_2 + 2 * (box_side + 1)) {
std::cout << __LINE__ << '\n';
const auto x = (box_side/2 + 1) - (number - box_side_2 - box_side - 1);
const auto y = box_side/2 + 1;
std::cout << "(" << x << ", " << y << ")" << '\n';
distance = std::abs(x) + std::abs(y);
}
else if (number < box_side_2 + 3 * (box_side + 1)) {
std::cout << __LINE__ << '\n';
const auto x = -(box_side/2 + 1);
const auto y = (box_side/2 + 1) - (number - box_side_2 - 2 * (box_side + 1));
std::cout << "(" << x << ", " << y << ")" << '\n';
distance = std::abs(x) + std::abs(y);
}
else {
std::cout << __LINE__ << '\n';
const auto x = -(box_side/2 + 1) + (number - box_side_2 - 3 * (box_side + 1));
const auto y = -(box_side/2 + 1);
std::cout << "(" << x << ", " << y << ")" << '\n';
distance = std::abs(x) + std::abs(y);
}
std::cout << distance << '\n';
return 0;
}