Skip to content

Commit f1b58fa

Browse files
committed
add day11
1 parent 46d8aa9 commit f1b58fa

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

2024/day11/.gitignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
input.txt
2+
flamegraph.svg
3+
perf.data*
4+
### Rust
5+
# Generated by Cargo
6+
# will have compiled files and executables
7+
debug/
8+
target/
9+
10+
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
11+
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
12+
Cargo.lock
13+
14+
# These are backup files generated by rustfmt
15+
**/*.rs.bk
16+
17+
# MSVC Windows builds of rustc generate these, which store debugging information
18+
*.pdb
19+

2024/day11/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "day11"
3+
authors = ["mirsella <[email protected]>"]
4+
version = "0.1.0"
5+
edition = "2021"
6+
7+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
8+
9+
[dependencies]
10+
regex = { workspace = true }
11+
itertools = { workspace = true }
12+
pathfinding = { workspace = true }
13+
rayon = { workspace = true }
14+
indexmap = { workspace = true }

2024/day11/src/main.rs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
use std::collections::HashMap;
2+
3+
fn part(input: &str, count: u64) -> u64 {
4+
let mut stones = input
5+
.split_whitespace()
6+
.map(|s| (s.parse::<u64>().unwrap(), 1))
7+
.collect::<HashMap<_, _>>();
8+
for _ in 0..count {
9+
let mut new = HashMap::with_capacity(stones.len());
10+
for (k, v) in stones {
11+
let kstr = k.to_string();
12+
if k == 0 {
13+
*new.entry(1).or_default() += v;
14+
} else if kstr.len() % 2 == 0 {
15+
let (a, b) = kstr.split_at(kstr.len() / 2);
16+
*new.entry(a.parse().unwrap()).or_default() += v;
17+
*new.entry(b.parse().unwrap()).or_default() += v;
18+
} else {
19+
*new.entry(k * 2024).or_default() += v;
20+
}
21+
}
22+
stones = new;
23+
}
24+
stones.values().sum()
25+
}
26+
27+
fn main() {
28+
let input = include_str!("../input.txt");
29+
println!("Part 1: {}", part(input, 25));
30+
println!("Part 2: {}", part(input, 75));
31+
}
32+
33+
#[cfg(test)]
34+
mod tests {
35+
const INPUT: &str = "125 17";
36+
#[test]
37+
fn part1() {
38+
assert_eq!(super::part(INPUT, 25), 55312);
39+
}
40+
}

0 commit comments

Comments
 (0)