Last active
January 6, 2025 08:34
-
-
Save jhusain/c23da01c4d0de9b9d74c09db917d74f6 to your computer and use it in GitHub Desktop.
Revisions
-
jhusain renamed this gist
Jan 6, 2025 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
jhusain revised this gist
Jan 4, 2025 . 1 changed file with 9 additions and 13 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,11 +5,7 @@ fn defrag_disk(sectors: &mut [Option<u32>]) -> Option<()> { let left_idx = sectors.iter().position(|s| s.is_none())?; // Find the last Full sector after the left_idx let right_idx = sectors[left_idx + 1..].iter().rposition(|s| s.is_some())? + left_idx + 1; // Swap the Empty and Full sectors sectors.swap(left_idx, right_idx); @@ -31,16 +27,16 @@ fn main() { let mut id = 0u32; for (idx, num) in disk_map.into_iter().enumerate() { let val = if idx % 2 == 0 { let v = Some(id); id += 1; v } else { None }; for _ in 0..num { disk.push(val); } } -
jhusain revised this gist
Jan 4, 2025 . 1 changed file with 11 additions and 23 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,36 +1,24 @@ fn defrag_disk(sectors: &mut [Option<u32>]) -> Option<()> { let mut sectors = sectors; loop { // Find the first Empty sector let left_idx = sectors.iter().position(|s| s.is_none())?; // Find the last Full sector after the left_idx let right_idx_from_right = sectors[left_idx + 1..sectors.len()] .iter() .rev() .position(|s| s.is_some())?; let right_idx = sectors.len() - right_idx_from_right - 1; // Swap the Empty and Full sectors sectors.swap(left_idx, right_idx); // Focus on the remaining unsorted range sectors = &mut sectors[left_idx + 1..right_idx]; } } fn main() { let disk_map_str = "2333133121414131402"; let disk_map: Vec<u32> = disk_map_str @@ -44,14 +32,14 @@ fn main() { let mut id = 0u32; for (idx, num) in disk_map.into_iter().enumerate() { if idx % 2 == 0 { let val = Some(id); id += 1; for _ in 0..num { disk.push(val); } } else { for _ in 0..num { disk.push(None); } } } -
jhusain revised this gist
Jan 3, 2025 . 1 changed file with 7 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,12 +6,19 @@ enum DiskSector { fn defrag_disk_recur(sectors: &mut [DiskSector]) -> Option<()> { let mut sectors = sectors; // This loop is guaranteed to terminate because eventually the left_idx or right_idx // will not be found as the slice and the function will early return. loop { // Note the use of the '?' operator here. This will short circuit // the function and return None if the expression evaluates to None. let left_idx = sectors.iter().position(|s| *s == DiskSector::Empty)?; // What's great about slices is that getting a new one with a range never // throws an OutOfBounds error. It just truncates the slice size to 0 or len. let right_idx_from_right = sectors[left_idx + 1..sectors.len()] .iter() .rev() // Note use of the '?' operator again. .position(|s| matches!(s, DiskSector::Full(_)))?; let right_idx = sectors.len() - right_idx_from_right - 1; -
jhusain revised this gist
Jan 3, 2025 . 1 changed file with 13 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,17 +5,19 @@ enum DiskSector { } fn defrag_disk_recur(sectors: &mut [DiskSector]) -> Option<()> { let mut sectors = sectors; loop { let left_idx = sectors.iter().position(|s| *s == DiskSector::Empty)?; let right_idx_from_right = sectors[left_idx + 1..sectors.len()] .iter() .rev() .position(|s| matches!(s, DiskSector::Full(_)))?; let right_idx = sectors.len() - right_idx_from_right - 1; sectors.swap(left_idx, right_idx); sectors = &mut sectors[left_idx + 1..right_idx]; } } fn defrag_disk(disk: &mut Vec<DiskSector>) { -
jhusain revised this gist
Jan 3, 2025 . No changes.There are no files selected for viewing
-
jhusain revised this gist
Jan 3, 2025 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -7,7 +7,7 @@ enum DiskSector { fn defrag_disk_recur(sectors: &mut [DiskSector]) -> Option<()> { let left_idx = sectors.iter().position(|s| *s == DiskSector::Empty)?; let right_idx_from_right = sectors[left_idx + 1..sectors.len()] .iter() .rev() .position(|s| matches!(s, DiskSector::Full(_)))?; -
jhusain revised this gist
Jan 3, 2025 . 1 changed file with 10 additions and 12 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,22 +4,20 @@ enum DiskSector { Empty, } fn defrag_disk_recur(sectors: &mut [DiskSector]) -> Option<()> { let left_idx = sectors.iter().position(|s| *s == DiskSector::Empty)?; let right_idx_from_right = sectors .iter() .rev() .position(|s| matches!(s, DiskSector::Full(_)))?; let right_idx = sectors.len() - right_idx_from_right - 1; sectors.swap(left_idx, right_idx); defrag_disk_recur(&mut sectors[left_idx + 1..right_idx]); Some(()) } fn defrag_disk(disk: &mut Vec<DiskSector>) { defrag_disk_recur(disk.as_mut_slice()); } -
jhusain created this gist
Jan 3, 2025 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,54 @@ #[derive(Clone, Copy, Debug, PartialEq, Eq)] enum DiskSector { Full(u32), Empty, } fn defrag_disk_recur(sectors: &mut [DiskSector]) { let left_idx_search = sectors.iter().position(|s| *s == DiskSector::Empty); let right_idx_search = sectors .iter() .rev() .position(|s| matches!(s, DiskSector::Full(_))) .and_then(|idx| Some(sectors.len() - idx - 1)); match (left_idx_search, right_idx_search) { (Some(left_idx), Some(right_idx)) => { sectors.swap(left_idx, right_idx); defrag_disk_recur(&mut sectors[left_idx + 1..right_idx]); } _ => (), } } fn defrag_disk(disk: &mut Vec<DiskSector>) { defrag_disk_recur(disk.as_mut_slice()); } fn main() { let disk_map_str = "2333133121414131402"; let disk_map: Vec<u32> = disk_map_str .chars() .map(|c| c.to_digit(10).unwrap()) .collect(); let disk_capacity: u32 = disk_map.iter().sum(); let mut disk = Vec::with_capacity(disk_capacity.try_into().unwrap()); let mut id = 0u32; for (idx, num) in disk_map.into_iter().enumerate() { if idx % 2 == 0 { let val = DiskSector::Full(id); id = id + 1; for _ in 0..num { disk.push(val); } } else { for _ in 0..num { disk.push(DiskSector::Empty); } } } defrag_disk(&mut disk); println!("{:?}", disk); }