Compare commits
3 Commits
2bc751dd0d
...
206c1fca85
Author | SHA1 | Date | |
---|---|---|---|
206c1fca85 | |||
f7cf4f1e9f | |||
b08c8fbd80 |
@ -1,4 +1,3 @@
|
|||||||
use std::collections::LinkedList;
|
|
||||||
use std::fmt::{Display, Write};
|
use std::fmt::{Display, Write};
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{BufRead, BufReader, Lines};
|
use std::io::{BufRead, BufReader, Lines};
|
||||||
@ -53,6 +52,7 @@ struct Inode {
|
|||||||
struct DiskMap {
|
struct DiskMap {
|
||||||
map: Vec<Unit>,
|
map: Vec<Unit>,
|
||||||
files: Vec<Inode>,
|
files: Vec<Inode>,
|
||||||
|
frees: Vec<Inode>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: BufRead> From<Lines<T>> for DiskMap {
|
impl<T: BufRead> From<Lines<T>> for DiskMap {
|
||||||
@ -62,8 +62,9 @@ impl<T: BufRead> From<Lines<T>> for DiskMap {
|
|||||||
let mut file_id = 0;
|
let mut file_id = 0;
|
||||||
let mut map = Vec::new();
|
let mut map = Vec::new();
|
||||||
let mut files = Vec::new();
|
let mut files = Vec::new();
|
||||||
for i in 0..line.len() {
|
let mut frees = Vec::new();
|
||||||
let len = line[i] - b'0';
|
for (i, c) in line.iter().enumerate() {
|
||||||
|
let len = c - b'0';
|
||||||
if i % 2 == 0 {
|
if i % 2 == 0 {
|
||||||
// file
|
// file
|
||||||
files.push(Inode {
|
files.push(Inode {
|
||||||
@ -77,12 +78,17 @@ impl<T: BufRead> From<Lines<T>> for DiskMap {
|
|||||||
file_id += 1;
|
file_id += 1;
|
||||||
} else {
|
} else {
|
||||||
// free
|
// free
|
||||||
|
frees.push(Inode {
|
||||||
|
id: 0,
|
||||||
|
pos: map.len(),
|
||||||
|
len,
|
||||||
|
});
|
||||||
for _ in 0..len {
|
for _ in 0..len {
|
||||||
map.push(Unit::Free)
|
map.push(Unit::Free)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Self { map, files }
|
Self { map, files, frees }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,43 +121,28 @@ impl DiskMap {
|
|||||||
|
|
||||||
fn problem1<T: BufRead>(input: Lines<T>) -> u64 {
|
fn problem1<T: BufRead>(input: Lines<T>) -> u64 {
|
||||||
let mut map = DiskMap::from(input);
|
let mut map = DiskMap::from(input);
|
||||||
let mut i = map.map.len() - 1;
|
let mut last_free = 0;
|
||||||
while i != 0 {
|
for file in map.files.iter().rev() {
|
||||||
// find start index of 'file'
|
|
||||||
if map.map[i] == Unit::Free {
|
|
||||||
i -= 1;
|
|
||||||
continue;
|
|
||||||
};
|
|
||||||
let mut len = 1;
|
|
||||||
while i >= 1 && map.map[i] == map.map[i - 1] {
|
|
||||||
i -= 1;
|
|
||||||
len += 1;
|
|
||||||
}
|
|
||||||
let file_id = match map.map[i] {
|
|
||||||
Unit::File(id) => id,
|
|
||||||
_ => panic!(),
|
|
||||||
};
|
|
||||||
let frees = map
|
let frees = map
|
||||||
.map
|
.map
|
||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.take(i + len + 1)
|
.skip(last_free) // we greedy fill, so no need to check for free space before the last one we used
|
||||||
.filter(|(_i, u)| **u == Unit::Free || **u == Unit::File(file_id))
|
.take(file.pos + file.len as usize) // and we only need to search until the end of the current file
|
||||||
|
.filter(|(_i, u)| **u == Unit::Free || **u == Unit::File(file.id)) // look for free space or our existing space
|
||||||
.map(|(i, _u)| i)
|
.map(|(i, _u)| i)
|
||||||
.take(len)
|
.take(file.len as usize) // get the first file.len free blocks
|
||||||
.collect_vec();
|
.collect_vec();
|
||||||
if frees[0] >= i {
|
// Note: no need to test for too small frees list here, since we are guaranteed at worst to find our current position
|
||||||
if i > 0 {
|
if frees[0] >= file.pos {
|
||||||
i -= 1;
|
// if the first available free is > file.pos, it's fully packed, job done
|
||||||
}
|
break;
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
for j in 0..len {
|
#[allow(clippy::needless_range_loop)]
|
||||||
map.map.swap(frees[j], i + j);
|
for j in 0..file.len as usize {
|
||||||
}
|
map.map.swap(frees[j], file.pos + j);
|
||||||
if i > 0 {
|
|
||||||
i -= 1;
|
|
||||||
}
|
}
|
||||||
|
last_free = frees[file.len as usize - 1]
|
||||||
}
|
}
|
||||||
map.checksum()
|
map.checksum()
|
||||||
}
|
}
|
||||||
@ -159,30 +150,27 @@ fn problem1<T: BufRead>(input: Lines<T>) -> u64 {
|
|||||||
// PROBLEM 2 solution
|
// PROBLEM 2 solution
|
||||||
fn problem2<T: BufRead>(input: Lines<T>) -> u64 {
|
fn problem2<T: BufRead>(input: Lines<T>) -> u64 {
|
||||||
let mut map = DiskMap::from(input);
|
let mut map = DiskMap::from(input);
|
||||||
// println!("before: {}", map);
|
|
||||||
for file in map.files.iter().rev() {
|
for file in map.files.iter().rev() {
|
||||||
let free_pos = map
|
let free = map.frees.iter_mut().find(|inode| inode.len >= file.len); // find the first entry in the free space map large enough
|
||||||
.map
|
if let Some(free) = free {
|
||||||
.windows(file.len as usize)
|
if free.pos >= file.pos {
|
||||||
.take(file.pos)
|
// if it's past our position, continue, but can't break since there might be free space for future files
|
||||||
.enumerate()
|
continue;
|
||||||
.find(|(_i, u)| {
|
|
||||||
u.iter().all(|u| match u {
|
|
||||||
Unit::Free => true,
|
|
||||||
_ => false,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.map(|(i, _)| i);
|
|
||||||
if let Some(free_pos) = free_pos {
|
|
||||||
// println!("moving {}@{:?} to {}", file.id, file.pos, free_pos);
|
|
||||||
for j in 0..file.len {
|
|
||||||
map.map[free_pos + j as usize] = map.map[file.pos + j as usize];
|
|
||||||
map.map[file.pos + j as usize] = Unit::Free;
|
|
||||||
}
|
}
|
||||||
// println!("after: {}", map);
|
for j in 0..file.len {
|
||||||
|
map.map.swap(free.pos + j as usize, file.pos + j as usize);
|
||||||
|
}
|
||||||
|
// Note: It is slightly faster to keep these hanging around in the free map with size = 0 then to remove them from the vec
|
||||||
|
free.len -= file.len;
|
||||||
|
free.pos += file.len as usize;
|
||||||
|
|
||||||
|
map.frees.push(Inode {
|
||||||
|
id: 0,
|
||||||
|
pos: file.pos,
|
||||||
|
len: file.len,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// println!("after: {}", map);
|
|
||||||
map.checksum()
|
map.checksum()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user