Skip to content

Commit 58d253a

Browse files
committed
AoC 2022 Day 10 - cpp
1 parent 1c4f010 commit 58d253a

File tree

4 files changed

+298
-1
lines changed

4 files changed

+298
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
| python3 | [](src/main/python/AoC2022_01.py) | [](src/main/python/AoC2022_02.py) | [](src/main/python/AoC2022_03.py) | [](src/main/python/AoC2022_04.py) | | [](src/main/python/AoC2022_06.py) | | | [](src/main/python/AoC2022_09.py) | [](src/main/python/AoC2022_10.py) | | | | | | | | | | | | | | | |
1212
| java | [](src/main/java/AoC2022_01.java) | [](src/main/java/AoC2022_02.java) | [](src/main/java/AoC2022_03.java) | [](src/main/java/AoC2022_04.java) | [](src/main/java/AoC2022_05.java) | [](src/main/java/AoC2022_06.java) | [](src/main/java/AoC2022_07.java) | [](src/main/java/AoC2022_08.java) | [](src/main/java/AoC2022_09.java) | [](src/main/java/AoC2022_10.java) | | | | | | | | | | | | | | | |
1313
| bash | [](src/main/bash/AoC2022_01.sh) | [](src/main/bash/AoC2022_02.sh) | [](src/main/bash/AoC2022_03.sh) | [](src/main/bash/AoC2022_04.sh) | | [](src/main/bash/AoC2022_06.sh) | | | | | | | | | | | | | | | | | | | |
14-
| c++ | [](src/main/cpp/2022/01/AoC2022_01.cpp) | [](src/main/cpp/2022/02/AoC2022_02.cpp) | [](src/main/cpp/2022/03/AoC2022_03.cpp) | [](src/main/cpp/2022/04/AoC2022_04.cpp) | | [](src/main/cpp/2022/06/AoC2022_06.cpp) | | | [](src/main/cpp/2022/09/AoC2022_09.cpp) | | | | | | | | | | | | | | | | |
14+
| c++ | [](src/main/cpp/2022/01/AoC2022_01.cpp) | [](src/main/cpp/2022/02/AoC2022_02.cpp) | [](src/main/cpp/2022/03/AoC2022_03.cpp) | [](src/main/cpp/2022/04/AoC2022_04.cpp) | | [](src/main/cpp/2022/06/AoC2022_06.cpp) | | | [](src/main/cpp/2022/09/AoC2022_09.cpp) | [](src/main/cpp/2022/10/AoC2022_10.cpp) | | | | | | | | | | | | | | | |
1515
| julia | [](src/main/julia/AoC2022_01.jl) | [](src/main/julia/AoC2022_02.jl) | [](src/main/julia/AoC2022_03.jl) | [](src/main/julia/AoC2022_04.jl) | | [](src/main/julia/AoC2022_06.jl) | | | | | | | | | | | | | | | | | | | |
1616
<!-- @END:ImplementationsTable:2022@ -->
1717

src/main/cpp/2022/10/AoC2022_10.cpp

+236
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
#include <assert.h>
2+
3+
#include <numeric>
4+
#include <string>
5+
#include <vector>
6+
7+
#include "../../aoc/aoc.hpp"
8+
#include "../../aoc/grid/grid.hpp"
9+
#include "../../aoc/ocr/ocr.hpp"
10+
11+
using namespace std;
12+
13+
const char FILL = '#';
14+
const char EMPTY = ' ';
15+
16+
vector<pair<int, int>> program(const vector<string>& input) {
17+
int x = 1;
18+
int cycles = 0;
19+
vector<pair<int, int>> v;
20+
for (const string& line : input) {
21+
const auto splits = aoc::split(line);
22+
if (splits[0] == "noop") {
23+
v.push_back(make_pair(cycles, x));
24+
cycles++;
25+
} else if (splits[0] == "addx") {
26+
v.push_back(make_pair(cycles, x));
27+
cycles++;
28+
v.push_back(make_pair(cycles, x));
29+
cycles++;
30+
x += stoi(splits[1]);
31+
}
32+
}
33+
return v;
34+
}
35+
36+
inline int check(const int cycles, const int x) {
37+
return cycles % 40 == 20 ? x * cycles : 0;
38+
}
39+
40+
inline char draw(const int cycles, const int x) {
41+
return abs(x - cycles % 40) <= 1 ? FILL : EMPTY;
42+
}
43+
44+
vector<string> getPixels(const vector<string>& input) {
45+
const auto& p = program(input);
46+
vector<char> pixels;
47+
for (const pair<int, int>& state : p) {
48+
pixels.push_back(draw(state.first, state.second));
49+
}
50+
vector<string> grid;
51+
for (int i : aoc::Range::range(0, 240, 40)) {
52+
grid.push_back(string(pixels.begin() + i, pixels.begin() + i + 40));
53+
}
54+
return grid;
55+
}
56+
57+
string part1(const vector<string>& input) {
58+
const auto& p = program(input);
59+
int ans = accumulate(p.begin(), p.end(), 0,
60+
[](const int a, const pair<int, int>& p) {
61+
return a + check(p.first + 1, p.second);
62+
});
63+
return to_string(ans);
64+
}
65+
66+
string part2(const vector<string>& input) {
67+
const Grid<char> drawing = Grid<char>::from(getPixels(input));
68+
DEBUG(drawing.toString());
69+
return ocr::convert6(drawing, FILL, EMPTY);
70+
}
71+
72+
// clang-format off
73+
const vector<string> TEST = {
74+
"addx 15",
75+
"addx -11",
76+
"addx 6",
77+
"addx -3",
78+
"addx 5",
79+
"addx -1",
80+
"addx -8",
81+
"addx 13",
82+
"addx 4",
83+
"noop",
84+
"addx -1",
85+
"addx 5",
86+
"addx -1",
87+
"addx 5",
88+
"addx -1",
89+
"addx 5",
90+
"addx -1",
91+
"addx 5",
92+
"addx -1",
93+
"addx -35",
94+
"addx 1",
95+
"addx 24",
96+
"addx -19",
97+
"addx 1",
98+
"addx 16",
99+
"addx -11",
100+
"noop",
101+
"noop",
102+
"addx 21",
103+
"addx -15",
104+
"noop",
105+
"noop",
106+
"addx -3",
107+
"addx 9",
108+
"addx 1",
109+
"addx -3",
110+
"addx 8",
111+
"addx 1",
112+
"addx 5",
113+
"noop",
114+
"noop",
115+
"noop",
116+
"noop",
117+
"noop",
118+
"addx -36",
119+
"noop",
120+
"addx 1",
121+
"addx 7",
122+
"noop",
123+
"noop",
124+
"noop",
125+
"addx 2",
126+
"addx 6",
127+
"noop",
128+
"noop",
129+
"noop",
130+
"noop",
131+
"noop",
132+
"addx 1",
133+
"noop",
134+
"noop",
135+
"addx 7",
136+
"addx 1",
137+
"noop",
138+
"addx -13",
139+
"addx 13",
140+
"addx 7",
141+
"noop",
142+
"addx 1",
143+
"addx -33",
144+
"noop",
145+
"noop",
146+
"noop",
147+
"addx 2",
148+
"noop",
149+
"noop",
150+
"noop",
151+
"addx 8",
152+
"noop",
153+
"addx -1",
154+
"addx 2",
155+
"addx 1",
156+
"noop",
157+
"addx 17",
158+
"addx -9",
159+
"addx 1",
160+
"addx 1",
161+
"addx -3",
162+
"addx 11",
163+
"noop",
164+
"noop",
165+
"addx 1",
166+
"noop",
167+
"addx 1",
168+
"noop",
169+
"noop",
170+
"addx -13",
171+
"addx -19",
172+
"addx 1",
173+
"addx 3",
174+
"addx 26",
175+
"addx -30",
176+
"addx 12",
177+
"addx -1",
178+
"addx 3",
179+
"addx 1",
180+
"noop",
181+
"noop",
182+
"noop",
183+
"addx -9",
184+
"addx 18",
185+
"addx 1",
186+
"addx 2",
187+
"noop",
188+
"noop",
189+
"addx 9",
190+
"noop",
191+
"noop",
192+
"noop",
193+
"addx -1",
194+
"addx 2",
195+
"addx -37",
196+
"addx 1",
197+
"addx 3",
198+
"noop",
199+
"addx 15",
200+
"addx -21",
201+
"addx 22",
202+
"addx -6",
203+
"addx 1",
204+
"noop",
205+
"addx 2",
206+
"addx 1",
207+
"noop",
208+
"addx -10",
209+
"noop",
210+
"noop",
211+
"addx 20",
212+
"addx 1",
213+
"addx 2",
214+
"addx 2",
215+
"addx -6",
216+
"addx -11",
217+
"noop",
218+
"noop",
219+
"noop",
220+
};
221+
const vector<string> RESULT = {
222+
"## ## ## ## ## ## ## ## ## ## ",
223+
"### ### ### ### ### ### ### ",
224+
"#### #### #### #### #### ",
225+
"##### ##### ##### ##### ",
226+
"###### ###### ###### ####",
227+
"####### ####### ####### ",
228+
};
229+
// clang-format on
230+
231+
void samples() {
232+
assert(part1(TEST) == "13140");
233+
assert(getPixels(TEST) == RESULT);
234+
}
235+
236+
SMAIN(2022, 10)

src/main/cpp/2022/10/BUILD

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
cc_binary(
2+
name = "AoC2022_10",
3+
srcs = ["AoC2022_10.cpp"],
4+
deps = [
5+
"//src/main/cpp/aoc:aoc",
6+
"//src/main/cpp/aoc/grid:aoc_grid",
7+
"//src/main/cpp/aoc/ocr:aoc_ocr",
8+
"//src/main/cpp/aocd:aocd",
9+
],
10+
)
11+

src/main/cpp/2022/10/Makefile

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
CC := clang++
2+
CFLAGS := -Wall -std=c++17 -Ofast
3+
OBJ_DIR := ../../../../../build/cpp/obj
4+
AOC_OBJ_DIR := $(OBJ_DIR)/aoc
5+
GRID_OBJ_DIR := $(OBJ_DIR)/aoc
6+
OCR_OBJ_DIR := $(OBJ_DIR)/aoc
7+
AOCD_OBJ_DIR := $(OBJ_DIR)/aocd
8+
TARGET_DIR := ../../../../../build/cpp
9+
10+
AOC_SRCS = ../../aoc/aoc.cpp
11+
GRID_SRCS = ../../aoc/grid/grid.cpp
12+
OCR_SRCS = ../../aoc/ocr/ocr.cpp
13+
AOCD_SRCS = ../../aocd/aocd.cpp
14+
SRCS = $(MAIN).cpp
15+
AOC_OBJS = $(addprefix $(AOC_OBJ_DIR)/,$(notdir $(patsubst %.cpp,%.o,$(AOC_SRCS))))
16+
GRID_OBJS = $(addprefix $(GRID_OBJ_DIR)/,$(notdir $(patsubst %.cpp,%.o,$(GRID_SRCS))))
17+
OCR_OBJS = $(addprefix $(OCR_OBJ_DIR)/,$(notdir $(patsubst %.cpp,%.o,$(OCR_SRCS))))
18+
AOCD_OBJS = $(addprefix $(AOCD_OBJ_DIR)/,$(notdir $(patsubst %.cpp,%.o,$(AOCD_SRCS))))
19+
OBJS = $(addprefix $(OBJ_DIR)/,$(notdir $(patsubst %.cpp,%.o,$(SRCS))))
20+
TARGET = $(TARGET_DIR)/$(MAIN)
21+
22+
all: $(TARGET)
23+
24+
$(TARGET): $(AOC_OBJS) $(GRID_OBJS) $(OCR_OBJS) $(AOCD_OBJS) $(OBJS)
25+
$(CC) $(CFLAGS) -o $(TARGET) $(AOC_OBJS) $(GRID_OBJS) $(OCR_OBJS) $(AOCD_OBJS) $(OBJS)
26+
27+
$(AOC_OBJS): $(AOC_SRCS)
28+
@mkdir -p $(AOC_OBJ_DIR)
29+
$(CC) $(CFLAGS) -c $< -o $@
30+
31+
$(GRID_OBJS): $(GRID_SRCS)
32+
@mkdir -p $(GRID_OBJ_DIR)
33+
$(CC) $(CFLAGS) -c $< -o $@
34+
35+
$(OCR_OBJS): $(OCR_SRCS)
36+
@mkdir -p $(OCR_OBJ_DIR)
37+
$(CC) $(CFLAGS) -c $< -o $@
38+
39+
$(AOCD_OBJS): $(AOCD_SRCS)
40+
@mkdir -p $(AOCD_OBJ_DIR)
41+
$(CC) $(CFLAGS) -c $< -o $@
42+
43+
$(OBJS): $(SRCS)
44+
@mkdir -p $(OBJ_DIR)
45+
$(CC) $(CFLAGS) -c $< -o $@
46+
47+
clean:
48+
$(RM) $(AOC_OBJS) $(GRID_OBJS) $(OCR_OBJS) $(AOCD_OBJS) $(OBJS) $(TARGET)
49+
50+
.PHONY: all clean

0 commit comments

Comments
 (0)