-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06-Probably-a-Fire-Hazard.cpp
More file actions
102 lines (87 loc) · 2.82 KB
/
06-Probably-a-Fire-Hazard.cpp
File metadata and controls
102 lines (87 loc) · 2.82 KB
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
// Copyright (C) 2023 Joe Baker (JoeBlakeB)
// Advent of Code 2015 - Day 06: Probably a Fire Hazard
// Usage:
// scripts/cppRun.sh 2015/06-Probably-a-Fire-Hazard.cpp < 2015/inputs/06.txt
#include <iostream>
#include <sstream>
const int GRID_WIDTH = 1000;
const int GRID_HEIGHT = 1000;
class Grid1 {
public:
void setLights(int xFrom, int yFrom, int xTo, int yTo, bool state) {
for (int y = yFrom; y <= yTo; y++) {
for (int x = xFrom; x <= xTo; x++) {
grid[(y * GRID_WIDTH) + x] = state;
}
}
}
void toggleLights(int xFrom, int yFrom, int xTo, int yTo) {
for (int y = yFrom; y <= yTo; y++) {
for (int x = xFrom; x <= xTo; x++) {
grid[(y * GRID_WIDTH) + x] ^= true;
}
}
}
int litCount() {
int count = 0;
for (int i = 0; i < GRID_WIDTH * GRID_HEIGHT; i++) {
if (grid[i]) { count++; }
}
return count;
}
private:
bool grid[GRID_WIDTH * GRID_HEIGHT] = {false};
};
class Grid2 {
public:
void changeBrightness(int xFrom, int yFrom, int xTo, int yTo, int increase) {
for (int y = yFrom; y <= yTo; y++) {
for (int x = xFrom; x <= xTo; x++) {
int gridLocation = (y * GRID_WIDTH) + x;
int brightness = grid[gridLocation] + increase;
if (brightness < 0) { brightness = 0; }
grid[gridLocation] = brightness;
}
}
}
int totalBrightness() {
int count = 0;
for (int i = 0; i < GRID_WIDTH * GRID_HEIGHT; i++) {
count += grid[i];
}
return count;
}
private:
int grid[GRID_WIDTH * GRID_HEIGHT] = {false};
};
int main() {
Grid1 grid1;
Grid2 grid2;
std::string instruction;
while (std::getline(std::cin, instruction)) {
std::stringstream ss(instruction);
std::string action;
bool turnOn = false;
ss >> action;
if (action == "turn") {
std::string onOrOff;
ss >> onOrOff;
turnOn = onOrOff == "on";
}
int xFrom, yFrom, xTo, yTo;
char commaIgnore;
std::string throughIgnore;
ss >> xFrom >> commaIgnore >> yFrom >>
throughIgnore >> xTo >> commaIgnore >> yTo;
if (action == "turn") {
grid1.setLights(xFrom, yFrom, xTo, yTo, turnOn);
grid2.changeBrightness(xFrom, yFrom, xTo, yTo, turnOn ? 1 : -1);
} else {
grid1.toggleLights(xFrom, yFrom, xTo, yTo);
grid2.changeBrightness(xFrom, yFrom, xTo, yTo, 2);
}
}
std::cout << "There are " << grid1.litCount() << " lights lit in part one" << std::endl;
std::cout << "The total brightness is " << grid2.totalBrightness() << " for part two" << std::endl;
return 0;
}