+142
data/07/input.txt
+142
data/07/input.txt
···
1
+
......................................................................S......................................................................
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
+
.............................................................................................................................................
103
+
....................^.^.^.^.^...^...........^...^...^...^...^.^...^...^.....^.^.....^.^.....^.^.^.^.^.^.^...^.^.^.....^.^....................
104
+
.............................................................................................................................................
105
+
...................^.^...^.^...^...........^.^.^.^.^.^...^.^.....^.^.....^.....^.^.^...^.^.^.^.^...^.^.....^...^.^...^.^.^...................
106
+
.............................................................................................................................................
107
+
..................^.^.....^.............^.^.^.^.^.^.^.^.^.^...^...^.^...^.^.^.^.^.^.^.^.^.^.^.^.^.^...^.....^.......^...^.^..................
108
+
.............................................................................................................................................
109
+
.................^.....^...^.^...^...^.^...^.^.^...^...^.^.^.^.....^.^.^...^.^.^.^.^...^.^.^.^.....^.....^...^...^.^...^.^.^.................
110
+
.............................................................................................................................................
111
+
................^.^.^.^.....^...^...^.^...^.^...^.^...^.^.....^.^.^.^...^.^.^.............^.^...^.^.........^.^.^...^.^.^.^.^................
112
+
.............................................................................................................................................
113
+
...............^.^.^.^.^.^.^.^.^.^.^.^.^.^...^.^.^.^.^.^.........^.^.^.^...^.^.^.^.^...^...........^...^...^.^.^.^.....^...^.^...............
114
+
.............................................................................................................................................
115
+
..............^.....^.^...^.^.....^.^...^.^.^.^.^...^.^...^...^.^...^.......^.^...^.......^.^.....^.^...^.....^.......^.^.^.^.^..............
116
+
.............................................................................................................................................
117
+
.............^.^.^.^.^.^.^.^.^.^.^.^.^.^.^...^.^.^.^...^.....^.^...^.^.^.....^.^.^.^.^...^.^.^.^.^.^.^...^.^.^...^.^.^.^.^.^.^.^.............
118
+
.............................................................................................................................................
119
+
............^.^.^.^.^.^...^...^...........^.^.^.^.^.^.^...^.^...^...^...^.^.^.^.........^.^...^.^.^.^.^.^.^...^.....^.......^.^.^............
120
+
.............................................................................................................................................
121
+
...........^.^.....^.^.^.^.^.^.^.^...^.^.^.^.^...^.^.^.....^.^.^.^.^...^.......^...^.^.^.^.....^...^.^.^.^.^.....^.^.^.^.^...^.^.^...........
122
+
.............................................................................................................................................
123
+
..........^.^.^.^.^.^.^...^.....^.^.^...^.^.^...^...^.^.^.^.^.^.^.......^.^.......^.^.^.^.^.....^.^.^.^.^.^.....^.^...^.^...^.^.^.^..........
124
+
.............................................................................................................................................
125
+
.........^.....^.^.^.^...^...^...^...^.^...^.^.......^.^.^.^.^...^.^.^.....^.^.^.^.......^.^.^...^.^.^.^.^.......^...^.....^.^...^.^.........
126
+
.............................................................................................................................................
127
+
........^.^...^.^.^.^.^.^.....^...^.....^.^.^...^...^...^.^.^...^.^.^...^.^.....^...^.....^.^.^.^...^...^.^.^.^.....^.^.^...^.....^.^........
128
+
.............................................................................................................................................
129
+
.......^...^...^.^.^.....^.^...^...^.^.....^...^...^.^.^.^.^...^.^...^.^.^.^.....^.^.^...^.^.^...^.^...^.^.^.....^.^.^.^...^.^.^.^.^.^.......
130
+
.............................................................................................................................................
131
+
......^...........^.^.^.^.....^.^.^.^...^...............^.^.......^.^...^.^.^.^.....^...^...^.^.^.^.^...^.^...^.^.^.........^...^.^.^.^......
132
+
.............................................................................................................................................
133
+
.....^.^.^.^...^.^.^.^.^.^...^.......^.^...^.^.^.^.....^.^.^.^.^.^.^.^.^.^.^...^.^.....^.^.^...^.^...^.......^.^...^.^...^.^.^...^.^.^.^.....
134
+
.............................................................................................................................................
135
+
....^.^.^...^...^...^.^.^.^...^.^.......^.....^.....^...^.^...^...^.^.^.^.^.......^.^.^...^.^.^.....^.^.^...^...^.^.^.^.^...^.......^...^....
136
+
.............................................................................................................................................
137
+
...^.^.....^.^.^...^...^.^.^...^.^.^.^...^...^...^.^.^.^...^.^.^.^.^.^...^.^...^.^.^...^.....^.^.^...^.^.^...^.^.^.^.^.......^.^...^.^.^.^...
138
+
.............................................................................................................................................
139
+
..^.^.^.^...^.^.^.^...^.^.^.^.^.^.^...^...^.^.^.^...^.....^...^.^.^.^.^.^...^...^.^.^.^.....^.......^.^.^.^.^.....^.^.^.^.^.........^.^...^..
140
+
.............................................................................................................................................
141
+
.^.^.^.^.^.^.^.^.^...^.^.^...^...^.....^.^.....^.^.^.^.^.^...^...^.^...^.^.^.^.^...^.^.^.^...^.^.^.^...^...^...^...^.^.^.^.....^.^.^.....^.^.
142
+
.............................................................................................................................................
+16
data/07/test.txt
+16
data/07/test.txt
···
1
+
.......S.......
2
+
...............
3
+
.......^.......
4
+
...............
5
+
......^.^......
6
+
...............
7
+
.....^.^.^.....
8
+
...............
9
+
....^.^...^....
10
+
...............
11
+
...^.^...^.^...
12
+
...............
13
+
..^...^.....^..
14
+
...............
15
+
.^.^.^.^.^...^.
16
+
...............
+155
src/07/solution.cxx
+155
src/07/solution.cxx
···
1
+
#include "common/getinputpath.h"
2
+
#include <cstddef>
3
+
#include <format>
4
+
#include <fstream>
5
+
#include <map>
6
+
#include <optional>
7
+
#include <print>
8
+
#include <string>
9
+
#include <vector>
10
+
11
+
void render(std::vector<std::string> &data) {
12
+
if (data.size() < 2) {
13
+
return;
14
+
}
15
+
16
+
for (size_t i = 1; i < data.size(); ++i) {
17
+
auto &previous = data[i - 1];
18
+
auto ¤t = data[i];
19
+
20
+
for (size_t ch = 0; ch < current.size(); ++ch) {
21
+
char previous_ch = previous[ch];
22
+
char current_ch = current[ch];
23
+
if (previous_ch != 'S' && previous_ch != '|') {
24
+
continue;
25
+
}
26
+
27
+
// render
28
+
if (current_ch == '^') {
29
+
// split
30
+
if (ch > 0) {
31
+
current[ch - 1] = '|';
32
+
}
33
+
if (ch < current.size()) {
34
+
current[ch + 1] = '|';
35
+
}
36
+
} else {
37
+
current[ch] = '|';
38
+
}
39
+
}
40
+
}
41
+
}
42
+
43
+
long long count_beams_split(std::vector<std::string> &data) {
44
+
if (data.size() < 2) {
45
+
return 0;
46
+
}
47
+
auto out = 0;
48
+
49
+
for (size_t i = 1; i < data.size(); ++i) {
50
+
auto &previous = data[i - 1];
51
+
auto ¤t = data[i];
52
+
53
+
for (size_t ch = 0; ch < current.size(); ++ch) {
54
+
if (previous[ch] == '|' && current[ch] == '^') {
55
+
out += 1;
56
+
}
57
+
}
58
+
}
59
+
60
+
return out;
61
+
}
62
+
63
+
struct Position {
64
+
size_t x = 0;
65
+
size_t y = 0;
66
+
67
+
bool operator==(Position other) {
68
+
return this->x == other.x && this->y == other.y;
69
+
}
70
+
71
+
bool operator<(Position other) {
72
+
return this->x < other.x && this->y < other.y;
73
+
}
74
+
75
+
// std::map is a *sorted* structure, so it requires its types to implement
76
+
// comparators or not use std::map with unsortable values at all. I did not
77
+
// want to implement a comparator at the moment. this is part of a workaround.
78
+
std::string encode() { return std::format("{}/{}", this->x, this->y); }
79
+
80
+
static Position decode(std::string position) {
81
+
auto slash_idx = position.find_first_of('/');
82
+
auto x = std::stoll(position.substr(0, slash_idx));
83
+
auto y = std::stoll(position.substr(slash_idx + 1));
84
+
return Position{.x = static_cast<size_t>(x), .y = static_cast<size_t>(y)};
85
+
}
86
+
};
87
+
88
+
long long count_possible_paths(std::vector<std::string> &data,
89
+
Position position, long long acc,
90
+
std::map<std::string, long long> &cache) {
91
+
// TODO
92
+
std::optional<Position> new_pos{};
93
+
long long new_acc = acc;
94
+
if (position.y == data.size()) { // if we reached bottom
95
+
return acc + 1;
96
+
}
97
+
if (data[position.y][position.x] == '|' ||
98
+
data[position.y][position.x] == 'S') {
99
+
new_pos = Position{.x = position.x, .y = position.y + 1};
100
+
} else if (data[position.y][position.x] == '^') {
101
+
Position left = Position{.x = position.x - 1, .y = position.y};
102
+
Position right = Position{.x = position.x + 1, .y = position.y};
103
+
104
+
// a weird workaround to satisfy some static checks. maps must be sortable.
105
+
if (cache.contains(position.encode())) {
106
+
new_acc = cache[position.encode()];
107
+
} else {
108
+
new_acc = count_possible_paths(data, left, 0, cache) +
109
+
count_possible_paths(data, right, 0, cache);
110
+
111
+
cache[position.encode()] = new_acc;
112
+
}
113
+
}
114
+
115
+
if (!new_pos.has_value()) {
116
+
return new_acc;
117
+
}
118
+
__attribute__((musttail)) return count_possible_paths(data, new_pos.value(),
119
+
new_acc, cache);
120
+
}
121
+
122
+
long long count_possible_paths(std::vector<std::string> &data,
123
+
std::map<std::string, long long> &cache) {
124
+
Position start{
125
+
.x = data[0].find_first_of('S'),
126
+
.y = 0,
127
+
};
128
+
return count_possible_paths(data, start, 1, cache);
129
+
}
130
+
131
+
// runtime on Ryzen 5 5600G: 0.018s
132
+
int main() {
133
+
auto data_path = get_input_path(DATA_FOLDER);
134
+
std::ifstream data_ifstream(data_path);
135
+
if (!data_ifstream.is_open()) {
136
+
std::println("Cannot open the file at {}", data_path.string());
137
+
return 1;
138
+
}
139
+
140
+
std::vector<std::string> data{};
141
+
for (std::string t; std::getline(data_ifstream, t);) {
142
+
data.push_back(t);
143
+
}
144
+
145
+
render(data);
146
+
147
+
long long password_part_1 = count_beams_split(data);
148
+
149
+
std::map<std::string, long long> cache{};
150
+
long long password_part_2 = count_possible_paths(data, cache);
151
+
152
+
std::println("Eureka! {} / {}", password_part_1, password_part_2);
153
+
154
+
return 0;
155
+
}