day_9: fixed marginal case and validated
This commit is contained in:
@@ -11,7 +11,7 @@ https://adventofcode.com/2024
|
|||||||
| 06 | Guard Gallivant | :pushpin: | :pushpin: | 2D navigation |
|
| 06 | Guard Gallivant | :pushpin: | :pushpin: | 2D navigation |
|
||||||
| 07 | Bridge Repair | :pushpin: | :pushpin: | Equation parsing |
|
| 07 | Bridge Repair | :pushpin: | :pushpin: | Equation parsing |
|
||||||
| 08 | Resonant Collinearity | :pushpin: | | Matrix exploration |
|
| 08 | Resonant Collinearity | :pushpin: | | Matrix exploration |
|
||||||
| 09 | Disk fragmenter | | | |
|
| 09 | Disk fragmenter | :pushpin: | | Array indexing |
|
||||||
| 10 | | | | |
|
| 10 | | | | |
|
||||||
| 11 | | | | |
|
| 11 | | | | |
|
||||||
| 12 | | | | |
|
| 12 | | | | |
|
||||||
|
|||||||
@@ -1,14 +1,10 @@
|
|||||||
use super::{utils::read_disk_map_input, DiskChecksum, DiskMap};
|
use super::{utils::read_disk_map_input, DiskChecksum, DiskMap};
|
||||||
|
|
||||||
// Last result: 6349343813585
|
|
||||||
|
|
||||||
pub fn disk_fragmenter(input: &str) -> DiskChecksum {
|
pub fn disk_fragmenter(input: &str) -> DiskChecksum {
|
||||||
let disk_map = read_disk_map_input(input);
|
let disk_map = read_disk_map_input(input);
|
||||||
|
|
||||||
let organized_disk_map = organize_disk_map(disk_map);
|
let organized_disk_map = organize_disk_map(disk_map);
|
||||||
|
|
||||||
println!("Organized map: {:?}", organized_disk_map);
|
|
||||||
|
|
||||||
calc_disk_check_sum(&organized_disk_map)
|
calc_disk_check_sum(&organized_disk_map)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -18,20 +14,16 @@ pub fn organize_disk_map(mut disk_map: DiskMap) -> DiskMap {
|
|||||||
|
|
||||||
'outer: loop {
|
'outer: loop {
|
||||||
if end_index <= start_index || start_index > disk_map.len() - 1 {
|
if end_index <= start_index || start_index > disk_map.len() - 1 {
|
||||||
println!(
|
|
||||||
"Broke out of outer: {} {} with len {}",
|
|
||||||
start_index,
|
|
||||||
end_index,
|
|
||||||
disk_map.len()
|
|
||||||
);
|
|
||||||
break 'outer;
|
break 'outer;
|
||||||
}
|
}
|
||||||
|
|
||||||
if let None = disk_map[start_index] {
|
if let None = disk_map[start_index] {
|
||||||
'inner: loop {
|
'inner: loop {
|
||||||
|
if end_index <= start_index {
|
||||||
|
break 'inner;
|
||||||
|
}
|
||||||
if let Some(_) = disk_map[end_index] {
|
if let Some(_) = disk_map[end_index] {
|
||||||
disk_map.swap(start_index, end_index);
|
disk_map.swap(start_index, end_index);
|
||||||
end_index -= 1;
|
|
||||||
break 'inner;
|
break 'inner;
|
||||||
}
|
}
|
||||||
end_index -= 1;
|
end_index -= 1;
|
||||||
|
|||||||
@@ -170,7 +170,8 @@ pub fn read_disk_map_input(input: &str) -> DiskMap {
|
|||||||
|
|
||||||
let mut id: usize = 0;
|
let mut id: usize = 0;
|
||||||
for (digit_index, char_digit) in raw_map.chars().enumerate() {
|
for (digit_index, char_digit) in raw_map.chars().enumerate() {
|
||||||
if let Some(digit) = char_digit.to_digit(10u32) {
|
if let Some(digit) = char_digit.to_digit(10) {
|
||||||
|
if digit > 0 {
|
||||||
for _ in 0..digit {
|
for _ in 0..digit {
|
||||||
if digit_index % 2 == 0 {
|
if digit_index % 2 == 0 {
|
||||||
disk_map.push(Some(id));
|
disk_map.push(Some(id));
|
||||||
@@ -183,6 +184,7 @@ pub fn read_disk_map_input(input: &str) -> DiskMap {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
disk_map
|
disk_map
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user