-
Notifications
You must be signed in to change notification settings - Fork 0
/
part2.cpp
77 lines (63 loc) · 1.72 KB
/
part2.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
#include "Day15.hpp"
#include <vector>
#include <iostream>
#include <sstream>
#include <limits>
using namespace std;
struct Disc {
int positions;
int pos;
// rotates the disc excatly one slot forward
int rotate(){
pos = (pos + 1) % positions;
return pos;
}
// returns the position the disc would have in n seconds
int pseudo_rotate(unsigned n){
return ( (pos + n) % positions );
}
};
Result Day15::solve_p2(){
vector<Disc> discs;
// I SHOULD LEARN HOW TO USE REGEX!!
for(string line: this->data){
// skip beginning of line
stringstream stream(line.substr(12));
// read disc positions
int positions = 0;
stream >> positions;
// skip "positions"
string tmp;
stream >> tmp;
// read until 'n' (character before starting position)
char ch = ' ';
do {
stream >> ch;
} while(ch != 'n');
// read start positions
int startpos = 0;
stream >> startpos;
discs.push_back({positions, startpos});
}
// additional disc
discs.push_back({11, 0});
for(unsigned int ii = 1; ii < numeric_limits<unsigned int>::max(); ++ii){
// rotate each disc one slot
for(Disc &disc: discs){
disc.rotate();
}
int check = 0;
for(unsigned kk = 0; kk < discs.size(); ++kk){
if( discs[kk].pseudo_rotate(kk + 1) == 0 ){
++check;
} else {
// break for efficiency
break;
}
}
if( check == discs.size() ){
return {true, to_string(ii)};
}
}
return {false, "No suitable time slot found!"};
}