|
| 1 | +use std::fs; |
| 2 | +use regex::Regex; |
| 3 | +use std::collections::HashSet; |
| 4 | +use std::collections::HashMap; |
| 5 | +use std::time::Instant; |
| 6 | + |
| 7 | +const INPUT_FILE: &str = "./input.txt"; |
| 8 | + |
| 9 | +fn is_upper_case(s: &String) -> bool { |
| 10 | + return s.chars().all(|b| matches!(b, 'A'..='Z')); |
| 11 | +} |
| 12 | + |
| 13 | +fn dfs_part_one(vertex: &String, adj_list: &HashMap<String, HashSet<String>>, mut visited: HashSet<String>) -> i32 { |
| 14 | + let mut paths = 0; |
| 15 | + |
| 16 | + if vertex.eq(&"end".to_string()) { |
| 17 | + return 1; |
| 18 | + } |
| 19 | + |
| 20 | + if !is_upper_case(&vertex) { |
| 21 | + visited.insert(vertex.clone()); |
| 22 | + } |
| 23 | + |
| 24 | + let neighbours = &adj_list[vertex]; |
| 25 | + let unvisited_neighbours = neighbours.difference(&visited); |
| 26 | + |
| 27 | + for neighbour in unvisited_neighbours { |
| 28 | + paths += dfs_part_one(neighbour, adj_list, visited.clone()); |
| 29 | + } |
| 30 | + |
| 31 | + return paths; |
| 32 | +} |
| 33 | + |
| 34 | +fn dfs_part_two(vertex: &String, adj_list: &HashMap<String, HashSet<String>>, mut visited: HashSet<String>, small_revisited: bool) -> i32 { |
| 35 | + let mut paths = 0; |
| 36 | + |
| 37 | + if vertex.eq(&"end".to_string()) { |
| 38 | + return 1; |
| 39 | + } |
| 40 | + |
| 41 | + if !is_upper_case(&vertex) { |
| 42 | + visited.insert(vertex.clone()); |
| 43 | + } |
| 44 | + |
| 45 | + let neighbours = &adj_list[vertex]; |
| 46 | + |
| 47 | + for neighbour in neighbours { |
| 48 | + if neighbour.eq(&"start".to_string()) { |
| 49 | + continue; |
| 50 | + } |
| 51 | + |
| 52 | + if !visited.contains(neighbour) { |
| 53 | + paths += dfs_part_two(neighbour, adj_list, visited.clone(), small_revisited); |
| 54 | + } else if !small_revisited { |
| 55 | + paths += dfs_part_two(neighbour, adj_list, visited.clone(), true); |
| 56 | + } |
| 57 | + |
| 58 | + } |
| 59 | + |
| 60 | + return paths; |
| 61 | +} |
| 62 | + |
| 63 | +fn main() { |
| 64 | + let mut start = Instant::now(); |
| 65 | + |
| 66 | + let mut adj_list: HashMap<String, HashSet<String>> = HashMap::new(); |
| 67 | + |
| 68 | + let file_content = fs::read_to_string(INPUT_FILE).expect("File not found"); |
| 69 | + let line_regex = Regex::new(r"(\w+)-(\w+)").unwrap(); |
| 70 | + |
| 71 | + for capture in line_regex.captures_iter(&file_content) { |
| 72 | + let left = (&capture[1]).to_string(); |
| 73 | + let left_copy = left.clone(); |
| 74 | + let right = (&capture[2]).to_string(); |
| 75 | + let right_copy = right.clone(); |
| 76 | + |
| 77 | + (*adj_list.entry(left).or_insert(HashSet::new())).insert(right_copy); |
| 78 | + (*adj_list.entry(right).or_insert(HashSet::new())).insert(left_copy); |
| 79 | + } |
| 80 | + |
| 81 | + /* |
| 82 | + for (vertex, egde_list) in (&adj_list).into_iter() { |
| 83 | + println!("\"{:5}\": {:?}", &vertex, &egde_list); |
| 84 | + } |
| 85 | + */ |
| 86 | + |
| 87 | + println!("Parsing: {} ms", start.elapsed().as_millis()); |
| 88 | + |
| 89 | + start = Instant::now(); |
| 90 | + let p1_result = dfs_part_one(&"start".to_string(), &adj_list, HashSet::new()); |
| 91 | + println!("Part 1: {} in {} ms", p1_result, start.elapsed().as_millis()); |
| 92 | + |
| 93 | + start = Instant::now(); |
| 94 | + let p2_result = dfs_part_two(&"start".to_string(), &adj_list, HashSet::new(), false); |
| 95 | + println!("Part 2: {} in {} ms", p2_result, start.elapsed().as_millis()); |
| 96 | +} |
0 commit comments