-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDec10.cpp
More file actions
80 lines (63 loc) · 1.52 KB
/
Dec10.cpp
File metadata and controls
80 lines (63 loc) · 1.52 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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <sstream>
int checkCycle(int cycle,int regX){
if (
cycle==20 ||
cycle==60 ||
cycle==100||
cycle==140||
cycle==180||
cycle==220){
return cycle*regX;
}
return 0;
}
void draw(const std::vector<int>& pixels){
for (int i=0;i<240;++i){
if (pixels[i]==0) std::cout<<'.';
else std::cout<<'#';
if (i%40==39) std::cout<<'\n';
}
}
void checkPixel(int& cur_pixel,int regX,std::vector<int>& pixels){
if (std::abs((cur_pixel%40)-regX)<=1){
pixels[cur_pixel] = 1;
}
++cur_pixel;
}
int main(){
//READ DATA
std::ifstream file("data10.txt");
std::string str{};
int regX{1};
int cycle{};
std::string instruction{};
int amount{};
int total{};
std::vector<int> pixels(240,0);
int cur_pixel{};
while (getline(file,str)){
std::stringstream ss{str};
ss>>instruction;
if (instruction=="noop") {
++cycle;
total += checkCycle(cycle,regX);
checkPixel(cur_pixel,regX,pixels);
}else{
ss>>amount;
++cycle;
total += checkCycle(cycle,regX);
checkPixel(cur_pixel,regX,pixels);
++cycle;
total += checkCycle(cycle,regX);
checkPixel(cur_pixel,regX,pixels);
regX += amount;
}
}
std::cout<<total<<'\n';
draw(pixels);
return 0;
}