Skip to content

Commit d0149ab

Browse files
committed
Day 12 - late
1 parent 4a3e36a commit d0149ab

File tree

4 files changed

+133
-4
lines changed

4 files changed

+133
-4
lines changed

Day 12 - Rust/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# [Day 12](https://adventofcode.com/2021/day/12) in [Rust](https://www.rust-lang.org/)
2+
3+
### Additional dependecies
4+
* "regex"

Day 12 - Rust/input.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
start-co
2+
ip-WE
3+
end-WE
4+
le-ls
5+
wt-zi
6+
end-sz
7+
wt-RI
8+
wt-sz
9+
zi-start
10+
wt-ip
11+
YT-sz
12+
RI-start
13+
le-end
14+
ip-sz
15+
WE-sz
16+
le-WE
17+
le-wt
18+
zi-ip
19+
RI-zi
20+
co-zi
21+
co-le
22+
WB-zi
23+
wt-WE
24+
co-RI
25+
RI-ip

Day 12 - Rust/main.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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+
}

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@
22
Done in variety of languages
33

44
### Progress
5-
![](https://img.shields.io/badge/day%20📅-14-blue)
6-
![](https://img.shields.io/badge/stars%20⭐-26-yellow)
7-
![](https://img.shields.io/badge/days%20completed-13-red)
5+
![](https://img.shields.io/badge/day%20📅-15-blue)
6+
![](https://img.shields.io/badge/stars%20⭐-28-yellow)
7+
![](https://img.shields.io/badge/days%20completed-14-red)
8+
9+
### Notes
10+
* In all programs input file must be provided in the source code (usually somewhere at the top)
11+
* Additioal input that has to be provided in the code is stated in readme for given day
812

913
### Languages
1014

@@ -21,7 +25,7 @@ Done in variety of languages
2125
|9|Processing||22||
2226
|10|Java||23||
2327
|11|C||24||
24-
|12|||25||
28+
|12|Rust||25||
2529
|13|Ruby|
2630

2731
![](https://i.redd.it/nx0xene4l1281.png)

0 commit comments

Comments
 (0)