-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay17.cpp
More file actions
42 lines (33 loc) · 929 Bytes
/
Day17.cpp
File metadata and controls
42 lines (33 loc) · 929 Bytes
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
#include "Day17.h"
void Day17::Part1()
{
const int input = 366;
vector<int> buffer = vector<int>{ 0 };
int currentPosition = 0;
// Reserve space in the buffer
buffer.reserve(2018);
for (int i = 1; i <= 2017; ++i)
{
// Calculate the new index
currentPosition = (currentPosition + input + 1) % buffer.size();
// Insert the new number
buffer.insert(buffer.begin() + currentPosition, i);
}
// For part 1, the answer is the next number after the last inserted value
cout << "Day 17 Part 1 answer: " << buffer[currentPosition + 1] << endl;
}
void Day17::Part2()
{
const int input = 366;
int answer = 0;
int currentPosition = 0;
for (int i = 1; i <= 50000000; ++i)
{
// Calculate the new index
currentPosition = (currentPosition + input + 1) % i;
// If the current position is 0, update the answer
if (currentPosition == 0)
answer = i;
}
cout << "Day 17 Part 2 answer: " << answer << endl;
}