-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
143 lines (130 loc) · 2.66 KB
/
main.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
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
/************************************************************************/
/* Advent of Code:
/* Day 18: Like a GIF For Your Yard
/************************************************************************/
#include <algorithm>
#include <iostream>
#include <fstream>
#include <chrono>
#include <string>
#include <array>
template<int S>
using Grid = std::array<std::array<char, S>, S>;
int isLightOn(char ch)
{
if (ch == '#')
return 1;
return 0;
}
template<int S>
int neighboursOn(Grid<S>& grid, int x, int y)
{
return isLightOn(grid[x - 1][y - 1]) + isLightOn(grid[x - 1][y]) + isLightOn(grid[x - 1][y + 1]) + \
isLightOn(grid[x][y - 1]) + isLightOn(grid[x][y + 1]) + \
isLightOn(grid[x + 1][y - 1]) + isLightOn(grid[x + 1][y]) + isLightOn(grid[x + 1][y + 1]);
}
template<int S>
Grid<S> animate(Grid<S>& grid)
{
Grid<S> newGrid = grid;
for (int i = 1; i < S - 1; i++)
{
for (int j = 1; j < S - 1; j++)
{
if (isLightOn(grid[i][j]))
{
if (neighboursOn(grid, i, j) != 2 && neighboursOn(grid, i, j) != 3)
newGrid[i][j] = '.';
}
else
{
if (neighboursOn(grid, i, j) == 3)
newGrid[i][j] = '#';
}
}
}
return newGrid;
}
template<int S>
void lightCorners(Grid<S>& grid)
{
grid[1][1] = '#';
grid[1][S - 2] = '#';
grid[S - 2][1] = '#';
grid[S - 2][S - 2] = '#';
}
template<int S>
int countLights(Grid<S>& grid)
{
int lights = 0;
for (auto row : grid)
{
for (auto element : row)
{
if (element == '#')
lights++;
}
}
return lights;
}
template<int S>
int partOne(Grid<S> grid)
{
for (int i = 0; i < 100; i++)
{
grid = animate(grid);
}
return countLights(grid);
}
template<int S>
int partTwo(Grid<S> grid)
{
for (int i = 0; i < 100; i++)
{
lightCorners(grid);
grid = animate(grid);
}
lightCorners(grid);
return countLights(grid);
}
template<int S>
void setGrid(Grid<S>& grid, char val)
{
for (auto& row : grid)
{
for (auto& element : row)
{
element = val;
}
}
}
template<int S>
Grid<S> readGrid()
{
std::ifstream infile("data_d18.txt");
std::string input;
Grid<S> grid;
int i = 1;
setGrid<S>(grid, '.');
while (std::getline(infile, input))
{
int j = 1;
for (const auto& ch : input)
{
if (i > S || j > S)
return grid;
grid[i][j++] = ch;
}
i++;
}
return grid;
}
int main()
{
std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now();
Grid<102> grid = readGrid<102>();
std::cout << partOne(grid) << std::endl;
std::cout << partTwo(grid) << std::endl;
std::chrono::duration<double> time_span = std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::high_resolution_clock::now() - t1);
std::cout << "Time: " << time_span.count() << "s.";
}