aboutsummaryrefslogtreecommitdiff
path: root/2018/src/day11.cpp
diff options
context:
space:
mode:
Diffstat (limited to '2018/src/day11.cpp')
-rw-r--r--2018/src/day11.cpp143
1 files changed, 143 insertions, 0 deletions
diff --git a/2018/src/day11.cpp b/2018/src/day11.cpp
new file mode 100644
index 0000000..1f8f9e2
--- /dev/null
+++ b/2018/src/day11.cpp
@@ -0,0 +1,143 @@
1#include <iostream>
2#include <map>
3
4using namespace std;
5
6namespace {
7
8const int WIDTH = 300;
9const int HEIGHT = 300;
10
11using Grid = map<tuple<int, int>, int>;
12
13int
14power_level(int x, int y, int serial)
15{
16 auto rack_id = x + 10;
17 auto level = rack_id * y;
18 level += serial;
19 level *= rack_id;
20 level /= 100;
21 level %= 10;
22 level -= 5;
23 return level;
24}
25
26Grid
27build_grid(int serial)
28{
29 Grid grid;
30 for ( int x = 1; x <= WIDTH; ++x ) {
31 for ( int y = 1; y <= HEIGHT; ++y ) {
32 grid[{ x, y }] = power_level(x, y, serial);
33 }
34 }
35 return grid;
36}
37
38int
39sum3x3(const Grid& grid, int x, int y)
40{
41 auto sum = 0;
42 for ( int dx = 0; dx != 3; ++dx ) {
43 for ( int dy = 0; dy != 3; ++dy ) {
44 sum += grid.at({ x + dx, y + dy });
45 }
46 }
47 return sum;
48}
49
50void
51part1(const int serial)
52{
53 auto grid = build_grid(serial);
54
55 auto best_sum = numeric_limits<int>::min();
56 auto best_x = 0;
57 auto best_y = 0;
58
59 for ( int x = 1; x <= WIDTH - 2; ++x ) {
60 for ( int y = 1; y <= HEIGHT - 2; ++y ) {
61 if ( auto sum = sum3x3(grid, x, y); sum > best_sum ) {
62 best_sum = sum;
63 best_x = x;
64 best_y = y;
65 }
66 }
67 }
68
69 cout << "Part 1: " << best_x << ',' << best_y << '\n';
70}
71
72Grid
73pre_process(const Grid& grid)
74{
75 Grid sat;
76
77 for ( int y = 1; y <= HEIGHT; ++y ) {
78 for ( int x = 1; x <= WIDTH; ++x ) {
79 const auto a = grid.at({ x, y });
80 const auto b = (x > 1) ? sat.at({ x - 1, y }) : 0;
81 const auto c = (y > 1) ? sat.at({ x, y - 1 }) : 0;
82 const auto d = (x > 1 && y > 1) ? sat.at({ x - 1, y - 1 }) : 0;
83
84 sat[{ x, y }] = a + b + c - d;
85 }
86 }
87
88 return sat;
89}
90
91int
92get_safe(const Grid& grid, int x, int y)
93{
94 return (x < 1 || y < 1) ? 0 : grid.at({ x, y });
95}
96
97int
98square_sum(const Grid& grid, int x, int y, int s)
99{
100 int x2 = x + s - 1;
101 int y2 = y + s - 1;
102
103 return get_safe(grid, x2, y2) - get_safe(grid, x - 1, y2) - get_safe(grid, x2, y - 1) + get_safe(grid, x - 1, y - 1);
104}
105
106void
107part2(const int serial)
108{
109 static const int MAX_SIZE = 100;
110
111 auto grid = pre_process(build_grid(serial));
112
113 auto best_sum = numeric_limits<int>::min();
114 auto best_x = 0;
115 auto best_y = 0;
116 auto best_s = 0;
117
118 for ( int s = 1; s <= MAX_SIZE; ++s ) {
119 for ( int x = 1; x <= WIDTH - s + 1; ++x ) {
120 for ( int y = 1; y <= HEIGHT - s + 1; ++y ) {
121 if ( auto sum = square_sum(grid, x, y, s); sum > best_sum ) {
122 best_sum = sum;
123 best_x = x;
124 best_y = y;
125 best_s = s;
126 }
127 }
128 }
129 }
130
131 cout << "Part 2: " << best_x << ',' << best_y << "," << best_s << '\n';
132}
133
134} // namespace
135
136int
137main()
138{
139 static const int SERIAL = 7989;
140
141 part1(SERIAL);
142 part2(SERIAL);
143}