|
| 1 | +//! # Disk Fragmenter |
| 2 | +//! |
| 3 | +//! ## Part One |
| 4 | +//! |
| 5 | +//! Computes the checksum by simultaneously scanning forward for free blocks and |
| 6 | +//! backwards for files. No memory is allocated which makes it very fast. |
| 7 | +//! |
| 8 | +//! ## Part Two |
| 9 | +//! |
| 10 | +//! We build 10 [min heaps](https://en.wikipedia.org/wiki/Heap_(data_structure)) in an array to |
| 11 | +//! store the free space offsets. The index of the array implicitly stores the size of the |
| 12 | +//! free block. |
| 13 | +//! |
| 14 | +//! When moving a file to a free block, the corresponding heap is popped and then any leftover |
| 15 | +//! space is pushed back to the heap at a smaller index. The heap at index zero is not used |
| 16 | +//! but makes the indexing easier. |
| 17 | +use crate::util::heap::*; |
| 18 | + |
| 19 | +/// [Triangular numbers](https://en.wikipedia.org/wiki/Triangular_number) offset by two. |
| 20 | +/// Files can be a max size of 9 so we only need the first 10 values, including zero to make |
| 21 | +/// indexing easier. |
| 22 | +const EXTRA: [usize; 10] = [0, 0, 1, 3, 6, 10, 15, 21, 28, 36]; |
| 23 | + |
| 24 | +/// Remove any trailing newlines and convert to `usize`. |
| 25 | +pub fn parse(input: &str) -> Vec<usize> { |
| 26 | + input.trim().bytes().map(|b| (b - b'0') as usize).collect() |
| 27 | +} |
| 28 | + |
| 29 | +/// Block by block checksum comparison that doesn't allocate any memory. |
| 30 | +pub fn part1(disk: &[usize]) -> usize { |
| 31 | + // Start at the first free block and the last file. |
| 32 | + let mut free = 0; |
| 33 | + let mut file = disk.len() + disk.len() % 2; |
| 34 | + |
| 35 | + let mut available = 0; |
| 36 | + let mut needed = 0; |
| 37 | + |
| 38 | + let mut block = 0; |
| 39 | + let mut checksum = 0; |
| 40 | + |
| 41 | + while free < file { |
| 42 | + // Take as much space as possible from the current free block range. |
| 43 | + let size = needed.min(available); |
| 44 | + (checksum, block) = update(checksum, block, file, size); |
| 45 | + available -= size; |
| 46 | + needed -= size; |
| 47 | + |
| 48 | + // One or both of "available" and "free" could be zero. |
| 49 | + if needed == 0 { |
| 50 | + file -= 2; |
| 51 | + needed = disk[file]; |
| 52 | + } |
| 53 | + |
| 54 | + // When moving to the next free block, add the checksum for the file we're skipping over. |
| 55 | + if available == 0 { |
| 56 | + let size = disk[free]; |
| 57 | + (checksum, block) = update(checksum, block, free, size); |
| 58 | + available = disk[free + 1]; |
| 59 | + free += 2; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Account for any remaining file blocks left over. |
| 64 | + (checksum, _) = update(checksum, block, file, needed); |
| 65 | + checksum |
| 66 | +} |
| 67 | + |
| 68 | +pub fn part2(disk: &[usize]) -> usize { |
| 69 | + let mut block = 0; |
| 70 | + let mut checksum = 0; |
| 71 | + let mut free: Vec<_> = (0..10).map(|_| MinHeap::with_capacity(1_000)).collect(); |
| 72 | + |
| 73 | + // Build a min-heap (leftmost free block first) where the size of each block is |
| 74 | + // implicit in the index of the array. |
| 75 | + for (index, &size) in disk.iter().enumerate() { |
| 76 | + if index % 2 == 1 && size > 0 { |
| 77 | + free[size].push(block, ()); |
| 78 | + } |
| 79 | + |
| 80 | + block += size; |
| 81 | + } |
| 82 | + |
| 83 | + for (index, &size) in disk.iter().enumerate().rev() { |
| 84 | + block -= size; |
| 85 | + |
| 86 | + // Count any previous free blocks to decrement block offset correctly. |
| 87 | + if index % 2 == 1 { |
| 88 | + continue; |
| 89 | + } |
| 90 | + |
| 91 | + // Find the leftmost free block that can fit the file (if any). |
| 92 | + let mut next_block = block; |
| 93 | + let mut next_index = usize::MAX; |
| 94 | + |
| 95 | + for (i, heap) in free.iter().enumerate().skip(size) { |
| 96 | + if let Some((&first, ())) = heap.peek() { |
| 97 | + if first < next_block { |
| 98 | + next_block = first; |
| 99 | + next_index = i; |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // We can make smaller free block from bigger blocks but not the other way around. |
| 105 | + // As an optimization if all blocks of the biggest size are after our position then |
| 106 | + // we can ignore them. |
| 107 | + if !free.is_empty() { |
| 108 | + let last = free.len() - 1; |
| 109 | + if let Some((&first, ())) = free[last].peek() { |
| 110 | + if first > block { |
| 111 | + free.pop(); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // Update the checksum with the file's location (possibly unchanged). |
| 117 | + let id = index / 2; |
| 118 | + let extra = next_block * size + EXTRA[size]; |
| 119 | + checksum += id * extra; |
| 120 | + |
| 121 | + // If we used a free block, remove then add back any leftover space. |
| 122 | + if next_index != usize::MAX { |
| 123 | + free[next_index].pop(); |
| 124 | + if size < next_index { |
| 125 | + free[next_index - size].push(next_block + size, ()); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + checksum |
| 131 | +} |
| 132 | + |
| 133 | +/// Convenience function to update checksum based on file location and size. |
| 134 | +#[inline] |
| 135 | +fn update(checksum: usize, block: usize, index: usize, size: usize) -> (usize, usize) { |
| 136 | + let id = index / 2; |
| 137 | + let extra = block * size + EXTRA[size]; |
| 138 | + (checksum + id * extra, block + size) |
| 139 | +} |
0 commit comments