-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
91 lines (73 loc) · 2.27 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use std::fs;
use std::collections::HashSet;
fn next_nodes(start: (usize, usize), nodes: &Vec<Vec<u32>>, target: u32) -> Vec<(usize, usize)> {
let mut next_nodes = Vec::new();
if start.1 < nodes.len() - 1 && nodes[start.1 + 1][start.0] == target {
next_nodes.push((start.0, start.1 + 1));
}
if start.1 > 0 && nodes[start.1 - 1][start.0] == target {
next_nodes.push((start.0, start.1 - 1));
}
if start.0 < nodes[0].len() - 1 && nodes[start.1][start.0 + 1] == target {
next_nodes.push((start.0 + 1, start.1));
}
if start.0 > 0 && nodes[start.1][start.0 - 1] == target {
next_nodes.push((start.0 - 1, start.1));
}
next_nodes
}
fn dfs1(
start: (usize, usize),
nodes: &Vec<Vec<u32>>,
score: &mut i32,
visited: &mut HashSet<(usize, usize)>,
) {
if visited.contains(&(start.0, start.1)) {
return;
} else {
visited.insert((start.0, start.1));
}
if nodes[start.1][start.0] == 9 {
*score += 1;
return;
}
next_nodes(start, nodes, nodes[start.1][start.0] + 1)
.iter()
.for_each(|&n| dfs1(n, nodes, score, visited));
}
fn dfs2(start: (usize, usize), nodes: &Vec<Vec<u32>>, score: &mut i32) {
if nodes[start.1][start.0] == 9 {
*score += 1;
return;
}
next_nodes(start, nodes, nodes[start.1][start.0] + 1)
.iter()
.for_each(|&n| dfs2(n, nodes, score));
}
fn main() {
let input = fs::read_to_string("input.txt").expect("Should have been able to read the file");
let map: Vec<Vec<u32>> = input
.trim()
.lines()
.map(|l| l.chars().map(|c| c.to_digit(10).unwrap()).collect())
.collect();
let mut score = 0;
let initial_nodes: HashSet<_> = map
.iter()
.enumerate()
.flat_map(|(j, r)| {
r.iter()
.enumerate()
.filter_map(move |(i, &n)| if n == 0 { Some((i, j)) } else { None })
})
.collect();
initial_nodes
.iter()
.for_each(|&i| dfs1(i, &map, &mut score, &mut HashSet::<(usize, usize)>::new()));
println!("Scores (1): {score}");
score = 0;
initial_nodes
.iter()
.for_each(|&i| dfs2(i, &map, &mut score));
println!("Scores (2): {score}");
}