diff options
author | Federico Igne <undyamon@disroot.org> | 2023-12-13 16:23:51 +0100 |
---|---|---|
committer | Federico Igne <undyamon@disroot.org> | 2023-12-13 16:23:51 +0100 |
commit | a388690e255427aa28a2fd4364e094a10bde3382 (patch) | |
tree | 1d84a0262f1480cba28b504ff7cd7d2224eb7b38 | |
parent | ba6089f4eefcbd6c482d2f774b8ce016bd0a4441 (diff) | |
download | aoc-a388690e255427aa28a2fd4364e094a10bde3382.tar.gz aoc-a388690e255427aa28a2fd4364e094a10bde3382.zip |
aoc(2313): Point of Incidence
-rw-r--r-- | 2023/13/Makefile | 19 | ||||
-rw-r--r-- | 2023/13/resources/input_small.txt | 15 | ||||
-rw-r--r-- | 2023/13/src/part1.cpp | 76 | ||||
-rw-r--r-- | 2023/13/src/part2.cpp | 95 |
4 files changed, 205 insertions, 0 deletions
diff --git a/2023/13/Makefile b/2023/13/Makefile new file mode 100644 index 0000000..af6a294 --- /dev/null +++ b/2023/13/Makefile | |||
@@ -0,0 +1,19 @@ | |||
1 | CXXFLAGS := -std=c++17 | ||
2 | CPPFLAGS := -I../include | ||
3 | EXE := part1 part2 | ||
4 | |||
5 | .PHONY: all clean configure | ||
6 | |||
7 | all: $(EXE) | ||
8 | |||
9 | configure: | ||
10 | bear -- $(MAKE) all | ||
11 | |||
12 | %.o: %.cpp | ||
13 | $(CXX) -c $(CPPFLAGS) $(CXXFLAGS) $< -o $@ | ||
14 | |||
15 | clean: | ||
16 | rm -rf $(EXE) src/*.o compile_commands.json | ||
17 | |||
18 | %: src/%.o | ||
19 | $(CXX) $^ -o $@ | ||
diff --git a/2023/13/resources/input_small.txt b/2023/13/resources/input_small.txt new file mode 100644 index 0000000..3b6b5cc --- /dev/null +++ b/2023/13/resources/input_small.txt | |||
@@ -0,0 +1,15 @@ | |||
1 | #.##..##. | ||
2 | ..#.##.#. | ||
3 | ##......# | ||
4 | ##......# | ||
5 | ..#.##.#. | ||
6 | ..##..##. | ||
7 | #.#.##.#. | ||
8 | |||
9 | #...##..# | ||
10 | #....#..# | ||
11 | ..##..### | ||
12 | #####.##. | ||
13 | #####.##. | ||
14 | ..##..### | ||
15 | #....#..# | ||
diff --git a/2023/13/src/part1.cpp b/2023/13/src/part1.cpp new file mode 100644 index 0000000..2a043c2 --- /dev/null +++ b/2023/13/src/part1.cpp | |||
@@ -0,0 +1,76 @@ | |||
1 | #include <iostream> | ||
2 | #include <fstream> | ||
3 | #include <vector> | ||
4 | #include <tuple> | ||
5 | |||
6 | using Grid = std::pair<std::vector<int>, std::vector<int>>; | ||
7 | |||
8 | std::vector<Grid> parse(const char* file) | ||
9 | { | ||
10 | if (std::ifstream input{ file }; input.is_open()) | ||
11 | { | ||
12 | std::vector<int> rows, cols; | ||
13 | std::vector<Grid> grids; | ||
14 | |||
15 | std::string line; | ||
16 | while (not std::getline(input,line).eof()) | ||
17 | { | ||
18 | if (cols.size() < line.size()) | ||
19 | { | ||
20 | cols.resize(line.size(), 0); | ||
21 | } | ||
22 | |||
23 | if (line.empty()) | ||
24 | { | ||
25 | grids.push_back(std::make_pair(std::move(cols), std::move(rows))); | ||
26 | rows = cols = std::vector<int>{}; | ||
27 | continue; | ||
28 | } | ||
29 | |||
30 | int num{}; | ||
31 | for (int i = 0; i < line.size(); ++i) | ||
32 | { | ||
33 | num *= 2; | ||
34 | num += line[i] == '#'; | ||
35 | cols[i] *= 2; | ||
36 | cols[i] += line[i] == '#'; | ||
37 | } | ||
38 | rows.push_back(num); | ||
39 | } | ||
40 | grids.push_back(std::make_pair(std::move(cols), std::move(rows))); | ||
41 | |||
42 | return grids; | ||
43 | } | ||
44 | return {}; | ||
45 | } | ||
46 | |||
47 | int find_symmetry(const std::vector<int>& v) | ||
48 | { | ||
49 | for (int i = 1; i < v.size(); ++i) | ||
50 | { | ||
51 | if (v[i-1] == v[i]) | ||
52 | { | ||
53 | bool sim{true}; | ||
54 | for (int j = 1; j < std::min(i, (int)v.size() - i); ++j) | ||
55 | { | ||
56 | sim = sim && v[i-1-j] == v[i+j]; | ||
57 | } | ||
58 | if (sim) return i; | ||
59 | } | ||
60 | } | ||
61 | return 0; | ||
62 | } | ||
63 | |||
64 | int main(int argc, char* argv[]) | ||
65 | { | ||
66 | int answer{}; | ||
67 | |||
68 | for (const auto& grid : parse(argv[1])) | ||
69 | { | ||
70 | answer += find_symmetry(grid.first); | ||
71 | answer += 100 * find_symmetry(grid.second); | ||
72 | } | ||
73 | |||
74 | std::cout << answer << std::endl; | ||
75 | return 0; | ||
76 | } | ||
diff --git a/2023/13/src/part2.cpp b/2023/13/src/part2.cpp new file mode 100644 index 0000000..db14f0a --- /dev/null +++ b/2023/13/src/part2.cpp | |||
@@ -0,0 +1,95 @@ | |||
1 | #include <iostream> | ||
2 | #include <fstream> | ||
3 | #include <vector> | ||
4 | #include <tuple> | ||
5 | |||
6 | using Grid = std::pair<std::vector<uint32_t>, std::vector<uint32_t>>; | ||
7 | |||
8 | std::vector<Grid> parse(const char* file) | ||
9 | { | ||
10 | if (std::ifstream input{ file }; input.is_open()) | ||
11 | { | ||
12 | std::vector<uint32_t> rows, cols; | ||
13 | std::vector<Grid> grids; | ||
14 | |||
15 | std::string line; | ||
16 | while (not std::getline(input,line).eof()) | ||
17 | { | ||
18 | if (cols.size() < line.size()) | ||
19 | { | ||
20 | cols.resize(line.size(), 0); | ||
21 | } | ||
22 | |||
23 | if (line.empty()) | ||
24 | { | ||
25 | grids.push_back(std::make_pair(std::move(cols), std::move(rows))); | ||
26 | rows = cols = std::vector<uint32_t>{}; | ||
27 | continue; | ||
28 | } | ||
29 | |||
30 | int num{}; | ||
31 | for (int i = 0; i < line.size(); ++i) | ||
32 | { | ||
33 | num *= 2; | ||
34 | num += line[i] == '#'; | ||
35 | cols[i] *= 2; | ||
36 | cols[i] += line[i] == '#'; | ||
37 | } | ||
38 | rows.push_back(num); | ||
39 | } | ||
40 | grids.push_back(std::make_pair(std::move(cols), std::move(rows))); | ||
41 | |||
42 | return grids; | ||
43 | } | ||
44 | return {}; | ||
45 | } | ||
46 | |||
47 | bool equal(uint32_t a, uint32_t b, bool& smudge) | ||
48 | { | ||
49 | if (a == b) return true; | ||
50 | |||
51 | if (not smudge) | ||
52 | { | ||
53 | int smudges{}; | ||
54 | uint32_t diff = a ^ b; | ||
55 | while (diff > 0) | ||
56 | { | ||
57 | smudges += diff % 2; | ||
58 | diff /= 2; | ||
59 | } | ||
60 | return smudge = smudges == 1; | ||
61 | } | ||
62 | return false; | ||
63 | } | ||
64 | |||
65 | int find_symmetry(const std::vector<uint32_t>& v) | ||
66 | { | ||
67 | for (int i = 1; i < v.size(); ++i) | ||
68 | { | ||
69 | bool smudge{ false }; | ||
70 | if (equal(v[i-1], v[i], smudge)) | ||
71 | { | ||
72 | bool sim{true}; | ||
73 | for (int j = 1; j < std::min(i, (int)v.size() - i); ++j) | ||
74 | { | ||
75 | sim = sim && equal(v[i-1-j], v[i+j], smudge); | ||
76 | } | ||
77 | if (sim && smudge) return i; | ||
78 | } | ||
79 | } | ||
80 | return 0; | ||
81 | } | ||
82 | |||
83 | int main(int argc, char* argv[]) | ||
84 | { | ||
85 | int answer{}; | ||
86 | |||
87 | for (const auto& grid : parse(argv[1])) | ||
88 | { | ||
89 | answer += find_symmetry(grid.first); | ||
90 | answer += 100 * find_symmetry(grid.second); | ||
91 | } | ||
92 | |||
93 | std::cout << answer << std::endl; | ||
94 | return 0; | ||
95 | } | ||