Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e1b8a34

Browse filesBrowse files
committed
Added day 11 part 1 Rust based solution
1 parent d6fcacf commit e1b8a34
Copy full SHA for e1b8a34

File tree

5 files changed

+79
-0
lines changed
Filter options

5 files changed

+79
-0
lines changed

‎2024/day11/Cargo.toml

Copy file name to clipboard
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "day11"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
8+
[[bin]]
9+
name = "day11"
10+
path = "day11.rs"

‎2024/day11/day11.rs

Copy file name to clipboard
+59Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
use std::{env, fs, process::exit};
2+
3+
fn blink(paddles: Vec<i64>) -> Vec<i64> {
4+
paddles
5+
.iter()
6+
.flat_map(|&p| {
7+
if p == 0 {
8+
vec![1]
9+
} else if p.to_string().len() % 2 == 0 {
10+
let s = p.to_string();
11+
let (left, right) = s.split_at(s.len() / 2);
12+
vec![left.parse().unwrap(), right.parse().unwrap()]
13+
} else {
14+
vec![p * 2024]
15+
}
16+
})
17+
.collect()
18+
}
19+
20+
fn part1(input_raw_paddles: String) -> i64 {
21+
let mut paddles = input_raw_paddles
22+
.split(" ")
23+
.map(|p| p.parse::<i64>().unwrap())
24+
.collect::<Vec<i64>>();
25+
26+
for _ in 0..25 {
27+
paddles = blink(paddles);
28+
}
29+
30+
paddles.len() as i64
31+
}
32+
33+
fn part2(input_data: String) -> i32 {
34+
0
35+
}
36+
37+
#[cfg(test)]
38+
mod tests {
39+
use super::*;
40+
41+
#[test]
42+
fn test1() {
43+
let file_content = fs::read_to_string("./sample1.txt").unwrap();
44+
assert_eq!(part1(file_content), 55312);
45+
}
46+
}
47+
48+
fn main() {
49+
let input_path = env::args().nth(1);
50+
if input_path.is_none() {
51+
println!("Input path should be specified!");
52+
exit(1);
53+
}
54+
55+
println!(
56+
"Output: {}",
57+
part1(fs::read_to_string(input_path.unwrap()).unwrap())
58+
);
59+
}

‎2024/day11/sample1.txt

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

‎2024/day11/sample2.txt

Copy file name to clipboard
+8Lines 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

‎readme.md

Copy file name to clipboardExpand all lines: readme.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Para esta edição, planejo usar três linguagens de programação: Rust, C e Ja
2020
| Dia 8 | [Rust](./2024/day8/day8.rs) |
2121
| Dia 9 | [Rust](./2024/day9/day9.rs) |
2222
| Dia 10 | [Rust](./2024/day10/day10.rs) |
23+
| Dia 11 | [Rust](./2024/day11/day11.rs) |
2324

2425
### 2023
2526

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.