aboutsummaryrefslogtreecommitdiff
path: root/2016/src/day18.cpp
blob: 66d1bf0bbfc14fae96dfe7f53e82189ecb9e0549 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <algorithm>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <string>

using namespace std;

namespace {

string
readFile(const filesystem::path& filename)
{
	ifstream file{ filename };
	string   line;
	getline(file, line);
	return line;
}

void
solve(string line, size_t lines)
{
	long count = 0;
	while ( lines-- > 0 ) {
		string current_line = "." + line + ".";

		string new_line;

		for ( size_t i = 1; i < current_line.size() - 1; ++i ) {
			auto next_chr = '.';

			const auto left   = current_line[i - 1];
			const auto middle = current_line[i];
			const auto right  = current_line[i + 1];

			if ( left == '^' && middle == '^' && right == '.' ) {
				next_chr = '^';
			}
			else if ( left == '.' && middle == '^' && right == '^' ) {
				next_chr = '^';
			}
			else if ( left == '^' && middle == '.' && right == '.' ) {
				next_chr = '^';
			}
			else if ( left == '.' && middle == '.' && right == '^' ) {
				next_chr = '^';
			}

			new_line += next_chr;
		}

		count += ranges::count(line, '.');

		line = std::move(new_line);
	}

	cout << count << '\n';
}

void
part1(const string& line)
{
	solve(line, 40);
}

void
part2(const string& line)
{
	solve(line, 400000);
}

} // namespace

int
main()
{
	auto line = readFile("data/day18.txt");
	part1(line);
	part2(line);
}