Skip to content

Commit

Permalink
Complete day 9 part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardoholmes committed Dec 9, 2024
1 parent bd31e35 commit a008ef2
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 2 deletions.
1 change: 1 addition & 0 deletions 2024/examples/day_9_1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
2333133121414131402
68 changes: 67 additions & 1 deletion 2024/src/solutions/day_9.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,65 @@ pub fn part_one(input: &[Parsed]) -> Result<usize, String> {
}

pub fn part_two(input: &[Parsed]) -> Result<usize, String> {
Ok(0)
let mut grouped: Vec<(Option<usize>,usize)> = vec![];
let mut input_iter = input.iter();

while input_iter.len() > 0 {
// println!("{:?}", input_iter.clone().collect::<Vec<&Option<usize>>>());
let value = input_iter.next().unwrap().clone();
let count = 1 + input_iter.clone().take_while(|v| **v == value).count();
if count > 1 {
input_iter.nth(count-2); // -2 since next consumed one already and 0 index
}
grouped.push((value, count));
}

let mut index = grouped.len() - 1;
while index > 0 {
let (id, len) = grouped[index];

if id.is_some() {
let mut dest = None;
for (j, &(y, l)) in grouped[0..index].iter().enumerate() {
if y.is_some() || l < len {
continue;
}

dest = Some(j);
break;
}

if let Some(j) = dest {
if grouped[j].1 == len {
grouped[index].0 = None;
grouped[j].0 = id;
}
else { // need to split the new segment
grouped[index].0 = None;
grouped[j].0 = None;
grouped[j].1 = grouped[j].1 - len;
grouped.insert(j, (id, len));
index += 1; // avoid decrementing index
}
}
}

index -= 1;
}


Ok(
grouped.iter().fold((0,0), |(p,t), &(id,l)| {
(
p + l,
if let Some(x) = id {
t + x * (p..p+l).sum::<usize>()
} else {
t
}
)
}).1
)
}

#[cfg(test)]
Expand All @@ -54,5 +112,13 @@ mod tests {
let solution = part_one(&parsed);
assert_eq!(solution, Ok(1928));
}

#[test]
fn test_part2() {
let example = include_str!("../../examples/day_9_1.txt");
let parsed = parse(example).unwrap();
let solution = part_two(&parsed);
assert_eq!(solution, Ok(2858));
}
}

3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
| 6 | [:star2:][2019-6] | [:star2:][2020-6] | [:star2:][2021-6] | [:star2:][2022-6] | [:star2:][2023-6] | [:star2:][2024-6] |
| 7 | [:star:][2019-7] | [:star2:][2020-7] | [:star2:][2021-7] | [:star2:][2022-7] | [:star2:][2023-7] | [:star2:][2024-7] |
| 8 | [:star2:][2019-8] | [:star2:][2020-8] | [:star2:][2021-8] | [:star2:][2022-8] | [:star2:][2023-8] | [:star2:][2024-8] |
| 9 | | [:star2:][2020-9] | [:star2:][2021-9] | [:star2:][2022-9] | [:star2:][2023-9] | |
| 9 | | [:star2:][2020-9] | [:star2:][2021-9] | [:star2:][2022-9] | [:star2:][2023-9] | [:star2:][2024-9] |
| 10 | | [:star2:][2020-10] | [:star2:][2021-10] | [:star2:][2022-10] | [:star2:][2023-10] | |
| 11 | | [:star2:][2020-11] | [:star2:][2021-11] | [:star2:][2022-11] | [:star2:][2023-11] | |
| 12 | | [:star2:][2020-12] | [:star2:][2021-12] | [:star2:][2022-12] | [:star2:][2023-12] | |
Expand Down Expand Up @@ -147,3 +147,4 @@
[2024-6]: ./2024/src/solutions/day_6.rs
[2024-7]: ./2024/src/solutions/day_7.rs
[2024-8]: ./2024/src/solutions/day_8.rs
[2024-9]: ./2024/src/solutions/day_9.rs

0 comments on commit a008ef2

Please sign in to comment.