Skip to content

Commit c0b3b04

Browse files
committed
feat: day08
1 parent 97905ae commit c0b3b04

File tree

9 files changed

+422
-0
lines changed

9 files changed

+422
-0
lines changed

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
@@ -8,6 +8,7 @@ members = [
88
"crates/day05",
99
"crates/day06",
1010
"crates/day07",
11+
"crates/day08",
1112
"crates/helpers",
1213
]
1314

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@ Ideally, the solution does not use external crates/dependencies.
1818
| 5 |||||||
1919
| 6 |||||||
2020
| 7 |||||||
21+
| 8 |||||||

cpp/day08.cc

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
#include <algorithm>
2+
#include <cmath>
3+
#include <fstream>
4+
#include <iostream>
5+
#include <sstream>
6+
#include <string>
7+
#include <unordered_map>
8+
#include <vector>
9+
10+
struct GridSize {
11+
int rows;
12+
int cols;
13+
};
14+
15+
class Antenna {
16+
public:
17+
Antenna(const std::string &content) {
18+
int rows = std::count(content.begin(), content.end(), '\n');
19+
int cols = content.find('\n');
20+
if (cols != std::string::npos)
21+
cols = cols;
22+
this->grid = {rows - 1, cols - 1};
23+
24+
std::istringstream stream(content);
25+
std::string line;
26+
int row_idx = 0;
27+
28+
while (std::getline(stream, line)) {
29+
for (int col_idx = 0; col_idx < line.size(); ++col_idx) {
30+
char ch = line[col_idx];
31+
if (std::isalnum(ch)) {
32+
frequencies[ch].emplace_back(row_idx, col_idx);
33+
}
34+
}
35+
++row_idx;
36+
}
37+
}
38+
39+
static std::pair<std::pair<int, int>, std::pair<int, int>>
40+
new_antenna(const std::pair<int, int> &current_antenna,
41+
const std::pair<int, int> &other_antenna,
42+
const std::pair<int, int> &distance) {
43+
44+
int y = (current_antenna.first < other_antenna.first)
45+
? current_antenna.first - distance.first
46+
: current_antenna.first + distance.first;
47+
int x = (current_antenna.second < other_antenna.second)
48+
? current_antenna.second - distance.second
49+
: current_antenna.second + distance.second;
50+
51+
int dy = y - current_antenna.first;
52+
int dx = x - current_antenna.second;
53+
54+
return {{y, x}, {dy, dx}};
55+
}
56+
57+
std::pair<bool, bool> can_add_antenna(
58+
const std::pair<int, int> &antenna,
59+
const std::vector<std::pair<int, int>> &antenna_positions) const {
60+
61+
if (antenna.first >= 0 && antenna.second >= 0 &&
62+
antenna.second <= grid.cols && antenna.first <= grid.rows) {
63+
if (std::find(antenna_positions.begin(), antenna_positions.end(),
64+
antenna) != antenna_positions.end()) {
65+
return {false, false};
66+
}
67+
return {false, true};
68+
}
69+
return {true, false};
70+
}
71+
72+
size_t count_antennas(bool harmony_frequencies) {
73+
std::vector<std::pair<int, int>> antenna_positions;
74+
75+
for (const auto &[_, positions] : frequencies) {
76+
for (size_t i = 0; i < positions.size(); ++i) {
77+
const auto &pos_i = positions[i];
78+
for (size_t j = i + 1; j < positions.size(); ++j) {
79+
const auto &pos_j = positions[j];
80+
auto distance = std::make_pair(std::abs(pos_i.first - pos_j.first),
81+
std::abs(pos_i.second - pos_j.second));
82+
83+
if (harmony_frequencies) {
84+
for (const auto &pos : {pos_i, pos_j}) {
85+
auto [is_out, can_add] = can_add_antenna(pos, antenna_positions);
86+
if (is_out) {
87+
break;
88+
}
89+
if (can_add) {
90+
antenna_positions.push_back(pos);
91+
}
92+
}
93+
}
94+
95+
auto [first_antenna, distance_1] =
96+
new_antenna(pos_i, pos_j, distance);
97+
auto [second_antenna, distance_2] =
98+
new_antenna(pos_j, pos_i, distance);
99+
100+
while (true) {
101+
auto [is_out, can_add] =
102+
can_add_antenna(first_antenna, antenna_positions);
103+
if (is_out) {
104+
break;
105+
}
106+
if (can_add) {
107+
antenna_positions.push_back(first_antenna);
108+
}
109+
if (!harmony_frequencies) {
110+
break;
111+
}
112+
first_antenna.first += distance_1.first;
113+
first_antenna.second += distance_1.second;
114+
}
115+
116+
while (true) {
117+
auto [is_out, can_add] =
118+
can_add_antenna(second_antenna, antenna_positions);
119+
if (is_out) {
120+
break;
121+
}
122+
if (can_add) {
123+
antenna_positions.push_back(second_antenna);
124+
}
125+
if (!harmony_frequencies) {
126+
break;
127+
}
128+
second_antenna.first += distance_2.first;
129+
second_antenna.second += distance_2.second;
130+
}
131+
}
132+
}
133+
}
134+
135+
return antenna_positions.size();
136+
}
137+
138+
private:
139+
std::unordered_map<char, std::vector<std::pair<int, int>>> frequencies;
140+
GridSize grid;
141+
};
142+
143+
std::string read_file(const std::string &filename) {
144+
std::ifstream file(filename);
145+
if (!file) {
146+
throw std::runtime_error("Could not open the file");
147+
}
148+
std::stringstream buffer;
149+
buffer << file.rdbuf();
150+
return buffer.str();
151+
}
152+
153+
int main() {
154+
try {
155+
std::string message = read_file("crates/day08/input.txt");
156+
157+
// --- Part One ---
158+
Antenna checker(message);
159+
std::cout << "Part One solution: " << checker.count_antennas(false)
160+
<< std::endl;
161+
162+
// --- Part Two ---
163+
std::cout << "Part Two solution: " << checker.count_antennas(true)
164+
<< std::endl;
165+
} catch (const std::exception &e) {
166+
std::cerr << e.what() << std::endl;
167+
}
168+
169+
return 0;
170+
}

crates/day08/Cargo.toml

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

crates/day08/example_input.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
............
2+
........0...
3+
.....0......
4+
.......0....
5+
....0.......
6+
......A.....
7+
............
8+
............
9+
........A...
10+
.........A..
11+
............
12+
............

crates/day08/input.txt

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
.E..........m..0N.........f.......................
2+
........N........P0...............................
3+
.......j..................................F.......
4+
........1j............P........................C..
5+
...........................3..K......f..........E.
6+
...........V...y...0.....................F........
7+
1.......j.....P....y.N.......................F....
8+
....................m...................C.........
9+
..L......P....p..................w.m..............
10+
............E......p..AU........8......f..........
11+
..............C...............w....d..............
12+
j1...............E..........3.........f........w..
13+
.................p...A..........3.................
14+
.................3..p........KU...w..r..F.........
15+
7.........y........8.......................r......
16+
........y..u......K...............................
17+
...1..................8....C...K..................
18+
...........h.......................6..............
19+
......................U.........A.r..t........6...
20+
...........5.........8..c.........................
21+
.................U................t...............
22+
.....L...O...................t.............d......
23+
.........7........................................
24+
......L..H...c.....9....t.................6.......
25+
...........................c.M..................4.
26+
.....R..7...O.....................................
27+
.......................9......................d...
28+
..................................................
29+
.........L..9...R..........................6c.....
30+
..M.....T.5.................................d.....
31+
.......5OR...................T....................
32+
.......D......o.........v...................r.....
33+
...u....o.........5...............................
34+
.......WR.....Y...........................e...4...
35+
T............O......M..................4..a.......
36+
.Y...................M............................
37+
........W..D...............oh............e........
38+
.......7......Do...................A...e.......4..
39+
.W...Y..D........................h...v..........e.
40+
..........V.....9.l.......h.......a.........n..v..
41+
.......................H.....a2...................
42+
..................................................
43+
...V............Y....J..H2................vn......
44+
..............................H2.................n
45+
................V..........l...........k..........
46+
.T..u........................J...ak...............
47+
..................J.....l.........................
48+
.................l................................
49+
......u.........................................n.
50+
......................J..k............2...........

0 commit comments

Comments
 (0)