Skip to content

Instantly share code, notes, and snippets.

@jhusain
Last active January 6, 2025 08:34
Show Gist options
  • Save jhusain/c23da01c4d0de9b9d74c09db917d74f6 to your computer and use it in GitHub Desktop.
Save jhusain/c23da01c4d0de9b9d74c09db917d74f6 to your computer and use it in GitHub Desktop.

Revisions

  1. jhusain renamed this gist Jan 6, 2025. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. jhusain revised this gist Jan 4, 2025. 1 changed file with 9 additions and 13 deletions.
    22 changes: 9 additions & 13 deletions advent-of-code-7.rs
    Original 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_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;
    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() {
    if idx % 2 == 0 {
    let val = Some(id);
    let val = if idx % 2 == 0 {
    let v = Some(id);
    id += 1;
    for _ in 0..num {
    disk.push(val);
    }
    v
    } else {
    for _ in 0..num {
    disk.push(None);
    }
    None
    };

    for _ in 0..num {
    disk.push(val);
    }
    }

  3. jhusain revised this gist Jan 4, 2025. 1 changed file with 11 additions and 23 deletions.
    34 changes: 11 additions & 23 deletions advent-of-code-7.rs
    Original file line number Diff line number Diff line change
    @@ -1,36 +1,24 @@
    #[derive(Clone, Copy, Debug, PartialEq, Eq)]
    enum DiskSector {
    Full(u32),
    Empty,
    }

    fn defrag_disk_recur(sectors: &mut [DiskSector]) -> Option<()> {
    fn defrag_disk(sectors: &mut [Option<u32>]) -> 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)?;
    // Find the first Empty sector
    let left_idx = sectors.iter().position(|s| s.is_none())?;

    // 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.
    // Find the last Full sector after the left_idx
    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(_)))?;
    .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 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
    @@ -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 = DiskSector::Full(id);
    id = id + 1;
    let val = Some(id);
    id += 1;
    for _ in 0..num {
    disk.push(val);
    }
    } else {
    for _ in 0..num {
    disk.push(DiskSector::Empty);
    disk.push(None);
    }
    }
    }
  4. jhusain revised this gist Jan 3, 2025. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions advent-of-code-7.rs
    Original 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;

  5. jhusain revised this gist Jan 3, 2025. 1 changed file with 13 additions and 11 deletions.
    24 changes: 13 additions & 11 deletions advent-of-code-7.rs
    Original file line number Diff line number Diff line change
    @@ -5,17 +5,19 @@ 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(_)))?;
    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(())
    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>) {
  6. jhusain revised this gist Jan 3, 2025. No changes.
  7. jhusain revised this gist Jan 3, 2025. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion advent-of-code-7.rs
    Original 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
    let right_idx_from_right = sectors[left_idx + 1..sectors.len()]
    .iter()
    .rev()
    .position(|s| matches!(s, DiskSector::Full(_)))?;
  8. jhusain revised this gist Jan 3, 2025. 1 changed file with 10 additions and 12 deletions.
    22 changes: 10 additions & 12 deletions advent-of-code-7.rs
    Original file line number Diff line number Diff line change
    @@ -4,22 +4,20 @@ enum DiskSector {
    Empty,
    }

    fn defrag_disk_recur(sectors: &mut [DiskSector]) {
    let left_idx_search = sectors.iter().position(|s| *s == DiskSector::Empty);
    let right_idx_search = sectors
    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(_)))
    .and_then(|idx| Some(sectors.len() - idx - 1));
    .position(|s| matches!(s, DiskSector::Full(_)))?;
    let right_idx = sectors.len() - right_idx_from_right - 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]);
    }
    _ => (),
    }
    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());
    }
  9. jhusain created this gist Jan 3, 2025.
    54 changes: 54 additions & 0 deletions advent-of-code-7.rs
    Original 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);
    }