aboutsummaryrefslogtreecommitdiff
path: root/2017/src/day17.cpp
blob: 6cf498b69c55b5a33960d802df5d59c00df3e097 (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
46
47
#include <iostream>
#include <vector>

using namespace std;

namespace {

void
part1(size_t steps, size_t iterations)
{
	vector<size_t> buffer{ 0 };
	size_t         pos = 0;

	for ( size_t i = 1; i <= iterations; ++i ) {
		pos = (pos + steps) % i + 1;
		buffer.insert(buffer.begin() + static_cast<vector<size_t>::difference_type>(pos), i);
	}

	cout << "Part1: " << buffer[(pos + 1) % buffer.size()] << '\n';
}

void
part2(size_t steps, size_t iterations)
{
	size_t pos   = 0;
	size_t value = 0;

	for ( size_t i = 1; i <= iterations; ++i ) {
		pos = (pos + steps) % i;
		if ( pos == 0 ) {
			value = i;
		}
		pos = (pos + 1) % (i + 1);
	}

	cout << "Part2: " << value << '\n';
}

} // namespace

int
main()
{
	const auto input = 371;
	part1(input, 2017);
	part2(input, 50'000'000);
}