aboutsummaryrefslogtreecommitdiff
path: root/2016/src/day19.cpp
blob: 2d7282fa95599ec8733d622c1a0e0210887c78c2 (plain)
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
#include <cmath>
#include <iostream>

using namespace std;

namespace {

template<typename T>
T
fn(T N)
    requires std::is_integral_v<T>
{
	return T(1) << static_cast<T>(floor(log2(N)));
}

void
part1(unsigned N)
{
	cout << (2 * (N - fn(N))) + 1 << '\n';
}

void
part2(unsigned N)
{
	auto log_3 = [](double x) { return log(x) / log(3); };

	auto threes = pow(3, floor(log_3(N - 1)));
	if ( N <= 2 * threes ) {
		cout << static_cast<unsigned>(N - threes);
	}
	else {
		cout << static_cast<unsigned>(N - threes + N - (2 * threes));
	}
	cout << '\n';
}

} // namespace

int
main()
{
	const unsigned puzzle = 3017957;
	part1(puzzle);
	part2(puzzle);
}