-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_17.cpp
86 lines (64 loc) · 1.38 KB
/
problem_17.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
#include "AoC/2017/problem_17.h"
#include "AoC/problems_map.h"
#include "AoC/utils/algo.h"
#include "AoC/utils/zipper.h"
#include <istream>
#include <vector>
namespace
{
size_t solve_1_impl( const size_t steps_forward )
{
std::vector<size_t> values{ 0 };
AoC::Zipper zipper( values );
size_t current_elem = 1;
while ( current_elem <= 2017 )
{
zipper.next( steps_forward );
zipper.append( current_elem );
zipper.next();
++current_elem;
}
zipper.next();
return zipper.current();
}
size_t get_steps_num( std::istream& input )
{
size_t steps_forward = 0;
input >> steps_forward;
return steps_forward;
}
} // namespace
namespace AoC_2017
{
namespace problem_17
{
size_t solve_1( std::istream& input )
{
return solve_1_impl( get_steps_num( input ) );
}
size_t solve_2( std::istream& input )
{
const auto steps = get_steps_num( input );
size_t next_value_after_zero = 0;
for ( size_t i = 1, index = 0; i <= 50'000'000; ++i )
{
index = ( index + steps + 1 ) % i;
if ( index == 0 )
{
next_value_after_zero = i;
}
}
return next_value_after_zero;
}
AOC_REGISTER_PROBLEM( 2017_17, solve_1, solve_2 );
} // namespace problem_17
} // namespace AoC_2017
#ifndef NDEBUG
# include "impl_tests.h"
# include <cassert>
static void impl_tests()
{
assert( 638 == solve_1_impl( 3 ) );
}
REGISTER_IMPL_TEST( impl_tests );
#endif