-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
125 lines (109 loc) · 3.3 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use std::collections::HashMap;
use std::collections::HashSet;
use std::io::prelude::*;
use std::io::stdin;
fn main() {
let map: Vec<Vec<char>> = stdin()
.lock()
.lines()
.map(|line| line.unwrap().chars().collect())
.collect();
let start = map[0].iter().position(|&c| c == '.').unwrap();
let (m, n) = (map.len(), map[0].len());
let visited = vec![vec![false; n]; m];
println!("Part 1: {}", solve_part_1(0, start, &map, &visited));
println!("Part 2: {}", solve_part_2(start, &map));
}
fn solve_part_1(i: usize, j: usize, map: &Vec<Vec<char>>, visited: &Vec<Vec<bool>>) -> i32 {
if i >= map.len() || j >= map[0].len() || map[i][j] == '#' || visited[i][j] {
return i32::MIN;
}
if i == map.len() - 1 && map[i][j] == '.' {
return 0;
}
let mut visited = visited.clone();
visited[i][j] = true;
1 + match map[i][j] {
'.' => solve_part_1(i + 1, j, map, &visited)
.max(solve_part_1(i, j + 1, map, &visited))
.max(solve_part_1(i - 1, j, map, &visited))
.max(solve_part_1(i, j - 1, map, &visited)),
'v' => solve_part_1(i + 1, j, map, &visited),
'>' => solve_part_1(i, j + 1, map, &visited),
'^' => solve_part_1(i - 1, j, map, &visited),
'<' => solve_part_1(i, j - 1, map, &visited),
_ => unreachable!(),
}
}
fn solve_part_2(start: usize, map: &Vec<Vec<char>>) -> i32 {
let compressed = compress_grid(map);
dfs(0, start, map.len() - 1, &HashSet::new(), &compressed)
}
fn dfs(
i: usize,
j: usize,
end: usize,
visited: &HashSet<(usize, usize)>,
map: &HashMap<(usize, usize), Vec<(usize, usize)>>,
) -> i32 {
let mut visited = visited.clone();
if !visited.insert((i, j)) {
return i32::MIN;
}
if i == end {
return 0;
}
map.get(&(i, j))
.unwrap()
.iter()
.map(|&(x, y)| {
dfs(x, y, end, &visited, map)
+ (i as i32 - x as i32).abs()
+ (j as i32 - y as i32).abs()
})
.max()
.unwrap_or(-1)
}
fn compress_grid(map: &Vec<Vec<char>>) -> HashMap<(usize, usize), Vec<(usize, usize)>> {
let mut compressed = HashMap::new();
let (m, n) = (map.len(), map[0].len());
let mut push = |(a, b), (c, d)| {
compressed.entry((a, b)).or_insert(Vec::new()).push((c, d));
compressed.entry((c, d)).or_insert(Vec::new()).push((a, b));
};
for i in 0..m {
let mut k = 0;
for j in 0..n {
if map[i][j] == '#' {
k = j + 1;
continue;
}
if j == n - 1
|| (i + 1 < m && map[i + 1][j] != '#')
|| (i - 1 < m && map[i - 1][j] != '#')
{
if j > k {
push((i, j), (i, k));
}
}
}
}
for j in 0..n {
let mut k = 0;
for i in 0..m {
if map[i][j] == '#' {
k = i + 1;
continue;
}
if i == m - 1
|| (j + 1 < n && map[i][j + 1] != '#')
|| (j - 1 < n && map[i][j - 1] != '#')
{
if i > k {
push((i, j), (k, j));
}
}
}
}
compressed
}