Skip to content

Commit c08d0bc

Browse files
authored
feat: day10 (#14)
1 parent ddc659b commit c08d0bc

File tree

16 files changed

+357
-14
lines changed

16 files changed

+357
-14
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
/build
55
/perf
66
Makefile
7-
.reponse
7+
.response

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ members = [
1010
"crates/day07",
1111
"crates/day08",
1212
"crates/day09",
13+
"crates/day10",
1314
"crates/helpers",
1415
]
1516

cpp/day06.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,10 @@ int main() {
211211
try {
212212
string message = readFile("crates/day06/input.txt");
213213
Lab grid(message);
214+
214215
// --- Part One ---
215216
cout << "Part One solution: " << grid.findPath() << endl;
217+
216218
// --- Part Two ---
217219
cout << "Part Two solution: " << grid.findIfStuck() << endl;
218220
} catch (const exception &e) {

cpp/day08.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ int main() {
154154
std::cout << "Part Two solution: " << checker.count_antennas(true)
155155
<< std::endl;
156156
} catch (const std::exception &e) {
157-
std::cerr << e.what() << std::endl;
157+
std::cerr << "Error: " << e.what() << "\n";
158158
}
159159

160160
return 0;

cpp/day09.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ int main() {
176176
std::cout << "Part Two solution: "
177177
<< FileSystem::calculateChecksum(checker.getFiles()) << "\n";
178178

179-
} catch (const std::exception &ex) {
180-
std::cerr << "Error: " << ex.what() << "\n";
179+
} catch (const std::exception &e) {
180+
std::cerr << "Error: " << e.what() << "\n";
181181
return 1;
182182
}
183183
return 0;

cpp/day10.cc

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
#include "helpers.h"
2+
#include <fstream>
3+
#include <iostream>
4+
#include <sstream>
5+
#include <string>
6+
#include <unordered_map>
7+
#include <unordered_set>
8+
#include <vector>
9+
10+
const std::vector<std::pair<int, int>> DIRECTIONS = {
11+
{0, 1}, // Right
12+
{1, 0}, // Down
13+
{0, -1}, // Left
14+
{-1, 0} // Up
15+
};
16+
17+
class TrailHeads {
18+
public:
19+
TrailHeads(const std::string &content) {
20+
std::istringstream stream(content);
21+
std::string line;
22+
int row = 0;
23+
24+
while (std::getline(stream, line)) {
25+
for (int col = 0; col < line.size(); ++col) {
26+
char ch = line[col];
27+
if (isdigit(ch)) {
28+
int height = ch - '0';
29+
auto coords = std::make_pair(row, col);
30+
if (height == 0) {
31+
trailheads.push_back(coords);
32+
}
33+
grid[coords] = height;
34+
}
35+
}
36+
row++;
37+
}
38+
}
39+
40+
size_t count_trailheads() const {
41+
size_t total = 0;
42+
for (const auto &head : trailheads) {
43+
total += count_trail_unique(head, 0).size();
44+
}
45+
return total;
46+
}
47+
48+
size_t count_distinct_trailheads() const {
49+
size_t total = 0;
50+
for (const auto &head : trailheads) {
51+
total += count_trail_all(head, 0);
52+
}
53+
return total;
54+
}
55+
56+
private:
57+
struct pair_hash {
58+
template <class T1, class T2>
59+
std::size_t operator()(const std::pair<T1, T2> &pair) const {
60+
auto hash1 = std::hash<T1>{}(pair.first);
61+
auto hash2 = std::hash<T2>{}(pair.second);
62+
return hash1 ^ hash2;
63+
}
64+
};
65+
std::vector<std::pair<int, int>> trailheads;
66+
std::unordered_map<std::pair<int, int>, int, pair_hash> grid;
67+
68+
std::unordered_set<std::pair<int, int>, pair_hash>
69+
count_trail_unique(const std::pair<int, int> &position, int height) const {
70+
std::unordered_set<std::pair<int, int>, pair_hash> res;
71+
72+
for (const auto &[row_dir, col_dir] : DIRECTIONS) {
73+
auto new_dir =
74+
std::make_pair(position.first + row_dir, position.second + col_dir);
75+
auto it = grid.find(new_dir);
76+
if (it != grid.end()) {
77+
int new_pos = it->second;
78+
if (height + 1 != new_pos) {
79+
continue;
80+
}
81+
82+
if (new_pos == 9) {
83+
res.insert(new_dir);
84+
} else {
85+
auto unique_trails = count_trail_unique(new_dir, new_pos);
86+
res.insert(unique_trails.begin(), unique_trails.end());
87+
}
88+
}
89+
}
90+
return res;
91+
}
92+
93+
size_t count_trail_all(const std::pair<int, int> &position,
94+
int height) const {
95+
size_t res = 0;
96+
for (const auto &[row_dir, col_dir] : DIRECTIONS) {
97+
auto new_dir =
98+
std::make_pair(position.first + row_dir, position.second + col_dir);
99+
auto it = grid.find(new_dir);
100+
if (it != grid.end()) {
101+
int new_pos = it->second;
102+
if (height + 1 != new_pos) {
103+
continue;
104+
}
105+
106+
if (new_pos == 9) {
107+
res++;
108+
} else {
109+
res += count_trail_all(new_dir, new_pos);
110+
}
111+
}
112+
}
113+
return res;
114+
}
115+
};
116+
117+
int main() {
118+
try {
119+
std::string message = readFile("crates/day10/input.txt");
120+
TrailHeads checker(message);
121+
122+
// --- Part One ---
123+
std::cout << "Part One solution: " << checker.count_trailheads()
124+
<< std::endl;
125+
126+
// --- Part Two ---
127+
std::cout << "Part Two solution: " << checker.count_distinct_trailheads()
128+
<< std::endl;
129+
130+
} catch (const std::exception &e) {
131+
std::cerr << "Error: " << e.what() << std::endl;
132+
}
133+
134+
return 0;
135+
}

crates/day01/.response

Lines changed: 0 additions & 2 deletions
This file was deleted.

crates/day02/.response

Lines changed: 0 additions & 2 deletions
This file was deleted.

crates/day03/.response

Lines changed: 0 additions & 2 deletions
This file was deleted.

crates/day08/.response

Lines changed: 0 additions & 2 deletions
This file was deleted.

crates/day09/.response

Lines changed: 0 additions & 2 deletions
This file was deleted.

crates/day10/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "day10"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[lints]
7+
workspace = true
8+
9+
[dependencies]
10+
helpers = { path = "../helpers"}

crates/day10/example_input.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
89010123
2+
78121874
3+
87430965
4+
96549874
5+
45678903
6+
32019012
7+
01329801
8+
10456732

crates/day10/input.txt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
18776569014346783430321009012345676543089876765012101843210
2+
09683458723454894321234218987432189452176769854323678956521
3+
14592987654098765436549367016543032360145678765014545697434
4+
23601678432132650145678456323456501278234549343478034788910
5+
10712569549241043278901285434787654329103234256589121078923
6+
89803434678950012789854398103490769210567145187678438967434
7+
67810326589867821016760187312521898123498076094501567456556
8+
50981810410778932345123272210634567087654189143215678304567
9+
41832900325696543451074561028701256198563778012324569213498
10+
32963871234587123898785654139987341017652890123473478012323
11+
87874562187671034567698763245676892456701287634982101701414
12+
96565653094544343452188898743786743326898096545679876894505
13+
05454512123349852143089889652898653217652125621043965787676
14+
12343103098256765001234767601701062108943034432152014690189
15+
01289544567187678114345678015612341007765403569061023543287
16+
01276632101096789823210599126543652210812312678278710871296
17+
10345789780125679814676587439874743321907654589189658960345
18+
01234695690434578705689436510165894430418523971012347454434
19+
12304504541128963210790325565256766569349010810143232323521
20+
23413213232037654309821019874305412678256186743256101210650
21+
34329832198747012313412301878912301569107898653087082198761
22+
65896701007658965412301498965887432354518947894198898017321
23+
96785432216234878905432567054896345823101456763234745654450
24+
87123870125165687876541230123445458910112398654125656723765
25+
21012965634018796982650345678032567421012567432098749810894
26+
76321034676569805401721214789121076578323456501589038902923
27+
85432123589458012392895603210434789489210167895610121451012
28+
96524323428301123483486578765965672374331321234785430543212
29+
87012410210216782174987439834874301065145490121096789678101
30+
73453891346785493065234328703893216521036787654567645679201
31+
02567765445698304160145012012743227434321098943658938983210
32+
11098894332103215659876430125652108965670101812340127654321
33+
25437743210114106765454309836543345696987872701235672105898
34+
36727654012325699877869218747434210780745963890104383236785
35+
49818123963445788566978745658923312321894854761201294345656
36+
56709037874236567345432036547012103456783203454398761787654
37+
43237654365187665432301127015432176501654112341432650198323
38+
54108910210094572301432098326721085432456043210541043278910
39+
45678921109001481676501287445856794101387456101650169867610
40+
38787632278112390789043016532943893232297887232789856701521
41+
29698543465243654632132105621032124540196996243456765432430
42+
10101012354305783541245094765021043256785432123455965432107
43+
67652765410216792150956783876107650122312301098767874345698
44+
58543894324345891067810782987238987201406784567854901236787
45+
49987685456543282123425691096543271230545493201993210340123
46+
32106452567650112054534567899454100045654320192387651259654
47+
40045321898342106567651056928767652198743410887434540568798
48+
51235430210289287438122345210178943329652378976543039875687
49+
67896787320176396129034434356598712410701237010132128064516
50+
10765698451965445001545423347895606549890796521243989123105
51+
23896787567843234652436710212874307432037887430145478798234
52+
34785896765432128763429876108963218901126986543234567017345
53+
25634789854578019854210785654851019985435100125676454156236
54+
18721034563689108943218798743344328776434234234981043205145
55+
09343123872672397654309654312235899643421965547872124314076
56+
43216012901521087898701211201156732554310876958943235023489
57+
56905438765432076985654300321076541467215467867654332133012
58+
47876329034310145676701098730987210398706326321038949874323
59+
56981010123421234565892345645678901237987015432127654365434

0 commit comments

Comments
 (0)