please just dont look at the code ...
This commit is contained in:
		
							
								
								
									
										110
									
								
								d12/src/a1.rs
									
									
									
									
									
								
							
							
						
						
									
										110
									
								
								d12/src/a1.rs
									
									
									
									
									
								
							@@ -1,4 +1,114 @@
 | 
			
		||||
 | 
			
		||||
fn parse_input(inp :Vec<String>) -> Vec<Vec<char>> {
 | 
			
		||||
    inp.iter().map(|line| line.chars().collect::<Vec<char>>()).collect::<Vec<Vec<char>>>()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn bfs(map :Vec<Vec<char>>, start :(usize, usize), end :(usize, usize)) -> usize {
 | 
			
		||||
    let mut path_len = 0; 
 | 
			
		||||
 | 
			
		||||
    let mut discovered = vec![vec![false; map[0].len()]; map.len()]; 
 | 
			
		||||
    
 | 
			
		||||
    discovered[start.0][start.1] = true; 
 | 
			
		||||
 | 
			
		||||
    let mut queue :Vec<Vec<(usize, usize)>> = vec![]; 
 | 
			
		||||
 | 
			
		||||
    queue.push(vec![start]); 
 | 
			
		||||
 | 
			
		||||
    while !queue.is_empty() {
 | 
			
		||||
 | 
			
		||||
        let current_path = queue.remove(0); 
 | 
			
		||||
 | 
			
		||||
        path_len += 1; 
 | 
			
		||||
        
 | 
			
		||||
        
 | 
			
		||||
        
 | 
			
		||||
        if current_path.last().unwrap().eq(&end) {
 | 
			
		||||
            println!("{:?}", current_path); 
 | 
			
		||||
            return current_path.len(); 
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // up
 | 
			
		||||
        if current_path.last().unwrap().0 > 0 {
 | 
			
		||||
            let up = (current_path.last().unwrap().0 - 1, current_path.last().unwrap().1); 
 | 
			
		||||
            if (map[up.0][up.1] as i32 - map[current_path.last().unwrap().0][current_path.last().unwrap().1] as i32) < 2 {
 | 
			
		||||
                if !discovered[up.0][up.1] {
 | 
			
		||||
                    discovered[up.0][up.1] = true; 
 | 
			
		||||
                    let mut new_path = Vec::from(current_path.clone()); 
 | 
			
		||||
                    new_path.push(up); 
 | 
			
		||||
                    queue.push(new_path); 
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        // down
 | 
			
		||||
        if current_path.last().unwrap().0 < map.len() - 1 {
 | 
			
		||||
            let down = (current_path.last().unwrap().0 + 1, current_path.last().unwrap().1); 
 | 
			
		||||
            if (map[down.0][down.1] as i32 - map[current_path.last().unwrap().0][current_path.last().unwrap().1] as i32) < 2 {
 | 
			
		||||
                if !discovered[down.0][down.1] {
 | 
			
		||||
                    let mut new_path = Vec::from(current_path.clone()); 
 | 
			
		||||
                    discovered[down.0][down.1] = true; 
 | 
			
		||||
                    new_path.push(down); 
 | 
			
		||||
                    queue.push(new_path); 
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        // left
 | 
			
		||||
        if current_path.last().unwrap().1 > 0 {
 | 
			
		||||
            let left = (current_path.last().unwrap().0, current_path.last().unwrap().1 - 1); 
 | 
			
		||||
            if (map[left.0][left.1] as i32 - map[current_path.last().unwrap().0][current_path.last().unwrap().1] as i32) < 2 {
 | 
			
		||||
                if !discovered[left.0][left.1] {
 | 
			
		||||
                    discovered[left.0][left.1] = true; 
 | 
			
		||||
                    let mut new_path = Vec::from(current_path.clone()); 
 | 
			
		||||
                    new_path.push(left); 
 | 
			
		||||
                    queue.push(new_path); 
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        // right
 | 
			
		||||
        if current_path.last().unwrap().1 < map[0].len() - 1 {
 | 
			
		||||
            let right = (current_path.last().unwrap().0, current_path.last().unwrap().1 + 1); 
 | 
			
		||||
            if (map[right.0][right.1] as i32 - map[current_path.last().unwrap().0][current_path.last().unwrap().1] as i32) < 2 {
 | 
			
		||||
                if !discovered[right.0][right.1] {
 | 
			
		||||
                    discovered[right.0][right.1] = true; 
 | 
			
		||||
                    let mut new_path = Vec::from(current_path.clone()); 
 | 
			
		||||
                    new_path.push(right); 
 | 
			
		||||
                    queue.push(new_path);  
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return path_len;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn run(inp :Vec<String>) {
 | 
			
		||||
    
 | 
			
		||||
    let mut map = parse_input(inp); 
 | 
			
		||||
 | 
			
		||||
    let mut start = (0, 0); 
 | 
			
		||||
    let mut end = (0, 0); 
 | 
			
		||||
 | 
			
		||||
    // search for start & end marker & replace them 
 | 
			
		||||
    for i in 0..map.len() {
 | 
			
		||||
        for i2 in 0..map[i].len() {
 | 
			
		||||
            if map[i][i2] == 'S' {
 | 
			
		||||
                start = (i, i2); 
 | 
			
		||||
                map[i][i2] = 'a'; 
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if map[i][i2] == 'E' {
 | 
			
		||||
                end = (i, i2); 
 | 
			
		||||
                map[i][i2] = 'z'; 
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let path_len = bfs(map, start, end);
 | 
			
		||||
    println!("{}", path_len - 1);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										121
									
								
								d12/src/a2.rs
									
									
									
									
									
								
							
							
						
						
									
										121
									
								
								d12/src/a2.rs
									
									
									
									
									
								
							@@ -1,5 +1,126 @@
 | 
			
		||||
 | 
			
		||||
fn parse_input(inp :Vec<String>) -> Vec<Vec<char>> {
 | 
			
		||||
    inp.iter().map(|line| line.chars().collect::<Vec<char>>()).collect::<Vec<Vec<char>>>()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn bfs(map :&Vec<Vec<char>>, start :(usize, usize), end :(usize, usize)) -> Option<usize> {
 | 
			
		||||
    let mut path_len = 0; 
 | 
			
		||||
 | 
			
		||||
    let mut discovered = vec![vec![false; map[0].len()]; map.len()]; 
 | 
			
		||||
    
 | 
			
		||||
    discovered[start.0][start.1] = true; 
 | 
			
		||||
 | 
			
		||||
    let mut queue :Vec<Vec<(usize, usize)>> = vec![]; 
 | 
			
		||||
 | 
			
		||||
    queue.push(vec![start]); 
 | 
			
		||||
 | 
			
		||||
    while !queue.is_empty() {
 | 
			
		||||
 | 
			
		||||
        let current_path = queue.remove(0);         
 | 
			
		||||
        
 | 
			
		||||
        
 | 
			
		||||
        if current_path.last().unwrap().eq(&end) {
 | 
			
		||||
            return Some(current_path.len()); 
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        // up
 | 
			
		||||
        if current_path.last().unwrap().0 > 0 {
 | 
			
		||||
            let up = (current_path.last().unwrap().0 - 1, current_path.last().unwrap().1); 
 | 
			
		||||
            if (map[up.0][up.1] as i32 - map[current_path.last().unwrap().0][current_path.last().unwrap().1] as i32) < 2 {
 | 
			
		||||
                if !discovered[up.0][up.1] {
 | 
			
		||||
                    discovered[up.0][up.1] = true; 
 | 
			
		||||
                    let mut new_path = Vec::from(current_path.clone()); 
 | 
			
		||||
                    new_path.push(up); 
 | 
			
		||||
                    queue.push(new_path); 
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        // down
 | 
			
		||||
        if current_path.last().unwrap().0 < map.len() - 1 {
 | 
			
		||||
            let down = (current_path.last().unwrap().0 + 1, current_path.last().unwrap().1); 
 | 
			
		||||
            if (map[down.0][down.1] as i32 - map[current_path.last().unwrap().0][current_path.last().unwrap().1] as i32) < 2 {
 | 
			
		||||
                if !discovered[down.0][down.1] {
 | 
			
		||||
                    let mut new_path = Vec::from(current_path.clone()); 
 | 
			
		||||
                    discovered[down.0][down.1] = true; 
 | 
			
		||||
                    new_path.push(down); 
 | 
			
		||||
                    queue.push(new_path); 
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        // left
 | 
			
		||||
        if current_path.last().unwrap().1 > 0 {
 | 
			
		||||
            let left = (current_path.last().unwrap().0, current_path.last().unwrap().1 - 1); 
 | 
			
		||||
            if (map[left.0][left.1] as i32 - map[current_path.last().unwrap().0][current_path.last().unwrap().1] as i32) < 2 {
 | 
			
		||||
                if !discovered[left.0][left.1] {
 | 
			
		||||
                    discovered[left.0][left.1] = true; 
 | 
			
		||||
                    let mut new_path = Vec::from(current_path.clone()); 
 | 
			
		||||
                    new_path.push(left); 
 | 
			
		||||
                    queue.push(new_path); 
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        // right
 | 
			
		||||
        if current_path.last().unwrap().1 < map[0].len() - 1 {
 | 
			
		||||
            let right = (current_path.last().unwrap().0, current_path.last().unwrap().1 + 1); 
 | 
			
		||||
            if (map[right.0][right.1] as i32 - map[current_path.last().unwrap().0][current_path.last().unwrap().1] as i32) < 2 {
 | 
			
		||||
                if !discovered[right.0][right.1] {
 | 
			
		||||
                    discovered[right.0][right.1] = true; 
 | 
			
		||||
                    let mut new_path = Vec::from(current_path.clone()); 
 | 
			
		||||
                    new_path.push(right); 
 | 
			
		||||
                    queue.push(new_path);  
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return None;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
pub fn run(inp :Vec<String>) {
 | 
			
		||||
    
 | 
			
		||||
    let mut map = parse_input(inp); 
 | 
			
		||||
 | 
			
		||||
    let mut end = (0, 0); 
 | 
			
		||||
 | 
			
		||||
    for i in 0..map.len() {
 | 
			
		||||
        for i2 in 0..map[i].len() {
 | 
			
		||||
            if map[i][i2] == 'S' {
 | 
			
		||||
                map[i][i2] = 'a'; 
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            if map[i][i2] == 'E' {
 | 
			
		||||
                end = (i, i2); 
 | 
			
		||||
                map[i][i2] = 'z'; 
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    let mut min_path = usize::MAX; 
 | 
			
		||||
 | 
			
		||||
    for i in 0..map.len() {
 | 
			
		||||
        for i2 in 0..map[i].len() {
 | 
			
		||||
            if map[i][i2] == 'a' {
 | 
			
		||||
                let start = (i, i2); 
 | 
			
		||||
                let curr_path_len = bfs(&map, start, end); 
 | 
			
		||||
                if curr_path_len.is_none() {
 | 
			
		||||
                    continue;
 | 
			
		||||
                }
 | 
			
		||||
                if curr_path_len.unwrap() < min_path {
 | 
			
		||||
                    //println!("{}, {}", min_path, curr_path_len); 
 | 
			
		||||
                    min_path = curr_path_len.unwrap(); 
 | 
			
		||||
                } 
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //let path_len = bfs(map, start, end);
 | 
			
		||||
    println!("a2: {}", min_path - 1);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user