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 e10709e

Browse filesBrowse files
committed
Added day 9 part 2 Rust based solution
1 parent 5d5805e commit e10709e
Copy full SHA for e10709e

File tree

2 files changed

+46
-4
lines changed
Filter options

2 files changed

+46
-4
lines changed

‎2024/day9/day9.rs

Copy file name to clipboardExpand all lines: 2024/day9/day9.rs
+45-4Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{env, fs, process::exit};
1+
use std::{env, fs, ops::Range, process::exit};
22

33
fn part1(input_data: String) -> u64 {
44
let mut disk_content = Vec::new();
@@ -39,8 +39,43 @@ fn part1(input_data: String) -> u64 {
3939
.sum()
4040
}
4141

42-
fn part2(input_data: String) -> i32 {
43-
return 0;
42+
fn part2(input_data: String) -> u64 {
43+
let mut files: Vec<(Range<usize>, u64)> = Vec::new();
44+
let mut free_blocks: Vec<Range<usize>> = Vec::new();
45+
46+
let mut curr_i = 0;
47+
48+
for (index, size_char) in input_data.chars().enumerate() {
49+
let space_size = size_char.to_digit(10).unwrap();
50+
let space_range = curr_i as usize..curr_i as usize + space_size as usize;
51+
curr_i = space_range.end;
52+
53+
if index % 2 == 0 {
54+
files.push((space_range, index as u64 / 2));
55+
} else {
56+
free_blocks.push(space_range);
57+
}
58+
}
59+
60+
for file in files.iter_mut().rev() {
61+
if let Some((i, free)) = free_blocks
62+
.iter_mut()
63+
.enumerate()
64+
.find(|(_, f)| f.end <= file.0.start && f.len() >= file.0.len())
65+
{
66+
let free_start = free.start;
67+
*free = free_start + file.0.len()..free.end;
68+
*file = (free_start..free_start + file.0.len(), file.1);
69+
if free.len() == 0 {
70+
free_blocks.remove(i);
71+
}
72+
}
73+
}
74+
75+
files
76+
.into_iter()
77+
.map(|f| f.0.sum::<usize>() as u64 * f.1)
78+
.sum()
4479
}
4580

4681
#[cfg(test)]
@@ -52,6 +87,12 @@ mod tests {
5287
let file_content = fs::read_to_string("./sample1.txt").unwrap();
5388
assert_eq!(part1(file_content), 1928);
5489
}
90+
91+
#[test]
92+
fn test2() {
93+
let file_content = fs::read_to_string("./sample2.txt").unwrap();
94+
assert_eq!(part2(file_content), 2858);
95+
}
5596
}
5697

5798
fn main() {
@@ -63,6 +104,6 @@ fn main() {
63104

64105
println!(
65106
"Output: {}",
66-
part1(fs::read_to_string(input_path.unwrap()).unwrap())
107+
part2(fs::read_to_string(input_path.unwrap()).unwrap())
67108
);
68109
}

‎2024/day9/sample2.txt

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

0 commit comments

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