Skip to content

Commit

Permalink
2024_09 Part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Himangshu Saikia committed Dec 9, 2024
1 parent 451104c commit d5cc2f8
Showing 1 changed file with 80 additions and 105 deletions.
185 changes: 80 additions & 105 deletions src/bin/2024_09/main.rs
Original file line number Diff line number Diff line change
@@ -1,133 +1,108 @@
use std::collections::HashMap;
use std::fmt::Debug;

use aoc::common;
use aoc::{common, range::Range};

fn solve2(input: &str) -> usize {
let mut ans = 0;
let l = input.len();
let mut id_spaces_map : HashMap<usize, usize> = HashMap::new();
for c in input.chars().into_iter() {
#[derive(Clone)]
enum SpaceType {
Free,
Occupied(usize),
}

struct Space {
t: SpaceType,
r: Range<usize>,
}

impl Debug for Space {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut s = String::new();
for _ in self.r.a..self.r.b {
let t = match &self.t {
SpaceType::Free => ".",
SpaceType::Occupied(id) => &id.to_string(),
};
s.push_str(t);
}
write!(f, "{}", s)
}
}

fn solve<const PART: i32>(input: &str) -> usize {
let mut idx = 0;
let mut spaces = Vec::new();

for (i, c) in input.chars().enumerate() {
let x = c.to_digit(10).unwrap() as usize;
let id = id_spaces_map.len();
id_spaces_map.insert(id, x);
spaces.push(Space {
t: if i % 2 == 0 {
SpaceType::Occupied(i / 2)
} else {
SpaceType::Free
},
r: Range::new(idx, idx + x),
});
idx += x;
}

loop {
let l = spaces.len();
let mut found = false;
// even is file and odd is free space
for odd in (0..l).filter(|x| x % 2 == 1) {
for even in (0..l).filter(|x| x % 2 == 0) {
let free = *id_spaces_map.get(&odd).unwrap();
let file = *id_spaces_map.get(&even).unwrap();
if free >= file {
id_spaces_map.entry(odd).and_modify(|v| *v -= file);
id_spaces_map.entry(even).and_modify(|v| *v -= file);
found = true;
for i in (0..l).rev() {
if let SpaceType::Occupied(id) = spaces[i].t {
for j in 0..i {
if let SpaceType::Free = spaces[j].t {
let file = spaces[i].r;
let free = spaces[j].r;
if PART == 1 {
let spr = file.spread().min(free.spread());
found = true;
spaces[i].r = Range::new(free.a, free.a + spr);
spaces[j].r = Range::new(free.a + spr, free.b);
spaces.push(Space {
t: SpaceType::Occupied(id),
r: Range::new(file.a, file.b - spr),
});
break;
} else if free.spread() >= file.spread() {
let spr = file.spread();
found = true;
spaces[i].r = Range::new(free.a, free.a + spr);
spaces[j].r = Range::new(free.a + spr, free.b);
break;
}
}
}
if found {
break;
}
}

if found {
break;
}
}

if !found {
break;
}
}

0
}

fn solve<const PART: i32>(input: &str) -> u64 {
let mut ans = 0;
//let mut dk : [u64; 100000] = []
let mut id_spaces_map : HashMap<u64, u64> = HashMap::new();
let mut free_spaces : Vec<u64> = Vec::new();
let mut idx = 0;
for (i, c) in input.chars().into_iter().enumerate() {
let x = c.to_digit(10).unwrap() as u64;
if i % 2 == 0 {
let id = id_spaces_map.len() as u64;
id_spaces_map.insert(id, x);
} else {
for i in idx..idx + x {
free_spaces.push(i);
}
spaces.retain(|s| s.r.spread() > 0);
spaces.sort_by(|s1, s2| s1.r.a.cmp(&s2.r.a));
}
idx += x;
}
// println!("{id_spaces_map:?}");

// let mut max_id = *id_spaces_map.iter().filter(|(_, v)| *v > &0).map(|(k, _)| k).max().unwrap();

// println!("Max key {max_id} total space {idx}");

let mut arr : [u64; 95000] = [0; 95000];
let mut id = 0;
let mut idx : u64 = 0;

for (i, c) in input.chars().into_iter().enumerate() {

// if id_spaces_map.iter().filter(|(k, v)| *k >= &id && *v > &0).count() == 0 {
// break;
// }

let x = c.to_digit(10).unwrap() as u64;
//let mut done = false;
if i % 2 == 0 {
for j in idx..idx + x {
if id_spaces_map.iter().filter(|(k, v)| *k >= &id && *v > &0).count() == 0 {
break;
}
//println!("Id {id} Idx {j}");
ans += id * j;
id_spaces_map.entry(id).and_modify(|v| *v -= 1);
}
id += 1;
} else {
for j in idx..idx + x {
if let Some(max_id) = id_spaces_map.iter().filter(|(_, v)| *v > &0).map(|(k, _)| k).max() {
ans += *max_id * j;
//println!("Id {max_id} Idx {j}");
id_spaces_map.entry(*max_id).and_modify(|v| *v -= 1);
} else {
//done = true;
break;
}
//println!("{id_spaces_map:?}");
let mut ans = 0;
for s in spaces {
if let SpaceType::Occupied(id) = s.t {
for i in s.r.a..s.r.b {
ans += id * i;
}
}
idx += x;

// if done {
// break;
// }
}

//max_key(&mut id_spaces_map);

//println!("{id_spaces_map:?}");
//println!("{free_spaces:?}");

// let mut ptr1 = 0;
// let mut ptr2 =
// for (i, c) in input.chars().into_iter().enumerate() {
// let x = c.to_digit(10).unwrap();
// if i % 2 == 1 {
// free_space += x;
// }
// }

ans
}

fn main() {
let input = common::get_input();
//println!("{input:?}");
println!("{input:?}");
common::timed(&input, solve::<1>, true);
//common::timed(&input, solve::<2>, false);
common::timed(&input, solve::<2>, false);
}

#[cfg(test)]
Expand All @@ -136,8 +111,8 @@ mod tests {

#[test]
fn test_samples() {
let sample_input = "............\n........0...\n.....0......\n.......0....\n....0.......\n......A.....\n............\n............\n........A...\n.........A..\n............\n............";
assert_eq!(solve::<1>(sample_input), 14);
assert_eq!(solve::<2>(sample_input), 34);
let sample_input = "2333133121414131402";
assert_eq!(solve::<1>(sample_input), 1928);
assert_eq!(solve::<2>(sample_input), 2858);
}
}

0 comments on commit d5cc2f8

Please sign in to comment.