d09
This commit is contained in:
parent
b90d770c8a
commit
7f8a287764
3 changed files with 108 additions and 2 deletions
1
res/09/example
Normal file
1
res/09/example
Normal file
|
@ -0,0 +1 @@
|
|||
2333133121414131402
|
1
res/09/input
Normal file
1
res/09/input
Normal file
File diff suppressed because one or more lines are too long
108
src/days/d09.rs
108
src/days/d09.rs
|
@ -6,11 +6,115 @@ pub fn solve() {
|
|||
#[allow(unused)]
|
||||
let contents = fs::read_to_string(path).expect("Something went wrong reading the file");
|
||||
|
||||
let result: usize = 0;
|
||||
let orig_filesystem: Vec<isize> = contents
|
||||
.chars()
|
||||
.filter(|c| c.is_digit(10))
|
||||
.enumerate()
|
||||
.map(|(i, c)| {
|
||||
if i % 2 == 0 {
|
||||
vec![(i / 2) as isize; c.to_string().parse::<usize>().unwrap()]
|
||||
} else {
|
||||
vec![-1; c.to_string().parse::<usize>().unwrap()]
|
||||
}
|
||||
})
|
||||
.flatten()
|
||||
.collect();
|
||||
|
||||
let mut filesystem = orig_filesystem.clone();
|
||||
|
||||
let mut start = 0;
|
||||
let mut end = filesystem.len() - 1;
|
||||
|
||||
loop {
|
||||
while filesystem[start] != -1 {
|
||||
start += 1;
|
||||
}
|
||||
|
||||
while filesystem[end] == -1 {
|
||||
end -= 1;
|
||||
}
|
||||
|
||||
if start >= end {
|
||||
break;
|
||||
}
|
||||
|
||||
filesystem.swap(start, end);
|
||||
}
|
||||
|
||||
let result: usize = filesystem
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, n)| if *n == -1 { 0 } else { i * *n as usize })
|
||||
.sum();
|
||||
|
||||
println!("Result 1: {}", result);
|
||||
|
||||
let result: usize = 0;
|
||||
let mut filesystem = orig_filesystem.clone();
|
||||
|
||||
let mut free_space: Vec<(usize, usize)> = Vec::new();
|
||||
let mut iter = 0;
|
||||
while iter < filesystem.len() {
|
||||
if filesystem[iter] == -1 {
|
||||
let mut count = 0;
|
||||
while filesystem[iter] == -1 {
|
||||
count += 1;
|
||||
iter += 1;
|
||||
}
|
||||
free_space.push((iter - count, count));
|
||||
} else {
|
||||
iter += 1;
|
||||
}
|
||||
}
|
||||
|
||||
let mut iter = filesystem.len() - 1;
|
||||
while iter > 0 {
|
||||
while filesystem[iter] == -1 {
|
||||
if iter == 0 {
|
||||
break;
|
||||
}
|
||||
iter -= 1;
|
||||
}
|
||||
let typ = filesystem[iter];
|
||||
let end = iter;
|
||||
let mut start = iter;
|
||||
while filesystem[start] == typ {
|
||||
if start == 0 {
|
||||
break;
|
||||
}
|
||||
start -= 1;
|
||||
}
|
||||
start += 1;
|
||||
let len = end - start + 1;
|
||||
|
||||
if len == 0 {
|
||||
continue;
|
||||
}
|
||||
|
||||
let res = filesystem
|
||||
.windows(len)
|
||||
.enumerate()
|
||||
.find(|(_, win)| win.iter().all(|c| *c == -1));
|
||||
if res.is_none() {
|
||||
iter = start - 1;
|
||||
continue;
|
||||
}
|
||||
let (start_i, _) = res.unwrap();
|
||||
|
||||
if start_i >= start {
|
||||
iter = start - 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
for i in 0..len {
|
||||
filesystem.swap(start_i + i, start + i);
|
||||
}
|
||||
}
|
||||
|
||||
let result: usize = filesystem
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, n)| if *n == -1 { 0 } else { i * *n as usize })
|
||||
.sum();
|
||||
|
||||
println!("Result 2: {}", result);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue