Skip to content

Commit 0b903c2

Browse files
committed
Add 2022 day 10 cpp
1 parent 485ec78 commit 0b903c2

5 files changed

Lines changed: 298 additions & 1 deletion

File tree

2022/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ This folder contains solutions to each of the problems in Advent of Code 2022 in
1616
| <nobr> [Day 7: No Space Left On Device](https://adventofcode.com/2022/day/7) </nobr> | <nobr> [Part 1](/2022/cpp/day_07a.cpp) [Part 2](/2022/cpp/day_07b.cpp) </nobr> | </nobr> [Link](/2022/input/day_07_input) </nobr> | </nobr> [Link](/2022/sample_input/day_07_sample_input) </nobr> | </nobr> [Link](/2022/puzzles/day_07_puzzle) </nobr> |
1717
| <nobr> [Day 8: Treetop Tree House](https://adventofcode.com/2022/day/8) </nobr> | <nobr> [Part 1](/2022/cpp/day_08a.cpp) [Part 2](/2022/cpp/day_08b.cpp) </nobr> | </nobr> [Link](/2022/input/day_08_input) </nobr> | </nobr> [Link](/2022/sample_input/day_08_sample_input) </nobr> | </nobr> [Link](/2022/puzzles/day_08_puzzle) </nobr> |
1818
| <nobr> [Day 9: Rope Bridge](https://adventofcode.com/2022/day/9) </nobr> | <nobr> [Part 1](/2022/cpp/day_09a.cpp) [Part 2](/2022/cpp/day_09b.cpp) </nobr> | </nobr> [Link](/2022/input/day_09_input) </nobr> | </nobr> [Link](/2022/sample_input/day_09_sample_input) </nobr> | </nobr> [Link](/2022/puzzles/day_09_puzzle) </nobr> |
19+
| <nobr> [Day 10: Cathode-Ray Tube](https://adventofcode.com/2022/day/10) </nobr> | <nobr> [Part 1](/2022/cpp/day_10a.cpp) [Part 2](/2022/cpp/day_10b.cpp) </nobr> | </nobr> [Link](/2022/input/day_10_input) </nobr> | </nobr> [Link](/2022/sample_input/day_10_sample_input) </nobr> | </nobr> [Link](/2022/puzzles/day_10_puzzle) </nobr> |

2022/cpp/day_10a.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <fstream>
2+
#include <iostream>
3+
#include <string>
4+
5+
class CommSystem {
6+
public:
7+
void noop() {
8+
tick();
9+
};
10+
11+
void addx(const int val) {
12+
tick();
13+
tick();
14+
X += val;
15+
}
16+
17+
long long get_signal_strength() {
18+
return signal_strength;
19+
}
20+
21+
private:
22+
23+
void tick() {
24+
cycle_count++;
25+
if ((cycle_count - 20) % 40 == 0) {
26+
signal_strength += cycle_count * X;
27+
}
28+
}
29+
30+
long long X = 1;
31+
long long cycle_count = 0;
32+
long long signal_strength = 0;
33+
};
34+
35+
36+
int main(int argc, char * argv[]) {
37+
std::string input = "../input/day_10_input";
38+
if (argc > 1) {
39+
input = argv[1];
40+
}
41+
42+
std::string line;
43+
std::fstream file(input);
44+
45+
CommSystem cs;
46+
47+
while(std::getline(file, line)) {
48+
// std::cout << line << '\n';
49+
const auto space = line.find(' ');
50+
const auto instr = line.substr(0, space);
51+
if (instr == "noop") {
52+
cs.noop();
53+
} else if (instr == "addx") {
54+
cs.addx(std::stoi(line.substr(space + 1, line.size() - space - 1)));
55+
}
56+
}
57+
58+
std::cout << cs.get_signal_strength() << '\n';
59+
60+
return 0;
61+
}

2022/cpp/day_10b.cpp

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
#include <array>
2+
#include <fstream>
3+
#include <iostream>
4+
#include <string>
5+
6+
class CommSystem {
7+
public:
8+
void noop() {
9+
tick();
10+
};
11+
12+
void addx(const int val) {
13+
tick();
14+
tick();
15+
X += val;
16+
}
17+
18+
long long get_signal_strength() {
19+
return signal_strength;
20+
}
21+
22+
void print_screen() {
23+
for(const auto& row : screen) {
24+
for (const auto ele : row) {
25+
std::cout << ele;
26+
}
27+
std::cout << '\n';
28+
}
29+
}
30+
31+
32+
private:
33+
34+
void update_crt() {
35+
const auto pixel_id = (cycle_count - 1) % 240;
36+
const auto pixel_row = pixel_id / 40;
37+
const auto pixel_col = pixel_id % 40;
38+
// std::cout << "CRT at position: " << pixel_id << " (" << pixel_row << ',' << pixel_col << ')' << '\n';
39+
if (pixel_col == X || pixel_col == (X - 1) || pixel_col == (X + 1)) {
40+
screen[pixel_row][pixel_col] = '#';
41+
} else {
42+
screen[pixel_row][pixel_col] = '.';
43+
}
44+
}
45+
46+
void tick() {
47+
// std::cout << "Begin cycle: " << cycle_count + 1 << '\n';
48+
cycle_count++;
49+
update_crt();
50+
// print_screen();
51+
if ((cycle_count - 20) % 40 == 0) {
52+
signal_strength += cycle_count * X;
53+
}
54+
// std::cout << "End cycle: " << cycle_count << '\n';
55+
}
56+
57+
long long X = 1;
58+
long long cycle_count = 0;
59+
long long signal_strength = 0;
60+
std::array<std::array<char, 40>, 6> screen = {};
61+
};
62+
63+
64+
int main(int argc, char * argv[]) {
65+
std::string input = "../input/day_10_input";
66+
if (argc > 1) {
67+
input = argv[1];
68+
}
69+
70+
std::string line;
71+
std::fstream file(input);
72+
73+
CommSystem cs;
74+
75+
while(std::getline(file, line)) {
76+
// std::cout << line << '\n';
77+
const auto space = line.find(' ');
78+
const auto instr = line.substr(0, space);
79+
if (instr == "noop") {
80+
cs.noop();
81+
} else if (instr == "addx") {
82+
cs.addx(std::stoi(line.substr(space + 1, line.size() - space - 1)));
83+
}
84+
}
85+
86+
cs.print_screen();
87+
88+
return 0;
89+
}
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
addx 15
2+
addx -11
3+
addx 6
4+
addx -3
5+
addx 5
6+
addx -1
7+
addx -8
8+
addx 13
9+
addx 4
10+
noop
11+
addx -1
12+
addx 5
13+
addx -1
14+
addx 5
15+
addx -1
16+
addx 5
17+
addx -1
18+
addx 5
19+
addx -1
20+
addx -35
21+
addx 1
22+
addx 24
23+
addx -19
24+
addx 1
25+
addx 16
26+
addx -11
27+
noop
28+
noop
29+
addx 21
30+
addx -15
31+
noop
32+
noop
33+
addx -3
34+
addx 9
35+
addx 1
36+
addx -3
37+
addx 8
38+
addx 1
39+
addx 5
40+
noop
41+
noop
42+
noop
43+
noop
44+
noop
45+
addx -36
46+
noop
47+
addx 1
48+
addx 7
49+
noop
50+
noop
51+
noop
52+
addx 2
53+
addx 6
54+
noop
55+
noop
56+
noop
57+
noop
58+
noop
59+
addx 1
60+
noop
61+
noop
62+
addx 7
63+
addx 1
64+
noop
65+
addx -13
66+
addx 13
67+
addx 7
68+
noop
69+
addx 1
70+
addx -33
71+
noop
72+
noop
73+
noop
74+
addx 2
75+
noop
76+
noop
77+
noop
78+
addx 8
79+
noop
80+
addx -1
81+
addx 2
82+
addx 1
83+
noop
84+
addx 17
85+
addx -9
86+
addx 1
87+
addx 1
88+
addx -3
89+
addx 11
90+
noop
91+
noop
92+
addx 1
93+
noop
94+
addx 1
95+
noop
96+
noop
97+
addx -13
98+
addx -19
99+
addx 1
100+
addx 3
101+
addx 26
102+
addx -30
103+
addx 12
104+
addx -1
105+
addx 3
106+
addx 1
107+
noop
108+
noop
109+
noop
110+
addx -9
111+
addx 18
112+
addx 1
113+
addx 2
114+
noop
115+
noop
116+
addx 9
117+
noop
118+
noop
119+
noop
120+
addx -1
121+
addx 2
122+
addx -37
123+
addx 1
124+
addx 3
125+
noop
126+
addx 15
127+
addx -21
128+
addx 22
129+
addx -6
130+
addx 1
131+
noop
132+
addx 2
133+
addx 1
134+
noop
135+
addx -10
136+
noop
137+
noop
138+
addx 20
139+
addx 1
140+
addx 2
141+
addx 2
142+
addx -6
143+
addx -11
144+
noop
145+
noop
146+
noop

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ This repository contains solutions to the Advent of Code puzzles.
66

77
| Year | C++ | Python | Link to folders | Link to README.md |
88
|:----:|:----:|:----:|:----:|:----:|
9-
|2022 |1-9 | - | [Link](/2022/) |[Link](/2022/README.md) |
9+
|2022 |1-10 | - | [Link](/2022/) |[Link](/2022/README.md) |
1010
|2021 |1-25 | - | [Link](/2021/) |[Link](/2021/README.md) |
1111
|2020 |1-25 |1-25 | [Link](/2020/) |[Link](/2020/README.md) |
1212
|2019 |1-25 |1-7 | [Link](/2019/) |[Link](/2019/README.md) |

0 commit comments

Comments
 (0)