Skip to content

Commit e04ea6d

Browse files
authored
feat: day11 (#15)
1 parent c08d0bc commit e04ea6d

File tree

26 files changed

+272
-34
lines changed

26 files changed

+272
-34
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,16 @@ members = [
1111
"crates/day08",
1212
"crates/day09",
1313
"crates/day10",
14+
"crates/day11",
1415
"crates/helpers",
1516
]
1617

18+
[workspace.package]
19+
authors = ["martabal"]
20+
edition = "2021"
21+
rust-version = "1.83"
22+
license = "MIT"
23+
1724
[workspace.dependencies]
1825
regex = "1.11.1"
1926

cpp/day01.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ int main() {
6161
std::cout << "Part Two solution: sum is " << sum << std::endl;
6262

6363
} catch (const std::exception &e) {
64-
std::cerr << "Error: " << e.what() << '\n';
64+
std::cerr << "Error: " << e.what() << std::endl;
6565
return 1;
6666
}
6767

cpp/day02.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ int main() {
7373
}
7474
std::cout << "Part Two solution: sum is " << safe << std::endl;
7575
} catch (const std::exception &e) {
76-
std::cerr << "Error: " << e.what() << '\n';
76+
std::cerr << "Error: " << e.what() << std::endl;
7777
return 1;
7878
}
7979

cpp/day03.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ int main() {
155155
count = parseWithRules(message);
156156
std::cout << "Part Two solution: sum is " << count << std::endl;
157157
} catch (const std::exception &e) {
158-
std::cerr << "Error: " << e.what() << '\n';
158+
std::cerr << "Error: " << e.what() << std::endl;
159159
return 1;
160160
}
161161

cpp/day04.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,13 @@ int main() {
135135

136136
// --- Part One ---
137137
size_t count = count_word(grid, "XMAS");
138-
std::cout << count << '\n';
138+
std::cout << count << std::endl;
139139

140140
// --- Part Two ---
141141
count = count_x_pattern(grid, "MAS");
142-
std::cout << count << '\n';
142+
std::cout << count << std::endl;
143143
} catch (const std::exception &e) {
144-
std::cerr << "Error: " << e.what() << '\n';
144+
std::cerr << "Error: " << e.what() << std::endl;
145145
return 1;
146146
}
147147

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 << "Error: " << e.what() << "\n";
157+
std::cerr << "Error: " << e.what() << std::endl;
158158
}
159159

160160
return 0;

cpp/day09.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,15 @@ int main() {
169169
checker.fragmentFiles();
170170
std::cout << "Part One solution: "
171171
<< FileSystem::calculateChecksum(checker.getDefragmentedFiles())
172-
<< "\n";
172+
<< std::endl;
173173

174174
// --- Part Two ---
175175
checker.fragmentFilesBlock();
176176
std::cout << "Part Two solution: "
177-
<< FileSystem::calculateChecksum(checker.getFiles()) << "\n";
177+
<< FileSystem::calculateChecksum(checker.getFiles()) << std::endl;
178178

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

cpp/day11.cc

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#include "helpers.h"
2+
#include <cstdint>
3+
#include <fstream>
4+
#include <iostream>
5+
#include <sstream>
6+
#include <string>
7+
#include <unordered_map>
8+
#include <vector>
9+
10+
const uint8_t BLINK_NUMBER_PART_1 = 25;
11+
const uint8_t BLINK_NUMBER_PART_2 = 75;
12+
13+
class Stones {
14+
private:
15+
std::unordered_map<uint64_t, size_t> stones;
16+
17+
static std::unordered_map<uint64_t, size_t>
18+
rules(const std::unordered_map<uint64_t, size_t> &stones) {
19+
std::unordered_map<uint64_t, size_t> new_stones;
20+
21+
for (const auto &[stone, amount] : stones) {
22+
std::string stone_str = std::to_string(stone);
23+
24+
if (stone == 0) {
25+
new_stones[1] += amount;
26+
} else if (stone_str.length() % 2 == 0) {
27+
size_t mid = stone_str.length() / 2;
28+
uint64_t first_half = std::stoull(stone_str.substr(0, mid));
29+
uint64_t second_half = std::stoull(stone_str.substr(mid));
30+
new_stones[first_half] += amount;
31+
new_stones[second_half] += amount;
32+
} else {
33+
new_stones[stone * 2024] += amount;
34+
}
35+
}
36+
37+
return new_stones;
38+
}
39+
40+
public:
41+
explicit Stones(const std::string &content) {
42+
std::istringstream iss(content);
43+
uint64_t stone;
44+
while (iss >> stone) {
45+
stones[stone]++;
46+
}
47+
}
48+
49+
size_t blink(uint8_t blink_number) {
50+
auto new_stones = stones;
51+
for (uint8_t i = 0; i < blink_number; i++) {
52+
new_stones = rules(new_stones);
53+
}
54+
55+
size_t sum = 0;
56+
for (const auto &[_, amount] : new_stones) {
57+
sum += amount;
58+
}
59+
return sum;
60+
}
61+
};
62+
63+
int main() {
64+
try {
65+
std::string message = readFile("crates/day11/input.txt");
66+
Stones stones(message);
67+
68+
// --- Part One ---
69+
std::cout << "Part One solution: " << stones.blink(BLINK_NUMBER_PART_1)
70+
<< std::endl;
71+
72+
// --- Part Two ---
73+
std::cout << "Part Two solution: " << stones.blink(BLINK_NUMBER_PART_2)
74+
<< std::endl;
75+
} catch (const std::exception &e) {
76+
std::cerr << "Error: " << e.what() << std::endl;
77+
return 1;
78+
}
79+
return 0;
80+
}

crates/day01/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
[package]
22
name = "day01"
33
version = "0.1.1"
4-
edition = "2021"
4+
authors.workspace = true
5+
edition.workspace = true
6+
rust-version.workspace = true
7+
license.workspace = true
58

69
[lints]
710
workspace = true

crates/day02/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
[package]
22
name = "day02"
33
version = "0.1.0"
4-
edition = "2021"
4+
authors.workspace = true
5+
edition.workspace = true
6+
rust-version.workspace = true
7+
license.workspace = true
58

69
[lints]
710
workspace = true

crates/day03/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
[package]
22
name = "day03"
33
version = "0.2.0"
4-
edition = "2021"
4+
authors.workspace = true
5+
edition.workspace = true
6+
rust-version.workspace = true
7+
license.workspace = true
58

69
[lints]
710
workspace = true

crates/day04/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
[package]
22
name = "day04"
33
version = "0.1.0"
4-
edition = "2021"
4+
authors.workspace = true
5+
edition.workspace = true
6+
rust-version.workspace = true
7+
license.workspace = true
58

69
[lints]
710
workspace = true

crates/day04/src/main.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ struct Grid {
99
grid: Vec<Vec<char>>,
1010
}
1111

12+
const DIRECTIONS: [(isize, isize); 8] = [
13+
(0, 1), // Horizontal right
14+
(0, -1), // Horizontal left
15+
(1, 0), // Vertical down
16+
(-1, 0), // Vertical up
17+
(1, 1), // Diagonal down-right
18+
(1, -1), // Diagonal down-left
19+
(-1, 1), // Diagonal up-right
20+
(-1, -1), // Diagonal up-left
21+
];
22+
1223
const WORD_PART_ONE: &str = "XMAS";
1324
const WORD_PART_TWO: &str = "MAS";
1425

@@ -112,20 +123,9 @@ impl Grid {
112123
let cols = grid[0].len();
113124
let mut count = 0;
114125

115-
let directions = vec![
116-
(0, 1), // Horizontal right
117-
(0, -1), // Horizontal left
118-
(1, 0), // Vertical down
119-
(-1, 0), // Vertical up
120-
(1, 1), // Diagonal down-right
121-
(1, -1), // Diagonal down-left
122-
(-1, 1), // Diagonal up-right
123-
(-1, -1), // Diagonal up-left
124-
];
125-
126126
for row in 0..rows {
127127
for col in 0..cols {
128-
for (dx, dy) in &directions {
128+
for (dx, dy) in &DIRECTIONS {
129129
let mut found = true;
130130

131131
for (i, _) in word_chars.iter().enumerate().take(word_len) {

crates/day05/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
[package]
22
name = "day05"
33
version = "0.1.0"
4-
edition = "2021"
4+
authors.workspace = true
5+
edition.workspace = true
6+
rust-version.workspace = true
7+
license.workspace = true
58

69
[lints]
710
workspace = true

crates/day06/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
[package]
22
name = "day06"
33
version = "0.1.0"
4-
edition = "2021"
4+
authors.workspace = true
5+
edition.workspace = true
6+
rust-version.workspace = true
7+
license.workspace = true
58

69
[lints]
710
workspace = true

crates/day07/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
[package]
22
name = "day07"
33
version = "0.1.0"
4-
edition = "2021"
4+
authors.workspace = true
5+
edition.workspace = true
6+
rust-version.workspace = true
7+
license.workspace = true
58

69
[lints]
710
workspace = true

crates/day08/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
[package]
22
name = "day08"
33
version = "0.1.0"
4-
edition = "2021"
4+
authors.workspace = true
5+
edition.workspace = true
6+
rust-version.workspace = true
7+
license.workspace = true
58

69
[lints]
710
workspace = true

crates/day09/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
[package]
22
name = "day09"
33
version = "0.1.0"
4-
edition = "2021"
4+
authors.workspace = true
5+
edition.workspace = true
6+
rust-version.workspace = true
7+
license.workspace = true
58

69
[lints]
710
workspace = true

crates/day10/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
[package]
22
name = "day10"
33
version = "0.1.0"
4-
edition = "2021"
4+
authors.workspace = true
5+
edition.workspace = true
6+
rust-version.workspace = true
7+
license.workspace = true
58

69
[lints]
710
workspace = true

crates/day10/src/main.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,6 @@ fn main() {
100100
println!("Part Two solution: {}", checker.count_distinct_trailheads());
101101
}
102102

103-
104103
#[cfg(test)]
105104
mod tests {
106105
use super::*;

crates/day11/Cargo.toml

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

crates/day11/example_input.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
125 17

crates/day11/input.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
3 386358 86195 85 1267 3752457 0 741

0 commit comments

Comments
 (0)