day9: complete solution

previous commit should actually work for part 1 if you don't truncate
the input.
This commit is contained in:
Keenan Tims 2024-12-08 23:34:15 -08:00
parent a6ea5b4155
commit 2bc751dd0d
Signed by: ktims
GPG Key ID: 11230674D69038D4

View File

@ -44,8 +44,15 @@ enum Unit {
Free, Free,
} }
struct Inode {
id: usize,
pos: usize,
len: u8,
}
struct DiskMap { struct DiskMap {
map: Vec<Unit>, map: Vec<Unit>,
files: Vec<Inode>,
} }
impl<T: BufRead> From<Lines<T>> for DiskMap { impl<T: BufRead> From<Lines<T>> for DiskMap {
@ -54,21 +61,28 @@ impl<T: BufRead> From<Lines<T>> for DiskMap {
let line = line_s.as_bytes(); let line = line_s.as_bytes();
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();
for i in 0..line.len() { for i in 0..line.len() {
let len = line[i] - b'0';
if i % 2 == 0 { if i % 2 == 0 {
// file // file
for _ in 0..line[i] - b'0' { files.push(Inode {
id: file_id,
pos: map.len(),
len,
});
for _ in 0..len {
map.push(Unit::File(file_id)) map.push(Unit::File(file_id))
} }
file_id += 1; file_id += 1;
} else { } else {
// free // free
for _ in 0..line[i] - b'0' { for _ in 0..len {
map.push(Unit::Free) map.push(Unit::Free)
} }
} }
} }
Self { map } Self { map, files }
} }
} }
@ -101,8 +115,6 @@ 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);
println!("{}", map);
println!();
let mut i = map.map.len() - 1; let mut i = map.map.len() - 1;
while i != 0 { while i != 0 {
// find start index of 'file' // find start index of 'file'
@ -123,13 +135,15 @@ fn problem1<T: BufRead>(input: Lines<T>) -> u64 {
.map .map
.iter() .iter()
.enumerate() .enumerate()
.take(i + len) .take(i + len + 1)
.filter(|(_i, u)| **u == Unit::Free || **u == Unit::File(file_id)) .filter(|(_i, u)| **u == Unit::Free || **u == Unit::File(file_id))
.map(|(i, _u)| i) .map(|(i, _u)| i)
.take(len) .take(len)
.collect_vec(); .collect_vec();
if frees[0] == i && i > 0 { if frees[0] >= i {
i -= 1; if i > 0 {
i -= 1;
}
continue; continue;
} }
for j in 0..len { for j in 0..len {
@ -139,30 +153,20 @@ fn problem1<T: BufRead>(input: Lines<T>) -> u64 {
i -= 1; i -= 1;
} }
} }
println!("{}", map);
map.checksum() map.checksum()
} }
// 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);
let mut i = map.map.len() - 1; // println!("before: {}", map);
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 free_pos = map let free_pos = map
.map .map
.windows(len) .windows(file.len as usize)
.take(file.pos)
.enumerate() .enumerate()
.find(|(i, u)| { .find(|(_i, u)| {
u.iter().all(|u| match u { u.iter().all(|u| match u {
Unit::Free => true, Unit::Free => true,
_ => false, _ => false,
@ -170,16 +174,16 @@ fn problem2<T: BufRead>(input: Lines<T>) -> u64 {
}) })
.map(|(i, _)| i); .map(|(i, _)| i);
if let Some(free_pos) = free_pos { if let Some(free_pos) = free_pos {
for j in 0..len { // println!("moving {}@{:?} to {}", file.id, file.pos, free_pos);
map.map[free_pos + j] = map.map[i + j]; for j in 0..file.len {
map.map[i + j] = Unit::Free; 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);
if i > 0 {
i -= 1;
} }
} }
0 // println!("after: {}", map);
map.checksum()
} }
#[cfg(test)] #[cfg(test)]
@ -198,6 +202,6 @@ mod tests {
#[test] #[test]
fn problem2_example() { fn problem2_example() {
let c = Cursor::new(EXAMPLE); let c = Cursor::new(EXAMPLE);
assert_eq!(problem2(c.lines()), 0); assert_eq!(problem2(c.lines()), 2858);
} }
} }