aboutsummaryrefslogtreecommitdiff
path: root/2015/src/day11.cpp
blob: 06fd05b0446ba54712191437a184521c023f8b86 (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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <regex>
#include <string>

using namespace std;

void
increment(string& str)
{
	for ( auto it = rbegin(str); it != rend(str); ++it ) {
		*it += 1;
		if ( *it > 'z' ) {
			*it = 'a';
			continue;
		}
		return;
	}
}

bool
has_increasing_chars(string_view str)
{
	for ( size_t idx = 2; idx != str.length(); ++idx ) {
		if ( str[idx - 2] + 1 == str[idx - 1] && str[idx - 1] + 1 == str[idx] ) {
			return true;
		}
	}
	return false;
}

bool
contains_no_invalid_char(const string& str)
{
	return str.find_first_of("iol") == string::npos;
}

bool
contains_two_pairs(const string& str)
{
	static const regex regex("(.)\\1.*(.)\\2");

	return regex_search(str, regex);
}

void
part1(string& str)
{
	while ( true ) {
		if ( has_increasing_chars(str) && contains_no_invalid_char(str) && contains_two_pairs(str) ) {
			cout << str << endl;
			break;
		}
		increment(str);
	}
}

int
main()
{
	string str = "hepxcrrq";

	part1(str);

	increment(str);
	part1(str);
}