aboutsummaryrefslogtreecommitdiff
path: root/2015/src/day18.cpp
blob: fa465f49cafa587f0c0d8c52e3ed90ec17ba486f (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <algorithm>
#include <fstream>
#include <iostream>
#include <map>
#include <tuple>
using namespace std;

using puzzle_type = map<tuple<size_t, size_t>, bool>;

auto
read_file(string_view filename)
{
	fstream     input{ filename };
	puzzle_type puzzle;

	size_t line = 0;
	for ( string str; getline(input, str); ) {
		for ( size_t column = 0; column != str.size(); ++column ) {
			if ( str[column] == '#' ) {
				puzzle[{ column, line }] = true;
			}
		}

		++line;
	}

	return puzzle;
}

void
iterate(puzzle_type& puzzle, size_t width)
{
	puzzle_type next;

	for ( size_t line = 0; line != width; ++line ) {
		for ( size_t column = 0; column != width; ++column ) {
			static const vector<tuple<size_t, size_t>> offsets = {
				{ -1, -1 },
				{ 0, -1 },
				{ 1, -1 },
				{ -1, 0 },
				{ 1, 0 },
				{ -1, 1 },
				{ 0, 1 },
				{ 1, 1 }
			};

			size_t count = 0;
			for ( const auto& [delta_x, delta_y]: offsets ) {
				if ( puzzle[{ column + delta_x, line + delta_y }] ) {
					++count;
				}
			}

			if ( puzzle[{ column, line }] ) {
				if ( count == 2 || count == 3 ) {
					next[{ column, line }] = true;
				}
			}
			else {
				if ( count == 3 ) {
					next[{ column, line }] = true;
				}
			}
		}
	}

	puzzle.swap(next);
}

void
print(puzzle_type puzzle, size_t width)
{
	for ( size_t line = 0; line != width; ++line ) {
		for ( size_t column = 0; column != width; ++column ) {
			auto value = puzzle[{ column, line }];
			cout << (value ? '#' : '.');
		}
		cout << '\n';
	}
	cout << endl;
}

void
part1(puzzle_type puzzle)
{
	static const size_t width = 100;

	for ( int i = 0; i != 100; ++i ) {
		iterate(puzzle, width);
	}

	long sum = 0;
	for ( size_t line = 0; line != width; ++line ) {
		for ( size_t column = 0; column != width; ++column ) {
			auto value = puzzle[{ column, line }];
			if ( value ) {
				++sum;
			}
		}
	}
	cout << sum << endl;
}

void
enable_corners(puzzle_type& puzzle, size_t width)
{
	puzzle[{ 0, 0 }]                 = true;
	puzzle[{ width - 1, 0 }]         = true;
	puzzle[{ 0, width - 1 }]         = true;
	puzzle[{ width - 1, width - 1 }] = true;
}

void
part2(puzzle_type puzzle)
{
	static const size_t width = 100;

	for ( int i = 0; i != 100; ++i ) {
		enable_corners(puzzle, width);
		iterate(puzzle, width);
	}
	enable_corners(puzzle, width);

	long sum = 0;
	for ( size_t line = 0; line != width; ++line ) {
		for ( size_t column = 0; column != width; ++column ) {
			auto value = puzzle[{ column, line }];
			if ( value ) {
				++sum;
			}
		}
	}
	cout << sum << endl;
}

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